I'm having trouble finding an answer to this seemingly innocuous and simple question. I wish to use cmp or one of its derivatives (cmpl, cmpb, etc...) to compare two values in a GAS assembly program. The problem is, when I run multiple comparisons that should come out differently, they come out the same. I believe it involves my misunderstanding of how data is compared with the cmp operation.
Here is the situation:
I have a fill variable to take input like this, with a equ to hold the size:
buff: .fill 20
.equ bufLen, .-buff
Then I put the bufLen variable in a register, and the comparison value in another:
movl $bufLen, %eax
movl $0x03, %ebx
Finally, I compare and if compare equal, jump to another line:
cmpl %eax, %ebx
je anotherplace
However, when I compare inputs of lengths 2 and 4, they both come out less than (I changed the je to jl for a quick debug). Can anybody tell me what I'm doing wrong or point me to a question I missed that might tell me how I messed up?
Just as a reminder, this is GAS assembly architecture.
All help is much appreciated.
According to the comments, I show you some ways to get the length of a zero-terminated string (.asciz
) in Linux:
witch.s:
.data
witches: .asciz "Double, double toil and trouble; Fire burn, and cauldron bubble"
format0: .asciz "%s\n"
format1: .asciz "Return of printf: %u\n"
format2: .asciz "Return of strlen: %u\n"
format3: .asciz "Return of repne scasb: %u\n"
.text
.global main
main:
push $witches
push $format0
call printf # returns in EAX the amount of printed chars (+ \n!)
add $8, %esp
push %eax
push $format1
call printf
add $8, %esp
push $witches
call strlen # returns in EAX the length of the string
add $4, %esp
push %eax
push $format2
call printf
add $8, %esp
mov $witches, %edi
xor %al, %al
xor %ecx, %ecx
dec %ecx
repne scasb
neg %ecx # returns in ECX the length of the string + 2
push %ecx
push $format3
call printf
add $8, %esp
mov $0, %eax # return 0;
ret
Compile & run:
gcc -m32 witch.s
./a.out
There are more ways