0% found this document useful (0 votes)
4 views79 pages

CE 302 Microprocessors Week 7

The document covers assembly language arithmetic and logic instructions, detailing operations such as addition, subtraction, multiplication, and division, along with their effects on the flag register. It explains the handling of unsigned numbers and provides examples of addition and subtraction operations, including the use of specific instructions like INC, ADC, and SBB. Additionally, it discusses logic instructions like AND and OR, highlighting their functionality and impact on operand values.

Uploaded by

likohov635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views79 pages

CE 302 Microprocessors Week 7

The document covers assembly language arithmetic and logic instructions, detailing operations such as addition, subtraction, multiplication, and division, along with their effects on the flag register. It explains the handling of unsigned numbers and provides examples of addition and subtraction operations, including the use of specific instructions like INC, ADC, and SBB. Additionally, it discusses logic instructions like AND and OR, highlighting their functionality and impact on operand values.

Uploaded by

likohov635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Week 7

Assembly Language
Arithmetic Logic Instructions

These lecture notes are based on the book by Muhammed Ali Mazidi,
Janice Gillispie Mazidi, Danny Causey; «The x86 PC assembly language,
design, ad interfacing», 5the Ed., Prentice Hall
Introduction
u The arithmetic instructions include
u addition, subtraction, multiplication, division,
comparison, negation, increment, and decrement.

u The logic instructions include


u AND, OR, Exclusive-OR, NOT, shifts, rotates, and the
logical compare (TEST).
Arithmetic Logic Instructions

u Whenarithmetic and logic instructions


execute, contents of the flag register
change.
u interrupt, trap, and other flags do not change
u Any ADD instruction modifies the contents of
the sign, zero, carry, auxiliary carry, parity,
and overflow flags.
UNSIGNED ADDITION AND SUBTRACTION

u Unsigned numbers are defined as data in which


all the bits are used to represent data.
u Applies to the ADD and SUB instructions.
u No bits are set aside for the positive or negative
sign.
u Between 00 and FFH (0 to 255 decimal) for 8-bit data.
u Between 0000 and FFFFH (0 to 65535 decimal) for 16-bit
data.
UNSIGNED ADDITION AND SUBTRACTION
addition of unsigned numbers
u The form of the ADD instruction is:

• ADD and ADC are used to add two operands.


– The destination operand can be a register or in memory.
– The source operand can be a register, in memory, or immediate.
• Memory-to-memory operations are never allowed in x86
Assembly language.
– The instruction could change ZF, SF, AF, CF, or PF bits of the
flag register.
UNSIGNED ADDITION AND SUBTRACTION
addition of unsigned numbers
u Register Addition
u ADD AX, BX
u ADD AX, CX
u ADD AX, DX

u Immediate Addition
u Immediate addition is employed whenever constant or
known data are added.
u MOV DL, 12H
u ADD DL, 33H
Memory-to-Register Addition

u Moves memory data to be added to the AL (and other)


register.

MOVE DI, OFFSET NUMB


MOV AL, 0
ADD AL, [DI]
ADD AL, [DI+1]
Array Addition

Memory arrays are sequential lists of data

MOV AL, 0 ; clear sum


MOV SI, 3 ; address element 3
ADD AL, ARRAY[SI] ; add element 3
ADD AL, ARRAY[SI+2] ; add element 5
ADD AL, ARRAY[SI+4] ; add element 7

This element is ARRAY [1]


Example
ARRAY DB 1, 3, 5, 4, 5, 9, 7,2,9
Increment Addition

u The INC instruction adds 1 to any register or


memory location, except a segment register.
u The size of the data must be described by using the
BYTE PTR, WORD PTR, DWORD PTR, or QWORD PTR
directives.
u The assembler program cannot determine if the INC
[DI] instruction is a byte-, word-, or doubleword-
sized increment.
Increment Addition

INC BL ; BL = BL + 1
INC EAX ; EAX = EAX + 1
INC BYTE PTR[BX] ;increments the byte data at the data
;segment addressed by BX
INC WORD PTR[SI] ;increments the word data at the data
;segment addressed by BX
INC DATA1 ; adds 1 to the data at memory
; location DATA1
UNSIGNED ADDITION AND SUBTRACTION
addition of unsigned numbers

245
011
+_____
256
UNSIGNED ADDITION AND SUBTRACTION CASE1
addition of individual byte/word data
u Program 3-1a uses AH to accumulate carries as the operands are added to AL.
UNSIGNED ADDITION AND SUBTRACTION CASE1
addition of individual byte/word data
u Program 3-1a uses AH to accumulate carries as the operands are added to AL.

.DATA
COUNT EQU05 ;-------loop---------------------------
DATA1 DB 125, 235, 195, 91, 48 BACK: ADD AL, [SI]
JNC OVER
If there is a
SUMDW ? carry
;-------------initializing------------- INC AH
OVER: INC SI increment AH
.CODE
MAIN PROC FAR DEC CX
MOV AX, @DATA JNZ BACK
MOV DS, AX
MOV SUM, AX
MOV CX, COUNT
MOV SI, OFFSET DATA1 MOV AH, 4CH
MOV AX, 00 INT 21H
; MAIN ENDP
; END MAIN
UNSIGNED ADDITION AND SUBTRACTION CASE1
addition of individual byte/word data

u Numbers are converted to hex by the assembler:


u 125=7DH 235=0EBH 197=0C5H 91=5BH 48=30H

00 + 7D = 7D AH = 0 AL = 7D, CX = 4, CF = 0, ZF = 0
7D + EB = 168 AH = 1 AL = 68, CX = 3, CF = 1, ZF = 0
68 + C5 = 12B AH = 2 AL = 2D, CX = 2, CF = 1, ZF = 0
2D + 5B = 88 AH = 2 AL = 88, CX = 1, CF = 0, ZF = 0
88 + 30 = B8 AH = 2 AL = B8, CX = 0, CF = 0, ZF = 1

SUM = B802
UNSIGNED ADDITION AND SUBTRACTION
CASE1 addition of individual byte/word data
u Due to pipelining it is strongly recommended that the following lines of the
program be replaced:

– The "ADC AH,00" instruction in reality means add


00+AH+CF and place the result in AH.
• More efficient since the instruction "JNC OVER" has to empty
the queue of pipelined instructions and fetch the instructions
from the OVER target every time the carry is zero (CF = 0).
UNSIGNED ADDITION AND SUBTRACTION
CASE2 addition of multiword numbers
ADDING WORDS

.DATA
COUNT EQU05 ;-------loop---------------------------
DATA1 DW 27345,28521,29533,30105, 52375 BACK: ADD AX, [SI]
ADC BX, 0 ;add carry to BX
SUMDW 2 DUP(?) INC SI
;-------------initializing------------- INC SI
.CODE DEC CX
MAIN PROC FAR JNZ BACK
MOV AX, @DATA
MOV DS, AX MOV SUM, AX
MOV SUM+2, BX
MOV CX, COUNT
MOV SI, OFFSET DATA1 MOV AH, 4CH
MOV AX, 00 INT 21H
MOV BX, AX MAIN ENDP
; END MAIN
;
Addition of 32-bit numbers in 8086-80286
This cannot be easily performed without adding the carry flag bit because the
8086–80286 only adds 8- or 16-bit numbers

Add BX|AX + DX|CX

ADD AX, CX
ADC BX, DX
Subtraction of Unsigned Numbers

u Many forms of subtraction (SUB) appear in the instruction set.


u these use any addressing mode with 8-, 16-, or 32-bit data
u a special form of subtraction (decrement, or DEC) subtracts 1 from any
register or memory location
u Numbers that are wider than 16 bits or 32 bits must
occasionally be subtracted.
u the subtract-with-borrow instruction (SBB) performs this type of
subtraction
UNSIGNED ADDITION AND SUBTRACTION
Subtraction of unsigned numbers
SUBTRACTION is done by adding the 2's complement of the subtrahend.

• Steps in executing the SUB instruction:


1. Take the 2's complement of the subtrahend. (source
operand)
2. Add it to the minuend. (destination operand)
3. Invert the carry.
• The steps are performed for every SUB instruction regardless of
source & destination of the operands.
Examples

SUB BL, AL DEC BH


SUB AX, SP DEC CX
SUB DH, 6FH DEC BYTE PTR[DI]
SUB AX, 0CCABH DEC WORD PTR[BP]
SUB [DI], CH DEC NUMB
SUB AH, TEMP
SUB AX, 12
UNSIGNED ADDITION AND SUBTRACTION
Subtraction of unsigned numbers mov al,3fh
mov bl,23h
sub al, bl
u After the execution, if CF = 0, the result is positive.
u If CF = 1, the result is negative, and the destination
has the 2's complement of the result.
Carry is
inverted
Subtraction Example
mov al,31h
mov bl,23h
sub bl,al

Means bl - al

23H= 0010 0011


31H= 0011 0001
-31H= 1100 1111
23+(-31)
0010 0011
1100 1111
1111 0010 = F2H
The result is in 2’s complement form- convert to actual value
0000 1110 = -0EH
Carry = 0 but invert
UNSIGNED ADDITION AND SUBTRACTION
subtraction of unsigned numbers
u NOT performs the 1's complement of the operand.
u The operand is incremented to get the 2's complement.
Subtraction-with-Borrow
u A subtraction-with-borrow (SBB) instruction functions as a regular
subtraction, except that the carry flag (C), which holds the borrow, also
subtracts from the difference.
u most common use is subtractions wider than 16 bits in the 8086–80286
microprocessors or wider than 32 bits in the 80386–Core2.
u wide subtractions require borrows to propagate through the subtraction,
just as wide additions propagate the carry

SUB AX, DI
SBB BX, SI
Subtraction-with-Borrow
SBB is used for multibyte (multiword) numbers.
It will take care of the borrow of the lower operand. Loads a double
If the carry flag is 0, SBB works like SUB. word but only 16
bits of it
If the carry flag is 1, SBB subtracts 1 from the result

Carry is
zero; but
invert
UNSIGNED MULTIPLICATION & DIVISION
multiplication of unsigned numbers
u In multiplying two numbers in the x86 processor, use of registers AX, AL,
AH, and DX is necessary.
u The function assumes the use of those registers.
u Three multiplication cases:
u byte times byte; word times word; byte times word.
UNSIGNED MULTIPLICATION & DIVISION
multiplication of unsigned numbers
u byte × byte - one of the operands must be in the AL register and the
second can be in a register or in memory.
u After the multiplication, the result is in AX.

– 25H is multiplied by 65H and the result is saved in word-sized


memory named RESULT.
• Register addressing mode was used.
UNSIGNED MULTIPLICATION & DIVISION
multiplication of unsigned numbers
u word × word - one operand must be in AX & the second operand
can be in a register or memory.
u After multiplication, AX & DX will contain the result.
u Since word-by-word multiplication can produce a 32-bit result, AX will hold the
lower word and DX the higher word.
UNSIGNED MULTIPLICATION & DIVISION
multiplication of unsigned numbers
u word × byte - similar to word-by-word multiplication except
that AL contains the byte operand and AH must be set to zero.
UNSIGNED MULTIPLICATION & DIVISION
division of unsigned numbers

u Like multiplication, division of two numbers in the x86 uses of


registers AX, AL, AH, and DX.
u Four division cases:
u byte over byte;
u word over word.
u word over byte;
u doubleword over word.
UNSIGNED MULTIPLICATION & DIVISION
division of unsigned numbers

u Individe, in cases where the CPU cannot


perform the division, an interrupt is activated.
u Referred to as an exception, and the PC will display
a Divide Error message.
u If the denominator is zero. (dividing any number by 00)
u If the quotient is too large for the assigned register.
UNSIGNED MULTIPLICATION & DIVISION
division of unsigned numbers
u byte/byte - the numerator must be in the AL register and AH
must be set to zero.
u The denominator cannot be immediate but can be in a register or
memory, supported by the addressing modes.
u After the DIV instruction is performed, the quotient is in AL and the
remainder is in AH.
UNSIGNED MULTIPLICATION & DIVISION
division of unsigned numbers

