Learn how Summation of Two Numbers in Assembly works, how assembly program takes two numbers as input and displays their single or double-digit sum, illustrating fundamental assembly input/output operations.
This program is designed to get two single-digit numbers from the user, add them together, and then show the single-digit sum on the screen.
- Memory Setup: The
.DATA
section reserves space in memory for the messages that will prompt the user for input (MSG1
,MSG2
), the message indicating the result (SUM
), and single-byte storage locations for the first number (N1
), the second number (N2
), and the result of the addition (R
). - Data Segment Initialization: The
.CODE
section starts withMAIN PROC
, the beginning of the main part of the program. The linesMOV AX, @DATA
andMOV DS, AX
set up the Data Segment register (DS
) so the program can correctly access the messages and variables defined in the.DATA
section. - First Input:
LEA DX, MSG1
: The program finds the memory address of the “ENTER FIRST NUMBER:$” message and prepares to display it.MOV AH, 09H
andINT 21H
: These instructions call the DOS function to display the string pointed to byDX
.MOV AH, 1
andINT 21H
: These instructions call the DOS function to read a single character typed by the user. The ASCII code of the character is stored in theAL
register.SUB AL, 48
: This line converts the ASCII code of the digit (e.g., ‘5’ has ASCII 53) into its actual numerical value (e.g., 5).MOV N1, AL
: The numerical value of the first digit is stored in theN1
memory location.
- Second Input: This section repeats the process of the first input, but prompts the user with “ENTER SECOND NUMBER:$” (
MSG2
) and stores the numerical value of the second digit in theN2
memory location. - Calculation:
MOV AL, N1
: The numerical value of the first number is loaded into theAL
register.ADD AL, N2
: The numerical value of the second number is added to the value inAL
. The sum (which should be a single digit or a two-digit number less than 19 given single-digit inputs) is now inAL
.MOV R, AL
: The sum is stored in theR
memory location.MOV AH, 0
: The upper byte of theAX
register (AH
) is cleared, often done as a clean-up or preparation step.
- Preparing for Displaying the Sum:
AAA
: The “ASCII Adjust for Addition” instruction is used. For single-digit addition, if the result inAL
is greater than 9,AAA
will adjustAL
to contain the correct BCD digit (0-9) and put the carry (0 or 1) intoAH
.ADD AH, 48
: The value inAH
(the potential carry) is converted to its ASCII character representation (‘0’ or ‘1’).ADD AL, 48
: The value inAL
(the units digit of the sum) is converted to its ASCII character representation (‘0’-‘9’).MOV BX, AX
: The entireAX
register, now containing the ASCII codes of the (potentially) two digits of the sum, is copied into theBX
register.BH
will hold the ASCII of the carry (if any), andBL
will hold the ASCII of the units digit.
- Displaying the Result:
LEA DX, SUM
: The program prepares to display the “SUMMATION IS :$” message.MOV AH, 9
andINT 21h
: The “SUMMATION IS :$” message is displayed.MOV AH, 2
,MOV DL, BH
, andINT 21H
: The ASCII character inBH
(the potential carry) is displayed.MOV AH, 2
,MOV DL, BL
, andINT 21h
: The ASCII character inBL
(the units digit of the sum) is displayed.
- Program Termination:
MOV AH, 4CH
andINT 21H
: These instructions terminate the program and return control to the operating system.
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 10,13,"ENTER 1ST NUMBER:$"
MSG2 DB 0AH,0DH,"ENTER 2ND NUMBER:$"
SUM DB 10,13,"SUMMATION IS:$"
N1 DB ?
N2 DB ?
R DB ?
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1 ; Point to your message
MOV AH,09H ; Say "Show text!".
INT 21H ; Tell DOS to do it.
MOV AH,1 ; Say "Wait for keyboard input!".
INT 21H ; DOS waits, you type, the ASCII code goes to AL.
SUB AL,48 ; Convert ASCII to number
MOV N1,AL ; Store that number in the "box" named N1
LEA DX,MSG2
MOV AH,09H
INT 21H
MOV AH,1
INT 21H
SUB AL,48 ; Convert ASCII to number
MOV N2,AL
MOV AL,N1 ; Put the first number into our temporary calculation box (AL).
ADD AL,N2
MOV R,AL
MOV AH,0 ; FOR REGISTER CLEARANCE
AAA ; A special "fixer" for addition results (mostly for a specific way of storing numbers we haven't directly used).
ADD AH,48 ; Convert any carry to its displayable character.
ADD AL,48 ; Convert the sum's digit to its displayable character.
MOV BX,AX ; (Maybe) keep a copy of the displayable result.
LEA DX,SUM ; We tell DOS where in memory our message (SUM) is located.
MOV AH,9 ; We tell DOS what we want to do (display a string).
INT 21H ; We tell DOS to actually do it, using the information we provided in DX and AH.
; MOV AH, 2
; MOV: "Move" or "copy".
; AH: The top half of the AX storage box.
; 2: A number. This is the DOS function code for displaying a single character on the screen.
; Think: "Put the number 2 into the AH box. This tells DOS we want to show one character."
; MOV DL, BH
; MOV: "Move" or "copy".
; DL: A small storage box (the lower half of the DX register). DOS often uses DL to hold the character we want to display.
; BH: Another small storage box (the top half of the BX register). We're taking whatever character code (likely an ASCII value) is currently in BH.
; Think: "Take the character code that's in the BH box and put a copy of it into the DL box. This is the character we want to show."
; INT 21H
; INT 21H: "Ring the DOS bell!" We're asking DOS to perform the function we specified in AH (display a character), using the information in DL (the character to display).
; Think: "Hey DOS, I want to display a character (because AH is 2), and here's the character to display (it's in DL). Please show it on the screen!"
MOV AH,2
MOV DL,BH
INT 21H
MOV AH,2
MOV DL,BL
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN