0

I'm new to GAS assembly here, my goal is to display the sum of the added two numbers that were entered by the user. I used char to number conversion and vice-versa and the results is always wrong (non-numeric). Can somebody tell me where's the mistake? Thanks in advance.

.section .data
prompt_str1:
    .ascii "Enter first number: "
str1_end:
    .set STR1_SIZE, str1_end-prompt_str1
prompt_str2:
    .ascii "Enter second number: "
str2_end:
    .set STR2_SIZE, str2_end-prompt_str2

.section .bss 
.lcomm      input1  2
.lcomm      input2  2
.lcomm      ans     1

.macro write str,str_size
    movl $4, %eax
    movl $1, %ebx
    movl \str, %ecx
    movl \str_size, %edx
    int $0x80
.endm

.macro writenum str
    movl $4, %eax
    movl $1, %ebx
    movl \str, %ecx
    int $0x80
.endm

.macro read buff, buff_size
    movl  $3, %eax
    movl  $0, %ebx
    movl  \buff, %ecx
    movl  \buff_size, %edx
    int   $0x80
.endm

.section .text
.globl _start

_start:
write $prompt_str1, $STR1_SIZE
read $input1, $2

write $prompt_str2, $STR2_SIZE
read $input2, $2

subl $0x30, input1
subl $0x30, input2
movl $input1, %eax
addl $input2, %eax
movl %eax, ans
addl $0x30, ans
writenum $ans

exit:
movl $1, %eax
movl $0, %ebx
int $0x80
JWWalker
  • 22,385
  • 6
  • 55
  • 76
oLraX
  • 217
  • 4
  • 14

1 Answers1

1

movl $input1, %eax
addl $input2, %eax

You're moving the address of input1 into %eax and adding the address of input2. Lose the $!

There's probably more wrong with this code. Since you're only able to handle single digits (including sum!), you should probably be using bytes here, not "longs", but just dumping the $ gets you a numeric result at least.

This distinction between an address and "contents at that address" is a really important one! Unfortunately, every assembler does it differently... sigh...

Frank Kotler
  • 1,462
  • 1
  • 8
  • 4