u Various addressing modes of the denominator.


UNSIGNED MULTIPLICATION & DIVISION
division of unsigned numbers
DATA7 DB 95
DATA8 DB 10
u Various addressing modes of the denominator. QUOT1 DB ?
REMAIN DB ?
UNSIGNED MULTIPLICATION & DIVISION
division of unsigned numbers

u word/word - the numerator is in AX, and DX must be


cleared.
u The denominator can be in a register or memory.
u After DIV, AX will have the quotient.
u The remainder will be in DX.
UNSIGNED MULTIPLICATION & DIVISION
division of unsigned numbers
u word/byte - the numerator is in AX & the denominator can be in
a register or memory.
u After DIV, AL will contain the quotient, AH the remainder.
u The maximum quotient is FFH.
u This program divides AX = 2055 by CL = 100.
u The quotient is AL = 14H (20 decimal)
u The remainder is AH = 37H (55 decimal).
UNSIGNED MULTIPLICATION & DIVISION
division of unsigned numbers
u doubleword/word - the numerator is in AX and DX.
u The most significant word in DX, least significant in AX.
u The denominator can be in a register or in memory.
u After DIV, the quotient will be in AX, the remainder in DX.
u The maximum quotient FFFFH.
LOGIC INSTRUCTIONS - AND
u AND destination, source
u This instruction will perform a logical AND
on the operands, and place the result in the
destination.
u Destination operand can be a register or in
memory.
u Source
operand can be a register, memory, or
immediate.

• AND will automatically change the CF & OF to zero.


– PF, ZF, and SF are set according to the result.
• The rest of the flags are either undecided or unaffected.
LOGIC INSTRUCTIONS - AND

u AND can mask certain bits of the operand, and also to test for a zero
operand:

This code will AND DH with itself


and set ZF = 1 if the result is zero.
LOGIC INSTRUCTIONS - OR
u OR destination, source
u Destination/source operands are OR’ed, result
placed in the destination.
u Can set certain bits of an operand to 1.
u Destination operand can be a register or in memory.
u Source operand can be a register, in memory, or
immediate.

• Flags are set the same as for the AND instruction.


– CF & OF will be reset to zero.
• SF, ZF, and PF will be set according to the result.
• All other flags are not affected.
LOGIC INSTRUCTIONS - OR

• The OR instruction can also be used to test for a zero


operand.
– "OR BL,0" will OR the register BL with 0 and make ZF = 1 if
BL is zero.
– "OR BL,BL" will achieve the same result.
LOGIC INSTRUCTIONS - XOR

u XOR dest, src


u XORwill eXclusive-OR operands and
place result in the destination.
u Sets the result bits to 1 if they are not
equal, otherwise, reset to 0.
u Flags are set the same as for AND.
u Operand rules are the same as in the AND
and OR instructions.
LOGIC INSTRUCTIONS - XOR
u XOR can be used to see if two registers have the same value.
u "XOR BX,CX" will make ZF = 1 if both registers have the same value, and
if they do, the result (0000) is saved in BX, the destination.
u A widely used application of XOR is to toggle bits of an operand.

– Toggling bit 2 of register AL would cause it to change to the


opposite value; all other bits remain unchanged.
LOGIC INSTRUCTIONS - XOR
LOGIC INSTRUCTIONS - SHIFT

u Shifts the contents of a register or memory location


right or left.
u There are two kinds of shifts:
u Logical - for unsigned operands.
u Arithmetic - for signed operands.

u The number of times (or bits) the operand is shifted


can be specified directly if it is once only.
u Through the CL register if it is more than once.
LOGIC INSTRUCTIONS - SHIFT RIGHT
u SHR - logical shift right.
u Operand is shifted right bit by bit.
u For every shift the LSB (least significant bit) will
go to the carry flag. (CF)
u The MSB (most significant bit) is filled with 0.
LOGIC INSTRUCTIONS - SHIFT RIGHT
u If the operand is to be shifted once only, this is specified in
the SHR instruction itself.

– After the shift, BX = 7FFFH and CF = 1. SHIFT.


LOGIC INSTRUCTIONS - SHIFT RIGHT

u The operand to be shifted can be in a register or in memory.


u Immediate addressing mode is not allowed for SHIFT.
u "SHR 25,CL" will cause the assembler to give an error.
LOGIC INSTRUCTIONS - SHIFT LEFT
u SHL - Logical shift left, the reverse of SHR.
u After every shift, the LSB is filled with 0.
u MSB goes to CF.
u All rules are the same as for SHR.

3-11 can also


be coded as:
ROTATE INSTRUCTIONS
u ROR, ROL and RCR, RCL are designed specifically to perform a
bitwise rotation of an operand.
u They allow a program to rotate an operand right or left.
u Similar to shift instructions, if the number of times an operand
is to be rotated is more than 1, this is indicated by CL.
u The operand can be in a register or memory.
u There are two types of rotations.
u Simple rotation of the bits of the operand
u Rotation through the carry.
ROTATE INSTRUCTIONS
ROR/ROL rotate right/rotate left

u In ROR (Rotate Right), as bits are shifted from left to right, they
exit from the right end (LSB) and enter the left end (MSB).
u As each bit exits LSB, a copy is given to the carry flag.
u In ROR the LSB is moved to the MSB, & copied to CF.

Programs 3-7 & 3-8 on page 120 show applications of rotation instructions
ROTATE INSTRUCTIONS
ROR/ROL rotate right/rotate left

• In ROL (Rotate Left), as bits are shifted from right


to left, they exit the left end (MSB) and enter the right
end (LSB).
– Every bit that leaves the MSB is copied to the carry flag.
• In ROL the MSB is moved to the LSB and is also copied to CF

Programs 3-7 & 3-8 on page 120 show applications of rotation instructions
ROTATE INSTRUCTIONS
ROR rotate right

u If the operand is to be rotated once, the 1 is coded.


u If it is to be rotated more than once, register CL is used
to hold the number of times it is to be rotated.
ROTATE INSTRUCTIONS
ROL rotate left

u If the operand is to be rotated once, the 1 is coded.


u If it is to be rotated more than once, register CL is used to hold the number of
times it is to be rotated.
ROTATE INSTRUCTIONS
RCR/RCL right/left through carry
u In RCR, as bits are shifted from left to right, they exit the right end
(LSB) to the carry flag, and the carry flag enters the left end (MSB).
u The LSB is moved to CF and CF is moved to the MSB.
u CF acts as if it is part of the operand.
ROTATE INSTRUCTIONS
RCR/RCL right/left through carry
• In RCL, as bits are shifted from right to left, they exit
the left end (MSB) and enter the carry flag, and the
carry flag enters the right end (LSB).
– The MSB is moved to CF and CF is moved to the LSB.
• CF acts as if it is part of the operand.
ROTATE INSTRUCTIONS
RCR right through carry

1010

u If the operand is to be rotated once, the 1 is coded.


u If more than once, register CL holds the number of rotations.
LOGIC INSTRUCTIONS
COMPARE of unsigned numbers
u CMP destination , source
u Compares two operands & changes flags according to the result of the
comparison, leaving the operand unchanged.
u Destination operand can be in a register or in memory.
u Source operand can be in a register, in memory, or immediate.
u CF, AF, SF, PF, ZF, and OF flags reflect the result.
u Only CF and ZF are used.

Result positive
Result 0
Result negative
LOGIC INSTRUCTIONS
COMPARE of unsigned numbers
u Compare is really a SUBtraction.
u Except that the values of the operands do not change.
u Flags are changed according to the execution of SUB.
u Operands are unaffected regardless of the result.
u Only the flags are affected.
LOGIC INSTRUCTIONS
COMPARE of unsigned numbers

