Reputation: 89
I wrote a program which ask to insert a value: if the value is less or equal to 100 the program exit without errors; else it prints an error message and exit. Unfortunatly,it never prints the error message when I insert a value greater than 100 so maybe there's some problems in the conversion of input sting but I don't understand where! Here's the code:
.section .data
str:
.ascii "Insert value:\n"
str_len:
.long . - str
err:
.ascii "Error\n"
err_len:
.long . - err
car:
.byte 0
.section .text
.global _start
_start:
movl $4, %eax #print string with sys read
movl $1, %ebx
leal str, %ecx
movl str_len, %edx
int $0x80
xorl %eax, %eax
again:
pushl %eax #save eax on stack
movl $3, %eax # read from keyboard with sys write
xorl %ebx, %ebx
leal car, %ecx
mov $1, %edx # read 1 char at the time
int $0x80
cmp $10, car # if it reads '\n' then end
je end
subb $48, car # else it converts ASCII code of char to correspondent values
popl %eax
movl $10, %ebx
mull %ebx # eax = eax * 10 + car
addl car, %eax
jmp again
end:
popl %eax
cmpl $100, %eax
jg error
error:
movl $4, %eax #print error message
movl $1, %ebx
leal err, %ecx
movl err_len, %edx
exit:
movl $1, %eax
xorl %ebx, %ebx
int $0x80
Upvotes: 1
Views: 366
Reputation: 58427
There are a number of problems in your code:
This instruction will result in a 32-bit comparison (cmpl
):
cmp $10, car
So you need to specify that you want an 8-bit comparison by using cmpb
.
In the error
part of your program you've forgot the int $0x80
, so it won't print anything.
You've got an jg error
, but then the error:
label is on the very next line, so you'll end up there regardless of whether that jump is taken or not. That jump should be changed to jng exit
.
Upvotes: 2