developer
developer

Reputation: 53

I have an issue in assembly program emu 8086, when I print the sub result for two number

I Write an assembly program that makes the user enters 2 integers, stores them in a stack and then displays the sub of them:

and this my code:

.MODEL SMALL
.STACK 100H
.DATA 
  msg2 db 0dh,0ah, 'Result:   ',0dh, 0ah, '$' 
.CODE
MAIN PROC  
     mov ax,@data
     mov ds,ax
     mov cx, 2
      mov ah,1
TOP:
      int 21h 
      add al, 48h
      push ax
      LOOP TOP
      mov ah,9
      lea dx,msg2
      int 21h 
     XOR bx, bx
     mov cx,2
     add_:
      pop ax
      sub bx,ax
      LOOP add_
    mov ah, 2
    mov dl,bl
    sub dl,48h
    int 21h

but it's print like a character ! how can I print it with the right integer?

Note: If I add the two number, and but sub dl,30h it's print a correct integer, what the mistake?

Upvotes: 2

Views: 1234

Answers (2)

Fifoernik
Fifoernik

Reputation: 9899

  mov cx, 2
  mov ah,1
TOP:
  int 21h 
  add al, 48h
  push ax
  LOOP TOP

The single digit input that you get from DOS is an ASCII in the range [48,57]. You turn this into a number that you can further process by subtracting 48. That is 48 decimal or 30h hexadecimal!

  mov  cx, 2
  mov  ah, 01h
TOP:
  int  21h
  sub  al, 30h     ;From character "0", "1", ... to number 0, 1, ...
  push ax
  loop TOP

XOR bx, bx
 mov cx,2
add_:
 pop ax
 sub bx,ax
 LOOP add_
 mov ah, 2
 mov dl,bl
 sub dl,48h
 int 21h

When it comes to subtracting both numbers, that is not what your program does. You currently negate their sum, kind of!

Also both numbers are held in a single byte being the lowest 8 bits of AX, better known as AL. The subtraction must deal with this 8 bit part only.

 pop  dx        ; pop the 2nd inputted number
 pop  ax        ; pop the 1st inputted number
 sub  dl, al    ; calculate (2nd - 1st)
 mov  ah, 02h
 add  dl, 30h
 int  21h

To turn the result of the subtraction back into a displayable character, you add 48 decimal or 30h hexadecimal.


To obtain correct results (the program is not equipped to handle negative numbers), make sure that the 1st number that you input is smaller or equal to the 2nd number that you input. e.g. "4" then "9" which will output "5".

Upvotes: 3

Gustavo Topete
Gustavo Topete

Reputation: 1306

Well, you're using a really old and obsolete assembler hehehe, and it is not so flexible. Well, first of all, processor only knows about integers when talking about characters, so, if you want to print an integer it will be printed as it's char representation. Kowing this case, you have to route every digit of the sub result to it's correct character representation, for that you need an ASCII table (also, if I'm not wrong, that emu has a built in image of ASCII characters).

For example, if you do "6-3" the sub result is "3", so, to route it to the correct ASCII character (according to the given table) you have to add it 48 or 30h, so, 48+3 = 51, or 30h+3 = 33h, and that integer is the correct ASCII integer for "3" character. And if you have something like "23-3=20", then you have to do the same to every digit, it means, 0 + 30h = 30h, and 2+30h = 32h, so you'll have to print first the 32h character and then the 30h character, both corresponding to their correct and respective ASCII representation.

So, in a few words, according to the given table (it may be different for your emu), to correctly print the result, you have to add 30h to every digit and then print them.

Upvotes: 0

Related Questions