aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2024-07-13 21:32:21 -0700
committerChristian Cunningham <cc@localhost>2024-07-13 21:32:21 -0700
commita4e81a265038b42acd27ef315d1796dd332c50e0 (patch)
treecca5b5af2a4285fdb2b3060bbd06701f9ef09101
parent537b6c834b541b1f8f15f2ad31dcdc7f9d9a71a5 (diff)
Strings
-rw-r--r--lstring.inc35
-rw-r--r--zstring.inc57
2 files changed, 92 insertions, 0 deletions
diff --git a/lstring.inc b/lstring.inc
new file mode 100644
index 0000000..b7eed88
--- /dev/null
+++ b/lstring.inc
@@ -0,0 +1,35 @@
+%ifndef LSTRING_INC
+%define LSTRING_INC
+%ifndef SYS_INC
+%include "sys.inc"
+%endif
+
+%macro make_lstring 2-*
+%1_len_b: dq %1_len
+%1: db %2
+ %rep %0-2
+ db %3
+ %rotate 1
+ %endrep
+ %rotate 2
+%1_len equ $-%1
+%endm
+
+ section .text
+print_lstring:
+ ;; rax = lstring address
+ push rbp
+ mov rbp, rsp
+ ;;
+ push rax
+ mov rdx, [rax]
+ mov rsi, rax
+ add rsi, 8
+ mov rax, SYS_WRITE
+ mov rdi, 1
+ syscall
+ ;;
+ mov rsp, rbp
+ pop rbp
+ ret
+%endif
diff --git a/zstring.inc b/zstring.inc
new file mode 100644
index 0000000..5c88c02
--- /dev/null
+++ b/zstring.inc
@@ -0,0 +1,57 @@
+%ifndef ZSTRING_INC
+%define ZSTRING_INC
+%ifndef SYS_INC
+%include "sys.inc"
+%endif
+%ifndef ALLOC_INC
+%include "alloc.inc"
+%endif
+
+%macro make_zstring 2-*
+%1: db %2
+ %rep %0-2
+ db %3
+ %rotate 1
+ %endrep
+ db 0
+%endm
+
+ section .text
+length_zstring:
+ ;; rax = zstring address
+ push rbp
+ mov rbp, rsp
+ ;;
+ mov rdx, rax
+ xor rax, rax
+.loop:
+ mov cl, byte [rdx]
+ cmp cl, 0
+ je .exit
+ inc rax
+ inc rdx
+ jmp .loop
+.exit:
+ ;;
+ mov rsp, rbp
+ pop rbp
+ ret
+
+print_zstring:
+ ;; rax = zstring address
+ push rbp
+ mov rbp, rsp
+ ;;
+ push rax
+ mov rsi, rax
+ call length_zstring
+ mov rdx, rax
+ mov rax, SYS_WRITE
+ mov rdi, 1
+ syscall
+ ;;
+ mov rsp, rbp
+ pop rbp
+ ret
+
+%endif