I've been looking for hours, and i can't find the mistake in my code. The program seems to exit at the "opadd" tag. I am using x86_64 on Linux, with AT&T syntax.
The program takes as input a string of characters, for example, "2 3 add 4 mul", and then, for this particular example, should do as follows:
- adds 2 to the stack
- adds 3 to the stack
- computes the sum of 2 and 3 and then adds 5 to the stack
- multiplies 5 and 4 and adds 20 to the stack, and then prints 20.
.data
formatPrintf: .asciz "%d"
sir: .space 1000
delim: .asciz " "
formatScanf: .asciz "%1000[^\n]"
cuvant: .space 100
primulNumar: .space 4
atoiCuvant: .long 0
x: .space 4
y: .space 4
eval: .long 0
op: .space 4
add: .asciz "add"
sub: .asciz "sub"
mul: .asciz "mul"
div: .asciz "div"
.text
.global main
main:
pushl $sir
pushl $formatScanf
call scanf
popl %ebx
popl %ebx
pushl $delim
pushl $sir
call strtok
popl %ebx
popl %ebx
pushl %eax
call atoi
popl primulNumar
movl %eax, primulNumar
pushl primulNumar
et_loop:
pushl $delim
pushl $0
call strtok
popl %ebx
popl %ebx
cmp $0, %eax
je exit
mov %eax, cuvant
pushl %eax
call atoi
popl %ebx
mov %eax, atoiCuvant
cmp $0, atoiCuvant
je operatie
pushl %eax
jmp et_loop
operatie:
push $cuvant
push $add
call strcmp
popl %ebx
popl %ebx
cmp $0, %eax
je opadd
push $cuvant
push $sub
call strcmp
popl %ebx
popl %ebx
cmp $0, %eax
je opsub
push $cuvant
push $mul
call strcmp
popl %ebx
popl %ebx
cmp $0, %eax
je opmul
push $cuvant
push $div
call strcmp
popl %ebx
popl %ebx
cmp $0, %eax
je opdiv
opadd:
popl %edx
popl y
add %edx, y
push y
jmp et_loop
opmul:
popl %eax
popl %ebx
mul %ebx
pushl %eax
jmp et_loop
opdiv:
popl %eax
popl %ebx
div %ebx
pushl %eax
jmp et_loop
opsub:
popl %eax
popl y
sub %eax,y
pushl y
jmp et_loop
exit:
popl eval
pushl eval
pushl $formatPrintf
call printf
popl %ebx
popl %ebx
pushl $0
call fflush
popl %ebx
movl $1, %eax
xorl %ebx, %ebx
int $0x80
EDIT:
.data
formatPrintf: .asciz "%d"
sir: .space 1000
delim: .asciz " "
formatScanf: .asciz "%1000[^\n]"
cuvant: .space 100
primulNumar: .space 4
atoiCuvant: .space 4
x: .space 4
y: .long 4
eval: .space 4
op: .space 4
add: .asciz "a"
sub: .asciz "s"
mul: .asciz "m"
div: .asciz "d"
.text
.global main
main:
pushl $sir
pushl $formatScanf
call scanf
popl %ebx
popl %ebx
pushl $delim
pushl $sir
call strtok
popl %ebx
popl %ebx
pushl %eax
call atoi
popl %ebx
movl %eax, primulNumar
pushl primulNumar
et_loop:
pushl $delim
pushl $0
call strtok
popl %ebx
popl %ebx
cmp $0, %eax
je exit
mov %eax, %esi
pushl %eax
call atoi
popl %ebx
cmp $0, %eax
je operatie
pushl %eax
jmp et_loop
operatie:
mov $0, %ecx
movb (%esi,%ecx,1), %al
cmp $97,%al
je opadd
cmp $115, %al
je opsub
cmp $109, %al
je opmul
cmp $100, %al
je opdiv
opadd:
popl %ebx
popl %eax
add %ebx, %eax
pushl %eax
jmp et_loop
opmul:
popl %eax
popl %ebx
mul %ebx
pushl %eax
jmp et_loop
opdiv:
popl %ebx
popl %eax
xorl %edx, %edx
div %ebx
pushl %eax
jmp et_loop
opsub:
popl %eax
popl y
sub %eax,y
pushl y
jmp et_loop
exit:
popl eval
pushl eval
pushl $formatPrintf
call printf
popl %ebx
popl %ebx
pushl $0
call fflush
popl %ebx
movl $1, %eax
xorl %ebx, %ebx
int $0x80