Hey!
After an hour of sweating it out, I finally figured out how to do it. I didn't push or pop it at all, although i got some problem with it, that only the function and exit could write to it. Other labels couldn't write values to it :S
The more likely case of pushing eax onto the stack however, seems to me to be that the calling code had a value in eax it wanted to preserve, so before pushing on all the function parameters, it pushed eax to save the value during the function call. It would then do whatever it needs to with the return value that is in eax when the function is done, then pop the old eax off the stack for use.
I was the one that pushed eax to the stack. Since that didn't work, I just didn't do it again.
So if i returned control back to main. How would I pop %eax, because I would be in the C code then.
This is what I got so far. appears to be working
Code:
#include <stdio.h>
int cmpString(char *s1, char *s2);
int main()
{
char string1[]="Hello World" ;
char string2[]="Hello World" ;
if(!cmpString(string1,string2))
{
printf("The strings match!\n") ;
}
else
{
printf("The strings do not match!\n") ;
}
return 0 ;
}
/*c function to compare strings
int cmpString(char *s1, char *s2)
{
while((*s1!='\0'))
{
if(!(*s1==*s2))
{
return 1;
}
s1++ ;s2++ ;
}
return 0;
}
*/
Assembler code to compare string.
.section .data
.section .text
.global cmpString
cmpString:
push %ebp
movl %esp, %ebp
pushl %esi #save contents of esi
pushl %edi #save contents of edi
pushl %ebx #save contents of ebx
movl 12(%ebp), %esi #point to s2
movl 8(%ebp), %edi#point to string1
cmpLoop:
cmpb $0, (%esi)
je exit
movb (%esi),%bl
cmpb %bl, (%edi)
movl $1, %eax
jnz exit
incl %esi
incl %edi
movl $0, %eax
jmp cmpLoop
exit:
popl %ebx
popl %edi
popl %esi
popl %ebp
ret
.end