TASM Program to MultiplyTwo 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 multiplication.
Prerequisite:
TASM assembler
MUL instruction-
       Multiplies unsigned byte/word from source by  unsigned byte/word from AL/AX (multiplier) reg.
       Syntax-
                                    MUL source
       Result of 8bit X 8 bit will go in AX register and 16 bit X 16 bit will go in DX:AX register.
Algorithm for 8 bit Multiplication:
  1. Start
  2. Initialize data segment through AX register in the DS register.
  3. Display the message as “Enter the Multiplicand”
  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=35h
  9. Call Input procedure to make a number from ASCII hexadecimal to a normal hexadecimal number. AL=05h
  10. Add the contents of BL and AL store the result in BL   (BLßBL+AL so BL=15h)
  11. Display the message as “Enter the Multiplier”
  12. Read first digit in AL register through keyboard          AL=32h
  13. Call Input procedure to make a number from ASCII hexadecimal to a normal hexadecimal number.AL=02h
  14. Move contents of AL register to a CL.    (CLß AL  so CL=02h)
  15. Rotate the contents of CL register by 4 positions at left side. (CL=20h)
  16. Read a second digit in AL register through keyboard (AL=33h)
  17. Call Input procedure to make a number from ASCII hexadecimal to a normal hexadecimal number. AL=03h
  18. 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)
  19. Move the contents of BL to AL(i.e Multiplicand must be in accumulator)
  20. Multiply the contents of AL and CL and result gets stored in BL (E.g MUL CL so AX=02DFh)
  21. Preserve the result in some temporary variable say temp of 16 bit from AX.
  22. Mask the first nibble by AND operation with number F000h (AND AX,F000h so AX=0000h)
  23. Rotate the AX contents right by 12(in decimal)
  24. Call Output procedure with AL register to make a digit back in ASCII hexadecimal range (AX=0030h)
  25. Move the contents of AL to DL and display it on the screen
  26. Move result from temporary variable to AX again (Now AX=02DFh)
  27. Mask the second nibble by AND operation with number 0f00h (AND AX,0F00h so AX=0200h)
  28. Rotate the contents of AX to right by 8(in decimal)
  29. Call Output procedure with AL register to make a digit back in ASCII hexadecimal range (AX=0032h)
  30. Move the contents of AL to DL and display it on the screen.
  31. Move result from temporary variable to AX again (Now AX=02DFh)
  32. Mask the third nibble by AND operation with number 00F0h (AND AX,00F0h so AX=00D0h)
  33. Rotate the contents of AX to right by 4(in decimal)
  34. Call Output procedure with AL register to make a digit back in ASCII hexadecimal range      (AX=00044h(ASCII hex value for ‘D’)
  35. Move the contents of AL to DL and display it on the screen
  36. Move result back from temporary variable to AX again (Now AX=02DFh)
  37. Mask the fourth nibble by AND operation with number 000Fh (AND AX,000Fh so AX=000Fh)
  38. Call Output procedure with AL register to make a digit back in ASCII hexadecimal range (AX=0046h(i.e ASCII hex value for ‘F’))
  39. Move the contents of AL to DL and display it on the screen.
  40. 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.
TASM Program :
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 mul8
 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

 mov al,bl ; Multiply the two accepted Number's
 mov bl,cl
 mul bl

 mov cx,ax ; Store the value of the Result

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

 and ax,0f000h ; Isolate 1000's place of Result
 ror ax,12

 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 ax,0f00h ; Isolate 100's place of Result
 ror ax,8

 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 ax,00f0h ; Isolate 10's place of Result
 ror ax,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 ax,000fh ; 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 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 mul8.asm
Turbo Assembler  Version 4.1  Copyright (c) 1988, 1996 Borland International

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


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

C:\TASM\BIN>mul8

Enter first number: 0C
Enter second number: 0B
The Result is: 0084
--------------------------------

0 comments: