ascii adjust & decimal adjust

30
ASCII ADJUST AND DECIMAL ADJUST 1

Upload: techmx

Post on 28-Oct-2014

111 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ASCII Adjust & Decimal Adjust

ASCII ADJUST AND DECIMAL ADJUST

1

Page 2: ASCII Adjust & Decimal Adjust

INSTRUCTIONS

AAA - ASCII Adjust After Addition AAS - ASCII Adjust After Subtraction AAM - ASCII Adjust After Multiply AAD - ASCII Adjust Before Division DAA - Decimal Adjust for Addition DAS - Decimal Adjust for Subtraction

2

Page 3: ASCII Adjust & Decimal Adjust

INTRODUCTION

The PC supports BCD format,

Uses of BCD

1)No loss of precision

2)Simpler to perform arithmetic operation on small values from keyboard

BCD can be stored in two way:

Unpacked BCD

Packed BCD3

Page 4: ASCII Adjust & Decimal Adjust

4

Unpacked BCD representation contains only One decimal digit per byte. The digit is stored in the least significant 4 bits; the most significant 4 bits are not relevant to the value of the represented number.

Example: Representing 1527 01 05 02 07h

Unpacked BCD Data

Page 5: ASCII Adjust & Decimal Adjust

5

Packed BCD Data

Packed BCD representation packs two Decimal digits into a single byte.

Example: Representing 1527 15 27h

Page 6: ASCII Adjust & Decimal Adjust

ASCII Adjust After Addition

Adjusts the result of the addition of two unpacked BCD values to create a unpacked BCD result.

Operation 1:In ALIf rightmost nibble is >9 (ie)A to F Or AuxilaryFlag=1ADD 6 to rightmost nibble

6

Page 7: ASCII Adjust & Decimal Adjust

Operation 2:Clear left nibble form AL.

Operation 3: In AH ADD 1

Operation 4:Set Carry and AuxilaryCarry

7

Page 8: ASCII Adjust & Decimal Adjust

.model small

.datab1 dw 38hb2 dw 34h

.codemov ax,@datamov ds,axmov ax,b1 ;moving unpacked BCD into axmov bx,b2 ;moving unpacked BCD into bxadd ax,bxaaa ;adjusting unpacked BCD after additionor ax,3030hend

8

Page 9: ASCII Adjust & Decimal Adjust

ASCII Adjust After Subtraction

Adjusts the result of the subtraction of two unpacked BCD values to create a unpacked BCD result.

Operation 1: a)AAS checks the rightmost nibble in AL b)If rightmost nibble is >9 (ie)A to F Or AuxilaryFlag=1 c)Then Subtract 6 from rightmost nibble

9

Page 10: ASCII Adjust & Decimal Adjust

Operation 2:

Clear left nibble in AL.

Operation 3:

Subtracts 1 from AH

Operation 4:

Set Carry and AuxilaryCarry

10

Page 11: ASCII Adjust & Decimal Adjust

Example :d1 contains 34h , d2 contains 38h of byte

type Ax AF

Mov AL, d1 ; 0034 Sub AL, d2; 00fc 1AAS ; ff06 1 since the rightmost digit of AL is c ,

subtract 6 from AL Subtract 1 from ah, set AF and CF flags The answer is -4, is ff06 in 10’s

complement 11

Page 12: ASCII Adjust & Decimal Adjust

.model small

.datab1 dw 38hb2 dw 34h.codemov ax,@datamov ds,axmov ax,b1 ;moving unpacked BCD into axmov bx,b2 ;moving unpacked BCD into bxsub ax,bxaas ;adjusting unpacked BCD after

subtractionor ax,3030hend

12

Page 13: ASCII Adjust & Decimal Adjust

ASCII Adjust After Multiplication

For multiplication and Division of ASCII numbers require that the numbers first be converted into unpacked BCD format.

Before Multiplication First clear the leftmost nibble in each Byte.

After Multiplication AAM performs the following operations 1) Divides AL value by 10 (0AH) 2) Stores Quotient in AH 3) Store Remainder in AL

13

Page 14: ASCII Adjust & Decimal Adjust

Example:

AL contains 35H and CL contains 39H

Instruction comment AX CL

and CL, 0Fh; Convert CL to 09 0035 39

and AL,0Fh; Convert AL to 05 0005 09

mul CL; Multiply AL by CL 002D

AAM ; Convert to unpacked 0405

BCD

Or AX,3030h; covert to ASCII 3435

14

Page 15: ASCII Adjust & Decimal Adjust

Mul operation generates 45 (002Dh) in AX

AAM divides this value by 10, so quotient of 04 in AH and remainder of 05 in AL

OR instruction converts the unpacked BCD to

ASCII format15

Page 16: ASCII Adjust & Decimal Adjust

.model small

.datab1 dw 39h ; 9 in ASCII Formatb2 dw 35h ; 5 in ASCII Format.codemov ax,@datamov ds,axmov ax,b1 ;AX=0039hand AL,0fh ;AX=0009hmov bx,b2 ;BX=0035hand bl,0fh ;BX=0005hmul bx ;AX=002Dhaam or ax,3030hend

16

Page 17: ASCII Adjust & Decimal Adjust

ASCII Adjust Before Division AAD allows for a 2-Byte Dividend in AX. The

divisor can be only a single Byte(0-9) Before Division First clear the leftmost nibble in

each Byte. Operations done by AAD instruction 1) AAD multiplies the AH by 10(0Ah). 2) Then adds the product to AL and clears

the AH After AAD , division is performed.

17

Page 18: ASCII Adjust & Decimal Adjust

Example:

AX contains 3238 (28) - Dividend

CL contains 37 (07) – Divisor

Instruction comment AX CL

and CL,0Fh; convert to unpacked 3238 07

BCD

and AX,0F0Fh; convert to unpacked 0208 07

BCD

AAD; convert to binary 001C

div CL; divide by 7 0004

18

Page 19: ASCII Adjust & Decimal Adjust

AAD multiplies the AH by 10(0Ah) Adds the product 20(14h) to the AL and

clears

the AH The result is 001Ch, is hex

representation of decimal 28 Then division is performed. Remainder stored in AH, Quotient stored

in AL

19

Page 20: ASCII Adjust & Decimal Adjust

model small.datab1 dw 3238h ; 28 in ASCII Formatb2 db 37h ; 7 in ASCII Format.codemov ax,@datamov ds,axmov ax,b1 ;AX=3238hand ax,0f0fh ;AX=0208hmov cl,b2 ;CL=37hand cl,0fh ;CL=07h aad ; AX= 001cdiv cl ; AX=0004or ax,3030h; AX=3034end

20

Page 21: ASCII Adjust & Decimal Adjust

Decimal Adjust

To adjust the result of packed BCD numbers which stored in AL.

DAA - Decimal Adjust for Addition

DAS - Decimal Adjust for Subtraction

21

Page 22: ASCII Adjust & Decimal Adjust

Decimal Adjust for Addition

DAA performs the following operations:

Operation 1:

If AL is >9 (ie) A to F

or AuxilaryCarry=1 then

ADD 6 to AL

Set AuxilaryCarry=1

22

Page 23: ASCII Adjust & Decimal Adjust

Operation 2:

If AL contains value > 99

or Carry=1 then

ADD 60 to AL and set Carry =1

Otherwise

AuxilaryCarry=0 ,carry=0

23

Page 24: ASCII Adjust & Decimal Adjust

AL contains 45h

BL contains 45h

Instruction Comment AL

ADD AL,BL ; AL=AL+BL 8Ah

DAA ; Adjust packed BCD 90h

addition

24

Page 25: ASCII Adjust & Decimal Adjust

DAA checks the rightmost nibble of AL, it is A so add 6 to AL

Now AL = 90

25

Page 26: ASCII Adjust & Decimal Adjust

26

.model small

.datad1 dw 45h ;moving 45 as packed BCDd2 dw 45h ;moving 45 as packed BCD

.codemov ax,@datamov ds,axmov ax,d1mov bx,d2add ax,bxdaa ;adjusting the packed BCD after additionend

Page 27: ASCII Adjust & Decimal Adjust

Decimal Adjust for Subtraction

Operations performed by DAS :If AL is >9 (ie) A to F

or AuxilaryCarry=1 thenSubtract 6 from ALSet Carry=1

Otherwise AuxilaryCarry=0 ,carry=0

27

Page 28: ASCII Adjust & Decimal Adjust

AL contains 80h

BL contains 49h

Instruction Comment AL

SUB AL,BL ; AL=AL-BL 37h

DAS ; Adjust packed BCD 31h

Subtraction

28

Page 29: ASCII Adjust & Decimal Adjust

29

.model small

.datad1 dw 57hd2 dw 48h

.codemov ax,@datamov ds,axmov ax,d1 ;moving 45 as packed BCDmov bx,d2 ;moving 45 as packed BCDsub ax,bxdas ; adjusting the packed BCD after subractionend

Page 30: ASCII Adjust & Decimal Adjust

THANK U

30