Friday 25 January 2013

8086 microprocessor Programming (Part 8)

ADJUSTMENT INSTRUCTION

OBJECTIVES:


  • To study the 8086 instruction for ASCII and decimal adjustment for addition and subtraction.
  • To study the 8086 instruction for adjustment of multiplication and division of unpacked BCD numbers


INSTRUCTION:

ASCII and Packed Decimal Arithmetic:
When ASCII digits are added, the AAA( ASCII adjust after addition) instruction converts the binary result to BCD format. AAS performs the same operations after subtraction and AAM is used after multiplication. AAD (ASCII adjust before division) modifies AX before division takes place. All of this instruction is valid only for 8-bit operations.
Two instructions adjust the result of packed decimal operation. The DAA (decimal adjust after addition)  instruction adjust the contents of AL after 8-bit addition or subtraction, respectively

EXPERIMENT:


There are four parts of this experiment, each containing a relatively  smaller program. It will be convenient for you to put the main body of the program in the program template defined earlier in experiment 4. This will help you save time writing four times the common MASM directives and initialization instructions.

Part-1 : 

The following program is taken from chapter 8 of [1]. Writes it in a text editor, assemble, and link it, and then load it in DEBUG. Trace the program, and note down the contents of AL before and after the execution of AAA instruction, each time it is executed. Try the program for some other values of two input operands.

Page, 132
Title ASCII Decimal Addition
dosseg
.model small
.stack 100h
numdigits equ   8 ; number of digit
.code
comment # Demonstration of ASCII addition using two 8-digit strings
The AAA instruction adjusts the result after each addition#.
main   proc
mov ax @data ; initialize the DS register
mov ds,ax
mov   cx, numdigits ; set loop counter
mov  si, numdigits¬_1 ; point SI to first digit
mov ax, 0 ; initialize AX
next_digit :
mov al, val1[si] ; get digit from val1
add al, ah ; add previous carry
mov ah,0
add al, val2[si] ; add digit from val2
aaa ; AX= adjusted result
; AH contains carry if an y
or  al,30h ; convert digit to ASCII
mov result[si], al ; store digit in result
dec  si
dec cx
jnz next_digit ; back up the index
mov ax, 4C00h
int 21 h
main endp
.data
val1 db ‘09999999’ ; input operand
val2 db ‘09999999’ ; input operand
result db  numdigits dup(0)    ; result ‘19999998’
end main

Note : We have used a new assembler directive and a new member operator in the       program They are
1. EQU Directive: It assigns a symbolic name to a string or numeric constant. This increase the readability of a program and makes it possible to change constant in a single place at the beginning of a program .
2. DUP Operator: It repeats a single- or multiple byte value. For example, 20 bytes containing all binary  zeros would be coded as : db 20 dup ( 0)

Part –II:

The following program adds two 9-byte packed BCD numbers. Refer to chapter 8 of [1]. Trace it with DEBUG and note down the contents of AL both before and after the DAA instruction, each time it is executed check the AC and C flages as well.
Page, 132
title packed decimal addition
dosseg
.model small
.stack 100 h
.code
numbytes  equ   9 ; number of bytes to be added
main proc
mov ax. @data ; initialize the DS register
mov dx,ax
mov cx, numbytes ;
mov si, numbytes_1 ; start at lowest digit
clc ; clear carry flag
next_digit:
mov al, val1 [si] ; get digit from val1
adc al, val2 [si] ; add digit from val2 + carry
daa ; AL contains adjusted result
mov result [si] ; store the digit
dec si ; point to next byte
loop next_digit
mov ax, 4c00h
int   21h
main endp
data ; variables here
val1    db 09h,87h,65h,43h,87h46h,69h, 83h,76h
val2   db 00h,08h,76h,54h,32h,72h,84h,05h
result db numbytes dup (0)
end main

Note: The ‘loop’ instruction decrements CX register and jumps to a short label if CX is greater that zero. The short label must be in the range- 128 to + 127 bytes from the next instruction

Part-III:

The following program contains adjustment instruction for subtraction. Trace it in DEBUG, and note down the constants of AL and status flags where asked in the comments.
Page, 132
title Subtraction
dosseg
.model small
.stack 100 h
.code
main proc
mov ax,@data ; initgialize the DS register
mov ds, ax
mov al,38h ; ASCII for ‘8’
move bl,39h ; ASCII for ‘9’
sub al,bl ; note down
aas ; note down and comment
mov  al, 48h ; packed BCD 40
mov bl,85h ; packed BCD85
sub al,bl ; note down
das ; note down
mov ax,4C00h
int 21 h
main endp
.data ; variables here
end main

Part –IV:

The following program contains adjustment instructions for multiplication and division of unpacked BCD numbers. Write the program and load the ‘exe’file in DEBUG, Skip whenever you come across an ‘int 21h’instruction by setting appropriate breakpoint. Try for different input values

Page, 132.
title Multiplication and Division
dosseg
.model small
.stack 100h
.code
main proc
mov ax, @data ; initialize the DS register
mov da,ax
Comment # note down the constants of AX before and after the execution of AAM instruction #
mov   ah,01h ; inpute mode
int  21h ; get multiplicand in AL
and al, 0fh ; make it unpacked BCD
mov  bl,al ; save in BI
int     21h ; get multiplier
and al,ofh ; make it unpacked BCD
nul bl ; multiply the two numbers
aam ; ASCII adjust after
; multiplication
comment # Note down the contents of AX before and after the execution of AAD instruction # .
move ah,01h ; inpute mode
int  21 h ; get divisor in AL
and al ,0fh ; make it unpacked BCD
move bl,al ; save in BL
int 21 h ; get MS digit of dividend
and al,ofh ; make it unpacked BCD
move bh,al ; save in BH
int 21h ; get LS digit of dividend
move al,0fh ; make it unpacked BCD
mov  ah, bh ; now AX contains the dividend
; concerts to hex
aad ; adjustment before division

div bl
mov ax,4C00h
int 21h
main endp
.data ; variables here
end main

REFERENCES:


  1. Kip R. Irvine. Assembly Language for the IBM-PC Macmillan Publishing Company, 1990.


2 comments:

  1. Really Awesoem work, In case you need for any converter for your Mov file then use this one;

    Unique Converter

    ReplyDelete
  2. That converter will helps you to transfer your one file to other files;

    M4r to M4a

    Mts to Mov

    Mp4 to Ogg

    Aif to Wav

    Wmv to Mp3

    ReplyDelete