TASM Program to subtract Two 8 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 an
assembly language program for 8 bit subtraction.
Prerequisite:
TASM
assembler
Algorithm for 8 bit subtraction:
- Start
- Initialize data segment through AX register in
the DS register.
- Display the message as “Enter the first number”
- Read first digit in AL register through
keyboard (e.g. AL=31h)
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number.AL=01h
- Move contents of AL register to a BL. (BLß AL so BL=01h)
- Rotate the contents of BL register by 4
positions at left side. (BL=10h)
- 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
- Add the contents of BL and AL store the result
in BL (BLßBL+AL so BL=15h)
- Display the message as “Enter the second
number”
- Read first digit in AL register through
keyboard AL=32h
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number.AL=02h
- Move contents of AL register to a CL. (CLß AL so CL=02h)
- Rotate the contents of CL register by 4
positions at left side. (CL=20h)
- Read a second digit in AL register through
keyboard (AL=33h)
- Call Input procedure to make a number from
ASCII hexadecimal to a normal hexadecimal number. AL=03h
- Add the contents of CL and AL store the result
in CL (CLßCL+AL so CL=23h) (Now both
numbers are accepted as 15h and 23h)
- Subtract the contents of CL from BL and result
gets stored in BL (E.g SUB BL,CL so BL=F2h)
- Preserve the result in some temporary variable
say temp from BL.
- Mask the first nibble by AND operation with
number F0h (AND BL,F0h so BL=30h)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BL=33h)
- Move the contents of BL to DL and display it on
the screen
- Move result from temporary variable to BL again
(So BL=38h)
- Mask the second nibble by AND operation with
number 0Fh (AND BL,0Fh so BL=08h)
- Call Output procedure with BL register to make
a digit back in ASCII hexadecimal range (BL=38h)
- 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
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 BL register
4.
Add
30h to BL 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’.
Data segment msg db 0dh,0ah,"Enter first number: $" msg1 db 0dh,0ah,"Enter second number: $" result db 0dh,0ah,"The Result is: $" Data ends Code segment assume CS:Code,DS:Data start: mov ax,Data ; Move Data to Data Segment mov DS,ax mov dx,offset msg ; Display contents of variable msg sub8 mov ah,09h int 21h mov ah,01h ; To accept input and store ASCII value into al int 21h sub al,30h ; Accept 10's place of the Number mov bl,al rol bl,4 mov ah,01h ; To accept input and store ASCII value into al int 21h sub al,30h ; Accept unit's place of Number add bl,al ; Get the number by adding 10's and unit's place mov dx,offset msg1 ; Display contents of variable msg1 mov ah,09h int 21h mov ah,01h ; To accept input and store ASCII value into al int 21h sub al,30h ; Accept 10's place of the Number mov cl,al rol cl,4 mov ah,01h ; To accept input and store ASCII value into al int 21h sub al,30h ; Accept unit's place of Number add cl,al ; Get the number by adding 10's and unit's place sub bl,cl ; Subtract the two accepted Number's mov dx,offset result ; Display contents of string result mov ah,09h int 21h mov cl,bl ; Store the value of the Result and bl,0f0h ; Isolate 10's place of Result ror bl,4 call AsciiConv ; Convert to ASCII to display mov dl,bl ; Display a Number/Alphabet mov ah,02h int 21h mov bl,cl ; Retrieve original Result and bl,0fh ; Isolate unit's place of Result call AsciiConv ; Convert to ASCII to display mov dl,bl ; Display a Number/Alphabet mov ah,02h int 21h mov ah,4ch ; Terminate the program int 21h AsciiConv proc ; Compare to 0a if it is less than A then we need to add only 30 cmp bl,0ah ; If it is greater than or equal to 0a then we also need to add 07 jc skip add bl,07h skip: add bl,30h ret endp Code ends end startoutput:-
C:\TASM\BIN>tasm sub8.asm Turbo Assembler Version 4.1 Copyright (c) 1988, 1996 Borland International Assembling file: sub8.asm Error messages: None Warning messages: None Passes: 1 Remaining memory: 453k C:\TASM\BIN>tlink sub8.obj Turbo Link Version 7.1.30.1. Copyright (c) 1987, 1996 Borland International Warning: No stack C:\TASM\BIN>sub8 Enter first number: 13 Enter second number: 12 The Result is: 01 --------------------------------
0 comments: