0

I made a simple adding calculator in assembly 8086 that when you type an input on your keyboard it adds them up. I can add something like 2+2 and get the right answer but, when I add 9+9 it outputs B

.model small

.data
    message db "Enter a number: $" 
    message2 db 0xA, 0xD, "Enter another number: $" ; 0xA = new line, 0xD = carriage return, 0x24 = '$' 
    plus db " + $"
    equal db " = $"
.code
main proc  
    
    mov ax, seg message ; ax = message
    mov ds, ax          ; dx = ax = message
    mov dx, offset message 
    mov ah, 9h          ; output of a string at DS:DX
    int 21h   
    
    mov ah, 01h ; Read character (stores in al)
    int 21h
    
    mov bl, al  ; saving our input into bl
    
    mov ax, seg message
    mov ds, ax          ; dx = ax = message2
    mov ds, ax          ; dx = ax = message2
    mov dx, offset message2 
    mov ah, 9h          ; output of a string at DS:DX
    int 21h 
    
    mov ah, 1h ; Read character 
    int 21h
    
    mov cl, al ; stores 2nd input 
      
    ;mov dl, bl ; print 1st input
    ;mov ah, 02h ; Write character
    ;int 21h
    
    ; print + 
    mov ax, seg plus
    mov ds, ax
    mov dx, offset plus
    mov ah, 9h
    int 21h
    
    ; print 2nd number
    mov dl, cl
    mov ah, 02h
    int 21h  
    
    ; print =
    mov ax, seg equal
    mov ds, ax
    mov dx, offset equal
    mov ah, 9h
    int 21h
    
    ; This is were its having problems adding
    ; print sum
    sub bl, 48
    sub cl, 48
    add bl, cl
    mov dl, bl
    add dl, 48
    mov ah, 02h
    int 21h
    
    
endp
end main

Ive tried debugging it to see what is was doing, it was doing everything right but when it got to the adding part itll say 0x12 instead of 0x9 (when doing 9+9)

  • 1
    0x12 = 18 decimal is correct for 9+9. It can't be printed as a single-digit number in base 10 or base 16, so one `int 21h` / `ah=02` isn't going to work. Also, your title is wrong, you got `0x12` not `12`. – Peter Cordes Jun 16 '23 at 20:14
  • Here's a hint: `DAA` will help you do this task. – puppydrum64 Jul 20 '23 at 15:38

1 Answers1

0

It's hexadecimal. 12 in Hexadecimal = 18 in decimal:

hex: 0 1 2 3 4 5 6 7 8 9  a  b  c  d  e  f 10 11 12
dec: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Matt
  • 20,108
  • 1
  • 57
  • 70