3

need help, how to add two numbers and then print the result here is my code

    .MODEL SMALL
    .STACK 200H
    .DATA
NUM1 DB 12
NUM2 DB 3
VAL  DB ?
MSG1 DB "The sum is : $"

    .CODE
BEGIN PROC 
      MOV AX, @DATA
      MOV DS, AX

      MOV AL, NUM1
      ADD AL, NUM2
      MOV VAL, AL



      LEA DX, MSG1
      MOV AH, 9
      INT 21H


      MOV AH, 2
      MOV DL, VAL
      INT 21H

      MOV AX, 4C00H
      INT 21H
BEGIN ENDP
      END BEGIN

I got an output that says

The sum is 0 

What is the error to my code?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user3640979
  • 31
  • 1
  • 1
  • 2

3 Answers3

2

By dividing the value constantly by 10 you'll get the single digits in the remainder - but in the "wrong" order (last to first). To print it in the "right" order (first to last) you can reverse them by PUSHing and POPing (keyword: LIFO = last in first out):

.MODEL SMALL
.STACK 200H
.DATA
    NUM1 DB 12
    NUM2 DB 3
    VAL  DW ?
    MSG1 DB "The sum is : "
    DECIMAL  DB "00000$"

.CODE
BEGIN PROC
    MOV AX, @DATA
    MOV DS, AX

    XOR AX, AX
    MOV AL, NUM1
    ADD AL, NUM2
    ADC AH, 0
    MOV VAL, AX

    MOV AX, VAL
    CALL AX_to_DEC

    LEA DX, MSG1
    MOV AH, 9
    INT 21H

    MOV AX, 4C00H
    INT 21H
BEGIN ENDP

AX_to_DEC PROC
        mov bx, 10              ; divisor
        xor cx, cx              ; CX=0 (number of digits)

    First_Loop:
        xor dx, dx              ; Attention: DIV applies also DX!
        div bx                  ; DX:AX / BX = AX remainder: DX
        push dx                 ; LIFO
        inc cx                  ; increment number of digits
        test  ax, ax            ; AX = 0?
        jnz First_Loop          ; no: once more

        mov di, OFFSET DECIMAL  ; target string DECIMAL
    Second_Loop:
        pop ax                  ; get back pushed digit
        or ax, 00110000b        ; to ASCII
        mov byte ptr [di], al   ; save AL
        inc di                  ; DI points to next character in string DECIMAL
        loop Second_Loop        ; until there are no digits left

        mov byte ptr [di], '$'  ; End-of-string delimiter for INT 21 / FN 09h
        ret
AX_to_DEC ENDP

END BEGIN
rkhb
  • 14,159
  • 7
  • 32
  • 60
0

You cannot do it like this, You will not see the number, You must get the ascii representating hexvalue s of the digits, which represent Your result. Very basic basics. Let me look for a link.

Here it is: How to convert from 4-bit hexadecimal to 7-bit ASCII?

Community
  • 1
  • 1
icbytes
  • 1,831
  • 1
  • 17
  • 27
0

section .data num1 db 10 num2 db 20 result db 0

section .text global _start

_start: ; Move the values of num1 and num2 into registers mov al, byte [num1] mov bl, byte [num2]

; Add the numbers
add al, bl

; Move the result back to memory
mov byte [result], al

; Exit the program
mov eax, 1
xor ebx, ebx
int 0x80
ali
  • 1
  • 1
    This doesn't print the result. Also, it's a Linux program, not DOS like in the question. See [How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string)](https://stackoverflow.com/a/46301894) for how to do int -> string and make a write system-call in x86 Linux – Peter Cordes Jun 19 '23 at 06:33
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 30 '23 at 21:28