Friday, 2 March 2018

How to add two numbers in Assembly Language?

; Addition of two numbers

.MODEL SMALL
.STACK 100H   
.DATA
    MSG1 DB 'Enter a: $'
    MSG2 DB 0AH, 0DH, 'Enter b: $'                     ; variable declaration
    MSG3 DB 0AH, 0DH, 'Sum of a and b is: $'
    RESULT DB ?
.CODE
MAIN PROC  ;PROC = Procedure
    MOV AX, @DATA    ; initialize data segment
    MOV DS, AX
   
    LEA DX, MSG1    ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9       ; Output a String
    INT 21H
   
   
    MOV AH, 1   ; INPUT FUNCTION
    INT 21H     ; INT = Interupt
    MOV BL, AL
   
    MOV BH, AL 
   
    LEA DX, MSG2    ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9       ; Output a String
    INT 21H
   
   
    MOV AH, 1   ; INPUT FUNCTION
    INT 21H     ; INT = Interupt
    ADD AL, BL
   
    MOV RESULT, AL
    SUB RESULT, 30H
   
    LEA DX, MSG3    ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9       ; Output a String
    INT 21H
   
    MOV AH, 2   ; OUTPUT FUNCTION
    MOV DL, RESULT 
    INT 21H 
   
   
   EXIT:
   MOV AH, 4CH
   INT 21H
   MAIN ENDP
END MAIN

No comments:

Post a Comment