Hello all,
I'm new to assembly and Frank was very helpful in my initial posting to the nasm-users mailing list, pointing me in this direction for further information. All have been very helpful. However in contiuing my learning experience I am having a really rough time trying to understand how to write to memory locations to addresses passed to subroutines on the stack. I have a very brief example that I will provide, and if someone cares to point out specifically why this doesn't work, please let me know. I'm assuming it has to do with alignment or something, but I'm not well versed and not sure if that is the proper terminology. Without further adeiu, I am trying to mimic the bzero c library function available in all POSIX compliant machines. This is for self educational purposes only to help me understand the basics. I understand the C language quite well as it's what I'm most familiar with, but would like to explore asm in more depth. Thanks in advance!
# C Prototype
void bzero(char *addr, unsigned int);
#############################
section .text
global bzero
bzero:
push ebp ;
mov ebp, esp ; Stack Frame
mov eax, [ebp+8] ; Move address of pointer into eax
mov ecx, [ebp+12] ; Move length into ecx
.loop:
cmp ecx, 0 ; Is length zero?
je .done ; Yep, jump to done.
mov byte [eax], 0 ; Move a zero into the address referenced in eax.
sub ecx, 1 ; Subtract 1 from length
add eax, 1 ; Add one to address
jmp .loop ; Jump to the top of the loop
.done:
pop ebp ; Restore Frame
ret ; Return
#############################
The above results in a segmentation fault do the the following "mov byte [eax], 0". The address stored in eax is correct due to the debugging I've already done, namely after loading the address into eax I've returned from the routine and printed the value of the address in c. It matched my original pointer passed to the subroutine. Knowing this, I am nearly certain the problem is in fact "trying to move a zero into the address referenced in eax".
I've been scouring google for nasm examples detailing a specific example of a subroutine which does this but I've come up empty. This question is trivial, and I am hoping the answer is quite trivial as well.
I appreciate any help with my lack in asm knowledge, and thank those that respond in advance.
~Paul
Host: FreeBSD 7.2 (32bit) Pentium 3
nasm -ggnu -f elf -o bzero.o bzero.s
gcc -g -o test test.c bzero.o