Search code examples
assemblydosprocedurex86-16

Solve the exercise of adding two numbers in assembly


I want your help to solve this machine language exercise.

Write a program in assembly language in which a procedure is defined that has two parameters and calculates the sum of the first parameter and the second parameter and puts it in the second parameter. Use this procedure to write a program that receives two one-digit numbers from the keyboard and displays the sum of the two. Suppose the sum of two numbers becomes one digit.


my code :

nextline macro
    mov ah,2
    mov dl,13
    int 21h
    mov dl,10
    int 21h
endm
.MODEL SMALL
 .STACK 64

 .DATA
   msg1  DB  'Enter the character : $'
   msg2  DB  'The next character is : $'
   data1  dw  ?

 .CODE
   MAIN:
     MOV AX, @DATA             
     MOV DS, AX

     LEA DX, msg1           
     MOV AH, 9
     INT 21H

     MOV AH, 1                
     INT 21H
     mov data1,ax 
     
     push data1
     
     CALL NEXT_CHAR
     
     pop data1
                   
     nextline
     LEA DX, msg2          
     MOV AH, 9
     INT 21H

     MOV AH, 2                  
     MOV Dx, data1
     INT 21H

     MOV AH, 4CH                 
     INT 21H




 NEXT_CHAR PROC

    mov bp,sp
    push bx
                
    MOV Bx, [bp+2]
    INC BL
    mov [bp+2],bx 
    
    pop bx
    RET           
 NEXT_CHAR ENDP

END MAIN

Solution

  • You didn't understand the task description.

    a procedure is defined that has two parameters and calculates the sum of the first parameter and the second parameter and puts it in the second parameter.

    two Obviously your current procedure only uses 1 parameter
    puts it in the second parameter For this to happen you need to pass the second parameter by reference (not by value like for the first parameter)

    write a program that receives two one-digit numbers from the keyboard

    two one-digit numbers from the keyboard This means that you need two instances of that call to the DOS.GetCharacter function 01h, not just the one you have now.

    and displays the sum of the two. Suppose the sum of two numbers becomes one digit.

    Suppose the sum of two numbers becomes one digit This will simpify your task enormously. To convert from number [0,9] to character ["0","9"] you simply add 48, and then display that character with DOS.PrintChar function 02h for which you supply the character in DL and not in DX like you did!


    I'm reluctant to provide more code because I believe that you probably just didn't understand the task very well. Give it another try. You can always post another question with your next efforts...