u Program 3-3 uses CMP to find the highest byte in a series of 5 bytes
defined in the data segment.

.DATA AGAIN: CMP AL, [BX]


GRADES DB 69, 87, 96, 45, 75 JA NEXT
ORG 0008H MOV AL, [BX]
HIGHEST DB ? NEXT: INC BX
;-------------initializing------------- LOOP AGAIN
.CODE
MAIN PROC FAR MOV HIGHEST, AL
MOV AX, @DATA
MOV DS, AX MOV AH, 4CH
MOV CX, 5 INT 21H
MOV BX, OFFSET GRADES MAIN ENDP
XOR AL,AL END MAIN
;
;
LOGIC INSTRUCTIONS
COMPARE of unsigned numbers

u Program 3-3, coded in Assembly language, uses register AL to


hold the highest grade found so far.
u AL is given the initial value of 0.
u A loop compares each of the 5 bytes with AL.
u If AL contains a higher value, the loop continues to check the next byte.
u If AL is smaller than the byte checked, the contents of AL are replaced
by that byte and the loop continues.
LOGIC INSTRUCTIONS
COMPARE of unsigned numbers
u There is a relationship between the pattern of lowercase/uppercase ASCII letters,
as shown below for A and a:

A 0100 000141H The only bit that changes is d5.


To change from lowercase to
a 0110 000161H uppercase, d5 must be masked.
LOGIC INSTRUCTIONS
COMPARE of unsigned numbers

u Program 3-4 converts lower case letters to upper


case
u It detects if the letter is in lowercase,
u To determine if it is a lowercase letter, it is compared
with 61H and 7AH to see if it is in the range a to z.
u If true AND with 1101 1111B (DFH)
u Anything above or below this range should be left alone
LOGIC INSTRUCTIONS
COMPARE of unsigned numbers
LOWERCASE TO UPPERCASE CONVERSION

;-------loop---------------------------
.DATA
BACK: MOV AL, [SI]
DATA1 BB ‘My NAME is jOe’
CMP AL, 61H ;if less than ’a’
ORG 0020H
JB OVER
DATA2 DB 14 DUP(?)
CMP AL, 7AH ;if greater than ’z’
;-------------initializing-------------
.CODE JA OVER ;no need toconvert
MAIN PROC FAR AND AL, 11011111B ;convert uppercase
MOV AX, @DATA OVER: MOV [BX], AL
MOV DS, AX INC SI
MOV SI, OFFSET DATA1 INC BX
MOV BX, OFFSET DATA2 LOOP BACK
MOV CX, 14
; MOV AH, 4CH
; INT 21H
MAIN ENDP
END MAIN
BCD AND ASCII CONVERSION
BCD number system

u BCD stands for binary coded


decimal.
u Needed because we use the digits 0
to 9 for numbers in everyday life.
u Computer literature features
two terms for BCD numbers:
u Unpacked BCD.
u Packed BCD.
BCD AND ASCII CONVERSION
BCD unpacked vs. packed

u In unpacked BCD, the lower 4 bits of the number represent


the BCD number.
u The rest of the bits are 0.
u "0000 1001" and "0000 0101" are unpacked BCD for 9 & 5.
u

u Unpacked BCD it takes 1 byte of memory location.


u Or a register of 8 bits to contain the number.
BCD AND ASCII CONVERSION
BCD unpacked vs. packed

• In packed BCD, a single byte has two BCD numbers.


– One in the lower 4 bits; One in the upper 4 bits.
• "0101 1001" is packed BCD for 59.

– As it takes only 1 byte of memory to store the packed


BCD operands, it is twice as efficient in storing data.
BCD AND ASCII CONVERSION
ASCII numbers
u In ASCII keyboards, when key "0" is activated "011 0000" (30H) is
provided to the computer.
u 31H (011 0001) is provided for key "1", etc.

• To convert ASCII data to BCD, removed the tagged


