TASM Program to Add Two 16 Bit Numbers
On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
Objective:
Write a
menu based assembly language program to add 2 16 bit numbers.
Prerequisite:
TASM
assembler
Algorithm
- Start
- Initialize data segment through AX register in
the DS register.
- Display the 3 text message as “1. 16 bit
addition 2 .16 bit subtraction 3. Exit Enter your choice”
- Compare accepted choice with 03h.
- If zero flag is set then goto step no.6 otherwise goto step no 7
- Exit the program
- Display the message as “Enter first 16 bit
number”
- Read first digit in AL register through
keyboard (e.g. AL=32h)
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number.AL=02h
- Move contents of AH with 00h. (AHß 00h so AX=0002h)
- Rotate AX contents in left directions by 12
bits. (AX=2000h)
- Move the contents of AX to BX(BXßAX so BX=2000h)
- Read a
second digit in AL register through keyboard AL=35h
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number. AL=05h
- Move contents of AH with 00h. (AHß 00h so AX=0005h)
- Rotate AX contents in left directions by 8 bits.
(AX=0500h)
- Add the contents of AX and BX (BX=BX+AX so BX=2500h)
- Read a third digit in AL register through
keyboard AL=31h
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number. AL=01h
- Move contents of AH with 00h. (AHß 00h so AX=0001h)
- Rotate AX contents in left directions by 4 bits.
(AX=0010h)
- Add the contents of AX and BX (BX=BX+AX so BX=2510h)
- Read a fourth digit in AL register through
keyboard AL=30h
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number. AL=00h
- Move contents of AH with 00h. (AHß 00h so AX=0000h)
- Add the contents of AX and BX (BX=BX+AX so BX=2510h)
- Display the message as “Enter second 16 bit
number”
- Read first digit in AL register through
keyboard (e.g. AL=37h)
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number.AL=07h
- Move contents of AH with 00h. (AHß 00h so AX=0007h)
- Rotate AX contents in left directions by 12
bits. (AX=7000h)
- Move the contents of AX to CX(CXßAX so CX=7000h)
- Read a
second digit in AL register through keyboard AL=35h
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number. AL=05h
- Move contents of AH with 00h. (AHß 00h so AX=0005h)
- Rotate AX contents in left directions by 8 bits.
(AX=0500h)
- Add the contents of AX and CX (CX=CX+AX so CX=7500h)
- Read a third digit in AL register through
keyboard AL=31h
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number. AL=01h
- Move contents of AH with 00h. (AHß 00h so AX=0001h)
- Rotate AX contents in left directions by 4 bits.
(AX=0010h)
- Add the contents of AX and CX (CX=CX+AX so CX=7510h)
- Read a fourth digit in AL register through
keyboard AL=34h
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number. AL=04h
- Move contents of AH with 00h. (AHß 00h so AX=0004h)
- Add the contents of AX and BX (CX=CX+AX so CX=7514h)
- Compare accepted choice from AL with 02h
- If zero flag is set then goto step no 69 otherwise goto step no 49
- Add the contents of BX and CX(BX=BX+CX so BX=9A24h)
- Preserve the result in temporary variable as t
of 16 bit (so t=9A24h)
- Mask the first nibble by AND operation with
number F000h (AND BX,f000h so BX=9000h)
- Rotate the BX contents right by 12(in decimal
so BX=0009h)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BX=0039h)
- Move the contents of BL to DL and display it on
the screen
- Move result from temporary variable t to BX
again (Now BX=9A24h)
- Mask the second nibble by AND operation with
number 0F00h (AND BX,0F00h so BX=0A00h)
- Rotate the contents of BX to right by 8(in decimal)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BX=0041h(ASCII hex value of ‘A’))
- Move the contents of BL to DL and display it on
the screen.
- Move result from temporary variable to BX again
(Now AX=9A24h)
- Mask the third nibble by AND operation with
number 00F0h (AND BX,00F0h so BX=0020h)
- Rotate the contents of BX to right by 4(in
decimal)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BX=00032h)
- Move the contents of BL to DL and display it on
the screen
- Move result back from temporary variable to BX
again (Now BX=9A24h)
- Mask the fourth nibble by AND operation with
number 000Fh (AND BX,000fh so BX=0004h)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BX=0004h)
- Move the contents of BL to DL and display it on
the screen.
- Subtract
the contents of CX from BX(BXßBX-CX so BX=AFFCh)
- Preserve the result in temporary variable as t
of 16 bit (so t=AFFCh)
- Mask the first nibble by AND operation with
number F000h (AND BX,F000h so BX=A000h)
- Rotate the BX contents right by 12(in decimal
so BX=000Ah)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BX=0041h (i.e. ASCII hex value for ‘A’ ))
- Move the contents of BL to DL and display it on
the screen
- Move result from temporary variable t to BX
again (Now BX=AFFCh)
- Mask the second nibble by AND operation with
number 0f00h (AND BX,0F00h so BX=0F00h)
- Rotate the contents of BX to right by 8(in decimal)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BX=0046h(i.e ASCII hex value of ‘F’))
- Move the contents of BL to DL and display it on
the screen.
- Move result from temporary variable to BX again
(Now BX=AFFCh)
- Mask the third nibble by AND operation with
number 00F0h (AND BX,00F0h so BX=00F0h)
- Rotate the contents of BX to right by 4(in
decimal)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BX=0046h (i.e ASCII hex value of
‘F’))
- Move the contents of BL to DL and display it on
the screen
- Move result back from temporary variable to BX
again (Now BX=AFFCh)
- Mask the fourth nibble by AND operation with
number 000fh (AND AX,000Fh so AX=000Ch)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BX=0043h(i.e ASCII hex value of
‘C))
- Move the contents of BL to DL and display it on
the screen.
- Stop
Algorithm
for Input procedure :( To accept input from 0 to f)
1.
Compare
the contents of AL with 41h(Small case)
2.
Jump
to step no 4 if carry flag is set
3.
Sub 07h to AL register
4.
Sub
30h to AL register
5.
Return.
Algorithm
for Output procedure:
1.
Compare
the contents of BL with 0Ah
2.
Jump
to step no 4 if carry flag is set
3.
Add 07h to AL register
4.
Add
30h to AL register
5.
Return.
Note:
While
masking F or f is not case sensitive. But in input procedure 41h number is
considered for comparison because 41h is ASCII hex value for ‘A’. In output
procedure ‘0A’ is considered not ‘a’ is considered as small case a has 61h
ASCII hex value.So this input and output procedure are applicable for only
capital ‘A’ to ‘F’
TASM Program :Data Segment msg db 0dh,0ah,"Enter a 16-bit number: $" result db 0dh,0ah,"The Result is: $" newl db 0dh,0ah," $" Data ends Code Segment assume CS:Code,DS:Data Start: mov ax,Data mov DS,ax mov dx,offset msg;add16 mov ah,09h int 21h call AcceptNum mov bh,bl call AcceptNum mov cx,bx mov dx,offset msg mov ah,09h int 21h call AcceptNum mov bh,bl call AcceptNum add cx,bx mov dx,offset result mov ah,09h int 21h mov bl,ch call DispNum mov bl,cl call DispNum mov ah,4ch int 21h AcceptNum proc mov ah,01h int 21h call HexAccept mov bl,al rol bl,4 mov ah,01h int 21h call HexAccept add bl,al ret endp DispNum proc mov al,bl and al,0f0h ror al,4 mov dl,al call HexDisp mov ah,02h int 21h mov al,bl and al,0fh mov dl,al call HexDisp mov ah,02h int 21h endp HexAccept proc cmp al,41h jc norm sub al,07h norm: sub al,30h ret endp HexDisp proc cmp dl,0ah jc nothex add dl,07h nothex: add dl,30h ret endp Code ends end Startoutput:-
C:\TASM\BIN>tasm add16.asm Turbo Assembler Version 4.1 Copyright (c) 1988, 1996 Borland International Assembling file: add16.asm Error messages: None Warning messages: None Passes: 1 Remaining memory: 453k C:\TASM\BIN>tlink add16.obj Turbo Link Version 7.1.30.1. Copyright (c) 1987, 1996 Borland International Warning: No stack C:\TASM\BIN>add16 Enter a 16-bit number: 1312 Enter a 16-bit number: 1205 The Result is: 2517 --------------------------------
0 comments: