How does the following program look to print a list of strings? What places can I improve? Are there easier ways to print something like a linebreak after each string rather than hardcoding the \n
into the string itself?
# Program, print out a list of strings, one per line
.data
SYS_EXIT = 60
SYS_WRITE = 1
SYS_STDOUT = 1
# Empty string means end of strings
strings: .asciz "Once\n", "upon\n", "a\n", "time\n", "...\n", ""
.text
.globl _start
get_string_length:
mov $0, %eax
.L1_loop:
movzbl (%edi, %eax), %ecx
cmp $0, %cl
je .L1_exit
inc %eax
jmp .L1_loop
.L1_exit:
ret
_start:
mov $strings, %rbx
print_loop:
mov %rbx, %rdi
call get_string_length # (rdi=file_descriptor, rsi=starting_address, rdx=size)
cmp $0, %eax
jz exit
mov $SYS_STDOUT,%edi
mov %rbx, %rsi
mov %eax, %edx
mov $SYS_WRITE, %eax
syscall
lea 1(%eax, %ebx,), %ebx
jmp print_loop
exit:
mov $0, %edi
mov $SYS_EXIT, %eax
syscall