Search code examples
assemblyx86

Assembly addition program


i have the following code

section .bss
    num1 resb 2
    num2 resb 2
    res1 resb 2
    res2 resb 2

section .data
    fir dw 'Ingrese el primer numero: '
    lfir equ $ - fir
    sec dw 'Ingrese el segundo numero: '
    lsec equ $ - sec

    newline db 10


section .text
    global _start

_start:
    mov dl,lfir     ;print "Ingrese el primer numero: "
    mov ecx,fir
    mov bl,1
    mov al,4
    int 0x80

    mov dl,5        ;read the first number
    mov ecx,num1
    mov bl,2
    mov al,3
    int 0x80

    mov dl,lsec     ;print "Ingrese el segundo numero: "
    mov ecx,sec
    mov bl,1
    mov al,4
    int 0x80

    mov dl,5        ;read the second number
    mov ecx,num2
    mov bl,2
    mov al,3
    int 0x80

    mov eax,[num1]  ;move the first number into eax
    mov ebx,[num2]  ;move the second number to ebx
    sub ebx,48      ;conver the number to decimal
    add eax,ebx     ;add the 2 numbers
    mov [res1],eax  ;move the result into "res1"

    cmp eax, 57 ;check if the result has 2 digits
    jle singledigit ;instructions when it has 2 digits

    mov eax,[res1]
    sub eax,10
    mov [res1],eax
    mov ebx,'1'
    mov [res2],ebx

    mov dl,2        ;print res2
    mov ecx,res2
    mov ebx,1
    mov eax,4
    int 0x80

    mov dl,2        ;print res1
    mov ecx,res1
    mov ebx,1
    mov eax,4
    int 0x80

    jmp finish

singledigit:
    mov dl,2        ;print res1
    mov ecx,res1
    mov ebx,1
    mov eax,4
    int 0x80

    jmp finish

finish:
    mov dl,1        ;new line
    mov ecx,newline
    mov ebx,1
    mov eax,4
    int 0x80

    mov al,1
    int 0x80

It seems like it will always ignore the singledigit label, and therefore prints ascii characters below 48 (ascii 0) when the sum is less than 10, but it works fine when the sum is greater than 10


Solution

  • Just like Michael said, replacing every

    mov eax,[num1] with
    
    movzx eax,byte [num1]; 
    

    and the same for ebx and num2, did solve the issue