CE 302 Microprocessors Week 7
CE 302 Microprocessors 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 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
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
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:
.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 AX, CX
ADC BX, DX
Subtraction of Unsigned Numbers
Means bl - al
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.
u AND can mask certain bits of the operand, and also to test for a zero
operand:
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
Programs 3-7 & 3-8 on page 120 show applications of rotation instructions
ROTATE INSTRUCTIONS
ROR rotate right
1010
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.
;-------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
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:
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:
Program 3-5c
BCD AND ASCII CONVERSION
ASCII/BCD conversions
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.
Program 3-6