Wednesday, May 5, 2010

A Program for Multiplication of 2 Numbers


Home|Assembly Language Programming|
This program multiplies two digits supplied by the user as input. Then it displays the product as two decimal digit number. All the explanations of the instructions written here are supplied as comments.


data SEGMENT
msg1 DB 'Enter a number: $'
msg2 DB 0ah,0dh,'Enter another number: $'
msg3 DB 0ah,0dh,'The product is: $'
n1 DB ?
n2 DB ?
result DB ?
ten DB 10
ENDS

code SEGMENT

start:
MOV AX,data ;Initializes data segment
MOV DS,AX

print MACRO msg ;Defines a macro 'print'
MOV AH,09h ;Prints the string whose starting
MOV DX,OFFSET msg ;offset is stored in DX
INT 21h
ENDM
print msg1 ;Prints message and
MOV AH,01h ;Gets the first digit
INT 21h
SUB AL,'0' ;Derives the true binary value

MOV n1,AL ;Stores the value into variable n1
print msg2 ;Prints message and
MOV AH,01h ;Gets the second digit
INT 21h

SUB AL,'0' ;Calculates the true binary value
MUL n1 ;Multiply it with n1
MOV result,AL

print msg3
MOV AL,result ;Brings result in AL
MOV AH,0 ;Clears AH

DIV ten ;If the result is not valid BCD
MOV DL,AL ;divide the result by 10 to get
MOV CL,AL ;the first digit and stores it in DL and CL

ADD DL,'0' ;Get the ASCII code
MOV AH,02h
INT 21h ;Prints the first digit of product

MOV AL,CL ;Mov the first digit to AL
MUL ten ;Multiply it by 10
SUB result,AL ;get the second digit

XCHG DL,result ;stores second digit in DL
ADD DL,'0' ;Get the ASCII value
MOV AH,02h
INT 21h ;Prints the character

xit:
MOV AH,4ch ;Exits to the
INT 21h ;Operating System
ENDS
END start


Here is the output of this program, the first one is for single digit product and the second one is for 2 digit products.







12

0 comments:

Post a Comment