i had a sub routine for printing a string dynamically, without manually listing the length of the string in x86-64 intel style through following code (with stack) :-
section .data
output1 db "hello world",10,0
output2 db "hello world again",10,0
section .bss
section .text
global _start
_start:
mov rax,output1
call _output
mov rax,output2
call _output
call _exit
_output:
_output_setup:
push rax
mov rbx, 0
_output_length_loop:
inc rax
inc rbx
mov cl,[rax]
cmp cl, 0
jne _output_length_loop
_output_output:
mov rax, 1
mov rdi, 1
pop rsi
mov rdx, rbx
syscall
ret
_exit:
mov rax, 60
mov rdi, 0
syscall
ret
but i wanted the sub routine without stack so i tried writing the sub routine to do the same without using the stack so i did through following:-
section .data
output1 db "print this string",10,0
output2 db "print this string again",10,0
section .text
global _start
_start:
mov rcx,output1
call _output
mov rcx,output2
call _output
call _exit
_output:
_output_setup:
mov rbx,0
_output_length_loop:
inc rbx
inc rcx
mov dl,[rcx]
cmp dl,0
jne _output_length_loop
_output_output:
mov rax,0
mov rdi,0
mov rsi,rcx
mov rdx,rbx
syscall
ret
_exit:
mov rax,60
mov rdi,0
syscall
ret
it complied without error but when i ran the executable it, for some reason got stuck presumably it created a infinite loop for some reason so i had heard about a tool called GDB for debugging but being honest it was a mess for me but i did a line by line register analysis but i was not able to deduce reason for above
my line by line analysis:-
rcx rbx dl_
_start
b1 - mov rcx, output1
- call _output
_output
0 - mov rbx,0
_output_loop_length -0
1 - inc rbx
ib1 - inc rcx
p - mov dl,[rcx]
- cmp dl,0 (p != 0)
- jne _output_length_loop
_output_loop_length -1
2 - inc rbx
ib1 - inc rcx
r - mov dl,[rcx]
- cmp dl,0 (p != 0)
- jne _output_length_loop
...
_output_loop_length -2
3 - inc rbx
ib1 - inc rcx
0 - mov dl,[rcx]
- cmp dl,0 (p == 0)
- jne _output_length_loop
...
b1 stands for init byte
i in front of b1 stands for increased position of the init byte
i suspect "inc rcx" to be creating a infinite loop some how but i am not sure how in addition i have some ambiguity related the respective line.