"011" in the higher 4 bits of the ASCII.
– Each ASCII number is ANDed with "0000 1111“. (0FH)
BCD AND ASCII CONVERSION
ASCII to unpacked BCD conversion
u Programs 3-5a, 3-5b, and 3-5c show three methods for converting the 10 ASCII digits to
unpacked BCD.
u Using this data segment:

The data is defined as DB, a


byte definition directive, and is
accessed in word-sized chunks.

Program 3-5a
BCD AND ASCII CONVERSION
ASCII to unpacked BCD conversion
u Programs 3-5a, 3-5b, and 3-5c show three methods for converting the 10 ASCII digits to
unpacked BCD.
u Using this data segment:

Using the PTR directive as


shown, makes the code more
readable for programmers.

Program 3-5b
BCD AND ASCII CONVERSION
ASCII to unpacked BCD conversion
u Programs 3-5a, 3-5b, and 3-5c show three methods for converting the 10 ASCII
digits to unpacked BCD.
u Using this data segment:

3-5c uses based addressing


mode since BX+ASC is used
as a pointer.

Program 3-5c
BCD AND ASCII CONVERSION
ASCII/BCD conversions

u To convert ASCII to packed BCD, it is converted to unpacked


BCD (eliminating the 3), then combined to make packed
BCD.

u To convert packed BCD to ASCII, it must first be converted


to unpacked.
u The unpacked BCD is tagged with 011 0000 (30H).
BCD AND ASCII CONVERSION
ASCII to packed BCD conversion
u For 4 & 7, the keyboard gives 34 & 37, respectively.
u The goal is to produce packed BCD 47H or “0100 0111“.

AX:
3734
0704
0407
4007
4047
BCD AND ASCII CONVERSION
packed BCD to ASCII conversion
u Converting from packed BCD to ASCII.

AX:
2929
2009
0209
3239
BCD AND ASCII CONVERSION
BCD addition and subtraction
u After adding packed BCD numbers, the result may be no longer BCD.

Adding them gives 0011 1111B (3FH). (not BCD)

– The result should have been 17 +


0001 0111
0010 1000 28 = 45 (0100 0101).
+---------------- • To correct, add 6 (0110) to the low
0011 1111 digit: 3F + 06 = 45H.
0110 – The same could have happened in
+----------------
0100 0101 AC=1 the upper digit.
• This problem is so pervasive that the
vast majority of microprocessors
have an instruction to deal with it.
BCD AND ASCII CONVERSION
DAA
u DAA (decimal adjust for addition) is provided in the x86 for
correcting the BCD addition problem.
u DAA will add 6 to the lower, or higher nibble if needed
u Otherwise, it will leave the result alone.

After execution, DATA3 will contain 72H.


BCD AND ASCII CONVERSION
DAA general rules & summary
u General rules for DAA:
u The source can be an operand of any addressing mode.
u The destination must be AL in order for DAA to work.
u DAA must be used after the addition of BCD operands.
u BCD operands can never have any digit greater than 9.
u DAA works only after an ADD instruction.
u It will not work after the INC instruction.

• After an ADD or ADC instruction:


– If the lower nibble (4 bits) is greater than 9, or if AF = 1.
• Add 0110 to the lower 4 bits.
– If the upper nibble is greater than 9, or if CF = 1.
• Add 0110 to the upper nibble.
BCD AND ASCII CONVERSION
DAA summary of action
Use of DAA after adding multibyte packed BCD numbers.

Program 3-6

See the entire program listing on pages 116-117 of your textbook.


BCD AND ASCII CONVERSION
BCD subtraction and correction
u DAS (decimal adjust for subraction) is provided in
the x86 for correcting the BCD subtraction problem.
u When subtracting packed BCD (single-byte or multibyte) operands,
the DAS instruction is used after SUB or SBB.
u AL must be used as the destination register.

• After a SUB or SBB instruction:


– If the lower nibble is greater than 9, or if AF = 1.
• Subtract 0110 from the lower 4 bits.
– If the upper nibble is greater than 9, or CF = 1.
• Subtract 0110 from the upper nibble.

You might also like