I am a beginner in assembly and I have this homework where I have to create a strlen
function to find the length of any string.
I tried subtracting 4 from edx
because I am seeing 4 extra characters at the end, but that did not fix anything. They are still there.
section .data
text: db "Hello world, trying to find length of string using function." ;our string to be outputted
section .text
global _start ;declared for linker
_start:
mov eax, 4 ;system call number (sys write)
mov ebx, 1 ;file descriptor to write-only
mov ecx, text ;message to output
call strlen
mov edx, len ;length of string to print
int 80h ;interrupt
exit:
mov eax, 1 ;system call number (sys exit)
mov ebx, 0 ;file descriptor to read-only
int 80h ;interrupt
strlen:
push ebp ;prologue, save base pointer
mov ebp, esp ;copy esp to ebp
push edi ;push edi for use
;body
mov edi, text ;save text to edi, and i think when i do that edi expands? if text = 5 bytes, and edi was originally 4, then edi becomes 5?
sub edi, esp ;subtract edi starting point by the esp starting point to get len. ex: edi = 100, esp = 95
mov [len], edi ;copy value of edi onto len
pop edi ;epilogue, pop edi out of stack
mov esp, ebp ;return esp back to top of stack
pop ebp ;pop ebp back to original
ret ;return address
section .bss
len: resb 4 ;4 byte to integer
Let say I have the follow code in the .data
section:
section .data
text: db "Hello world, trying to find length of string using function."
The expected output should be "Hello world, trying to find length of string using function.", however I am getting "Hello world, trying to find length of string using function.####" where #
is any random character.
Thank you.