TASM Program to divide 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 division.
Prerequisite:
TASM assembler
DIV instruction-
       Divides unsigned word stored in AX by specified by byte(16/8).
       Syntax-
                                    DIV source
       DIV BL      (16 bit/8 bit)
                                    AX (AH and AL)=AX/BL
                                    AH will contain remainder and Al will contain quotient.
DIV BX      (16 bit /16 bit)
DX: AX=AX/BX                                
DX will contain remainder and AL will contain quotient


Algorithm for 8 bit Division:
1.      Start
2.      Initialize data segment through AX register in the DS register.
3.      Display the message as “Enter the Dividend”
4.      Read first digit in AL register through keyboard    (e.g. AL=31h)
5.      Call Input procedure to make a number from ASCII hexadecimal to a normal hexadecimal number.AL=01h
6.      Move contents of AL register to a BL.    (BLß AL  so BL=01h)
7.      Rotate the contents of BL register by 4 positions at left side. (BL=10h)
8.      Read a second digit in AL register through keyboard AL=32h
9.      Call Input procedure to make a number from ASCII hexadecimal to a normal hexadecimal number. AL=02h
10.  Add the contents of BL and AL store the result in BL   (BLßBL+AL so BL=12h)
11.  Initialize BH with 00h (So BX=0012h)
12.  Display the message as “Enter the Divisor”
13.  Read first digit in AL register through keyboard(AL=30h)
14.  Call Input procedure to make a number from ASCII hexadecimal to a normal hexadecimal number.AL=00h
15.  Move contents of AL register to a CL.    (CLß AL  so CL=00h)
16.  Rotate the contents of CL register by 4 positions at left side. (CL=00h)
17.  Read a second digit in AL register through keyboard (AL=34h)
18.  Call Input procedure to make a number from ASCII hexadecimal to a normal hexadecimal number. AL=04h
19.  Add the contents of CL and AL store the result in CL   (CLßCL+AL so CL=04h) (Now both numbers are accepted as 0012h and 04h)
20.  Move the contents of  BX to AX (i.e Dividend must be in accumulator)
21.  Divide  the contents of AX by CL and result gets stored in AX (E.g DIV CL  (AX/CL)so AX=0003h  that is AH=remainder as 00 and AL=quotient as 03 )
22.  Preserve the result in some temporary variable say temp of 16 bit from AX.
23.  Display text message as “Remainder is=”
24.  Mask the first nibble by AND operation with number F000h (AND AX,F000h so AX=0000h)
25.  Rotate the AX contents right by 12(in decimal)
26.  Call Output procedure with AL register to make a digit back in ASCII hexadecimal range (AX=0030h)
27.  Move the contents of AL to DL and display it on the screen
28.  Move result from temporary variable to AX again (Now AX=0003h)
29.  Mask the second nibble by AND operation with number 0F00h (AND AX,0F00h so AX=0000h)
30.  Rotate the contents of AX to right by 8(in decimal)
31.  Call Output procedure with AL register to make a digit back in ASCII hexadecimal range (AX=0030h)
32.  Move the contents of AL to DL and display it on the screen.
33.  Display text message as “Quotient is=”
34.  Move result from temporary variable to AX again (Now AX=0003h)
35.  Mask the third nibble by AND operation with number 00F0h (AND AX,00F0h so AX=0000h)
36.  Rotate the contents of AX to right by 4(in decimal)
37.  Call Output procedure with AL register to make a digit back in ASCII hexadecimal range      (AX=0030h)
38.  Move the contents of AL to DL and display it on the screen
39.  Move result back from temporary variable to AX again (Now AX=0003h)
40.  Mask the fourth nibble by AND operation with number 000Fh (AND AX,000Fh so AX=0003h)
41.  Call Output procedure with AL register to make a digit back in ASCII hexadecimal range (AX=0033h)
42.  Move the contents of AL to DL and display it on the screen.
43.  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  AL 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 Dividend: $"
 msg1 db 0dh,0ah,"Enter Divisor: $"
 resq db 0dh,0ah,"The Quotient is: $"
 resr db 0dh,0ah,"The Remainder 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 div8
 mov ah,09h
 int 21h

 mov ah,01h ; To accept input and store ASCII value into al
 int 21h

 call AsciitoHex

 mov bl,al ; Accept 10's place of the Number
 rol bl,4

 mov ah,01h ; To accept input and store ASCII value into al
 int 21h

 call AsciitoHex ; 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

 call AsciitoHex ; 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

 call AsciitoHex ; Accept unit's place of Number

 add cl,al ; Get the number by adding 10's and unit's place

 and ax,0000h
 mov al,bl ; Divide the two accepted Number's
 mov bl,cl
 div bl

 mov cx,ax ; Store the value of the Result

 mov dx,offset resq ; Display contents of string resq
 mov ah,09h
 int 21h

 mov ax,cx ; Retrieve the Result
 
 and al,0f0h ; Isolate 10's place of the Quotient
 ror al,4

 mov bl,al ; Convert to ASCII to display
 call AsciiConv 

 mov dl,bl ; Display a Number/Alphabet
 mov ah,02h
 int 21h

 mov ax,cx ; Retrieve original Result

 and al,0fh ; Isolate unit's place of Result

 mov bl,al ; Convert to ASCII to display
 call AsciiConv 

 mov dl,bl ; Display a Number/Alphabet
 mov ah,02h
 int 21h

 mov dx,offset resr ; Display contents of string resr
 mov ah,09h
 int 21h
 
 mov ax,cx ; Retrieve the Result

 and ah,0f0h ; Isolate 10's place of the Quotient
 ror ah,4

 mov bl,ah ; Convert to ASCII to display
 call AsciiConv 

 mov dl,bl ; Display a Number/Alphabet
 mov ah,02h
 int 21h

 mov ax,cx ; Retrieve original Result

 and ah,0fh ; Isolate unit's place of Result

 mov bl,ah ; Convert to ASCII to display
 call AsciiConv 

 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

 AsciitoHex proc ; Compare to 41 if it is less than A then we need to sub only 30
  cmp al,41h ; If it is greater than or equal to 41 then we also need to sub 07
  jc skippy
  sub al,07h
  skippy: sub al,30h
  ret
  endp
Code ends
end start
output:-
C:\Documents and Settings\admin>cd c:\tasm\bin

C:\TASM\BIN>tasm div8.asm
Turbo Assembler  Version 4.1  Copyright (c) 1988, 1996 Borland International

Assembling file:   div8.asm
Error messages:    None
Warning messages:  None
Passes:            1
Remaining memory:  452k


C:\TASM\BIN>tlink div8.obj
Turbo Link  Version 7.1.30.1. Copyright (c) 1987, 1996 Borland International
Warning: No stack

C:\TASM\BIN>div8

Enter Dividend: 0C
Enter Divisor: 03
The Quotient is: 04
The Remainder is: 00
--------------------------------

0 comments: