printf
returns its result in %eax
, thereby trashing your character. You do: addl $8, %esp
. That isn't enough - you've pushed three parameters. As it happens, this leaves your old %eax
on the stack, where a popl %eax
might solve your problem.
Why does pushing and printing the value in a register affect later comparisons of that register to another?
Question
I'm attempting to write a short assembly program to compare 2 strings. I'm using AT&T syntax and assembling my program with gcc and the -m32 flag.
When I run the code, I always enter "rectangle" when prompted.
I have written the following code. With the questionable block of code (enclosed in comments) active, if I enter "rectangle" when prompted, the program does not print "You entered rectangle." In fact, the program jumps out of the loop on the first iteration. If I comment out said block of code, the program does print "You entered rectangle." Since this block of code only prints the values in %eax and %ebx, why does it affect the jump condition?
.section .data
format:
.asciz "%s"
test_str:
.asciz "\n%c%c"
equal_str:
.asciz "You entered rectangle"
rectangle:
.asciz "rectangle"
.section .bss
.lcomm element, 100
.section .text
.globl main
main:
pushl $element
pushl $format
call scanf
addl $8, %esp
movl $0, %edi
loop:
movb element(,%edi,1), %al
movb rectangle(,%edi,1), %bl
#BEGIN QUESTIONABLE BLOCK
pushl %eax
pushl %ebx
pushl $test_str
call printf
addl $8, %esp
#END QUESTIONABLE BLOCK
cmpb %al, %bl
jne end
incl %edi
cmpb $0, %al
jne loop
pushl $equal_str
call printf
addl $4, %esp
jmp end
end:
call exit
I suppose none of the following information matters, but I really have no idea what could or couldn't affect assembly, so my CPU is an Intel Xeon E5-2650 and I'm using CentOS 6.5. Thanks in advance!
Solution