MP 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

MICROPROCESSORS

Logic and Arithmetic Instructions

Slides adapted from slides by Prof. Levent Eren


2

Outline

• Memory Examples
• Logic instructions
• Shifting instructions
• Arithmetic operations
• Overflow and carries
• Important flags setting
3

Memory Access
Example

myvar1 DW 01234h ; define word variable mov si,OFFSET myvar2 ; use SI as a pointer to myvar2
; (value=1234h) ; (equiv C code: SI=&myvar2 )
myvar2 DW 01234 ; define word variable
;(value=1234d = 4D2h) mov ax,[si] ; read memory at myvar2 (*(&myvar2))
myvar3 DW ? ; define word variable ; (indirect reference)
;(value uncertain)
myvar4 DW 01BCDh mov bx,OFFSET ce308msg ; BX is a pointer to a string
; (equiv C code: BX=&ce308msg)
ce308msg DB ‘CE308 is great'
dec BYTE PTR [bx+1] ; make that ‘E' a ‘D' !!!!
start:
mov ax,cs ; set up data segment mov si, 1 ; Use SI as an index
mov ds,ax ; DS=CS inc ce308msg[SI] ; == inc [SI + offset ce308msg]
; == inc [SI + 8]
; any memory reference we make is assumed to reside in ; == inc [9]
;the DS segment
mov ax,myvar2 ; AX <- myvar2
; == mov ax,[offset myvar2]
; == mov ax,[2]
4

Memory Access
Example (cont.)

;Memory can be addressed using four registers: ; Furthermore, a fixed 8-bit or 16-bit displacement from the
; SI -> Assumes DS index and base registers is allowed.
; DI -> Assumes DS
; BX -> Assumes DS mov ax,[23h] ; ax <- word in memory DS:0023
; BP -> Assumes SS !!! (this is why it isn't frequently mov ah,[bx+5] ; ah <- byte in memory DS:(BX+5)
used) mov ax,[bx+si+107] ; ax <- word at DS:(BX+SI+107)
; Examples: mov ax,[bx+di+47] ; ax <- word at DS:(BX+DI+47)
mov ax,[bx] ; ax <- word in memory pointed to by BX
mov al,[bx] ; al <- byte in memory pointed to by BX ; REMEMBER: memory to memory moves are ILLEGAL!!!
mov ax,[si] ; ax <- word pointed to by SI mov [bx],[si] ;ILLEGAL
mov ah,[si] ; ah <- byte pointed to by SI mov [di],[si] ;ILLEGAL (use movsw)
mov cx,[di] ; di <- word pointed to by DI
; Special case: stack operations!
mov ax,[bp] ; AX <- SS:[BP] STACK OPERATION!!!! pop myvar ; myvar <- SS:[SP]

; In addition, BX+SI and BX+DI are allowed:


mov ax,[bx+si]
mov ch,[bx+di]
5

Flag Register

AC (Alignment check)
(VM) Virtual mode
(RF) Resume
(NT) Nested task
(IOPL) Input/output
privilege level
(O) Overflow
(D) Direction
(I) Interrupt
(T) Trace
(S) Sign
(Z) Zero
(A) Auxiliary Carry
(P) Parity
(C) Carry

8086, 8088, 80186 80386, 80486DX

80286 80486SX
6

Logic Instructions
• Logic instructions operate on a bit-by-bit basis
NOT: A =~A
AND: A &= B
OR: A |= B
XOR: A ^= B
• Except for NOT, these instructions affect the flags as follows:
• clear the carry (C)
• clear the overflow (O)
• set the zero flag (Z) if the result is zero, or clear it otherwise
• copy the high order bit of the result into the sign flag (S)
• set the parity bit (P) according to the parity (number of 1s) in the
result
• scramble the auxiliary carry flag (A)
• The NOT instruction does not affect any flag
7

Logic Instructions (cont.)


• TEST (non-destructive AND) - logically ANDs two
operands and sets the flags but does not save the result
• typically one would use this instruction to see if a bit contains one
e.g., test al, 1
• sets the flags identically to the AND instruction
• AND and OR instructions are often used to mask out data
• a mask value is used to force certain bits to zero or one within
some other value
• a mask typically affects certain bits and leaves other bits unaffected
• AND forces selected bits to zero AND cl, 0DFh
• OR forces selected bits to one OR cl, 0DFh
8

Shifting Instructions

/SAL
9

Shifting Instructions (cont.)


• SHL/SAL (shift left/shift arithmetic left)
• moves each bit of the operand one bit position to the left the
number of times specified by the count operand
• zeros fill vacated positions at the L.O. bit; the H.O. bit shifts into the
carry flag
• A quick way to multiply by two
• Useful in packing data, e.g., consider two nibbles in AL and AH that
we want to combine
SHL AH, 4 ;requires 80286 or later
OR AL, AH
NOTE: There are two forms of shifts
1) immediate shift count (8086, 8088 allow an immediate shift of 1 only,
e.g., SHL AX, 1)
2) CL register to hold the shift count (e.g., SHL AX, CL)
10

Example

;multiply AX by decimal 10 (1010) (Same as AX times 2+8 – associate prop)


SHL AX, 1 ;AX times 2
MOV BX, AX
SHL AX, 2 ;AX times 8
ADD AX, BX ;10 x AX

;multiply AX by 18 (10010)

SHL AX, 1 ;AX times 2


MOV BX, AX
SHL AX, 3 ;AX times 16
ADD AX, BX ;18 x AX
11

Shifting Instructions (cont.)


• SHR (shift right)
• shifts all the bits in the destination operand to the right one bit
• zeros fill vacated positions at the H.O. bit
• the L.O. bit shifts into the carry flag
• A quick way to divide by two (works for unsigned
numbers)
• Useful for unpacking data, e.g., suppose you want to
extract the two nibbles in the AL register, leaving the H.O.
nibble in AH and the L.O. nibble in AL:
MOV AH, AL ;get a copy of the H.O. nibble
SHR AH, 4 ;move H.O. to L.O. and clear H.O.
AND AL, 0Fh ;remove H.O. nibble from AL
12

Shifting Instructions (cont.)


• SAR (shift arithmetic right)
• shifts all the bits in the destination operand to the right one bit
replicating the H.O. bit
• the L.O. bit shifts into the carry flag
• Main purpose is to perform a signed division by some power of two
MOV AX, -15
SAR AX, 1 ; Result is -8
• In 80286 and later you can use SAR to sign extend one
register into another, e.g.,
MOV AH, AL
If AL contains 11110001
SAR AH, 8 then AH will contain sign bits extension
11111111 11110001
13

Shifting Instructions (cont.)

• RCL (rotate through carry left)


• rotates bits to the left, through the carry flag
• bit in the carry flag is written back into bit zero (on the right)
• ROL (rotate left)
• rotates bits to the left
• shifts operand’s H.O. bit into bit zero
• e.g., extract bit 10 to 14 in AX and leave these bits in 0 to 4
ROL AX, 6
AND AX, 1Fh
NOTE: There are two forms of rotate
1) use of immediate rotate count (8086, 8088 allow an immediate rotate of 1 only,
e.g., ROL AX, 1)
2) use of register CL to hold the rotate count
14

Shifting Instructions (cont.)

• RCR (rotate through carry right)


• rotates bits to the right, through the carry flag
• bit in the carry flag is written back into H.O. bit (on the left)

• ROR (rotate right)


• rotates bits to rights
• shifts operand’s bit zero into H.O. bit
15

Shifting Operations
Example
mov ax,3 ; Initial register values
mov bx,5

or ax,9 ; ax <- ax | 00001001 (bitwise OR)


and ax,10101010b ; ax <- ax & 10101010 (bitwise AND)
xor ax,0FFh ; ax <- ax ^ 11111111 (bitwise XOR)
neg ax ; ax <- (-ax) (2's complement)
not ax ; ax <- (~ax) (bitwise inversion)

shl ax,1 ; logical shift left by 1 bit


shr ax,1 ; logical shift right by 1 bit
rol ax,1 ; rotate left (LSB=MSB)
ror ax,1 ; rotate right (MSB=LSB)

mov cl,3 ; Use CL to shift 3 bits


shr ax,cl ; Divide AX by 8
shl bx,cl ; Multiply BX by 8
16

Simple Arithmetic Instructions


• ADD (addition): A += B
• Register addition, e.g., ADD AX, BX
• Immediate addition, e.g., ADD DL, 33h
• Memory to register addition, e.g., memory data added to AL:
MOV DI, OFFSET NUMB ;address NUMB
MOV AL, 0 ;clear sum
ADD AL, [DI] ;add NUMB
ADD AL, [DI + 1] ;add NUMB + 1

• NOTE: any ADD instruction modifies the contents of the


sign, zero, carry, auxiliary carry, parity, and overflow flags
17

Simple Arithmetic Instructions (cont.)

• INC (increment addition): A++, e.g., memory data added


to AL:
MOV DI, OFFSET NUMB ;address NUMB
MOV AL, 0 ;clear sum
ADD AL, [DI] ;add NUMB
INC DI
ADD AL, [DI] ;add NUMB + 1

NOTE: The increment instructions do not affect the carry


flag bit, while affecting ZF, OF, SF, AF, PF.
18

Simple Arithmetic Instructions (cont.)

• ADC (addition with carry) - functions as regular addition,


except the bit in the carry flag (C) is also added to the
result
• used mainly to add numbers that are wider than 16 bits (8086 -
80286) or wider than 32 bits in the 80386, 80486)
• Example(8086):
• addition of two 32-bit numbers (BX:AX) + (DX:CX):
ADD AX, CX
ADC BX, DX
19

Simple Arithmetic Instructions (cont.)


• SUB (subtraction): A -= B
• Register subtraction, e.g., SUB CL, BL
• Immediate subtraction, e.g.,
MOV CH, 22h
SUB CH, 44h Result is -34 (1101 1110)
Flags change:
Z = 0 (result not zero)
C = 1 (borrow)
A = 1 (half-borrow)
S = 1 (result negative)
P = 1 (even parity)
0 = 0 (no overflow)

• NOTE: any SUB instruction modifies the contents of the


sign, zero, carry, auxiliary carry, parity, and overflow flags
20

Simple Arithmetic Instructions (cont.)


• DEC (decrement subtraction): A--, subtracts a 1 from a
register or the contents of a memory location e.g., DEC
BH
NOTE: The decrement instructions do not affect the carry flag bit while affect others.

• SBB (subtract with borrow) functions as regular


subtraction, except the carry flag (C), which holds the
borrow, also subtracts from the difference
• used mainly to subtract numbers that are wider than 16 bits (8086 -
80286) or wider than 32 bits in the 80386, 80486)
• Example(8086):
• subtraction of two 32-bit numbers (BX:AX) - (SI:DI):
SUB AX, DI
SBB BX, SI
21

Overflow and Carries


• Carry
• indicates a carry after addition or a borrow after subtraction
• CF: carry flag (unsigned) {1 = CY (there is carry); 0 = NC (no carry)}
• e.g., 36,864 (9000h) + 36,864 (9000h) = 73,728 (12000h) > 65,535 (FFFFh) {OV, CY}
• carry is set when unsigned goes out of range (denotes an unsigned arithmetic overflow)

• Overflow
• condition that occurs when signed numbers are added or subtracted
• OF: overflow flag (signed) {1 = OV, 0 = NV}
• e.g., 20,480 (5000h) + 20,480 (5000h) = 40,960 (A000h) > 32,767 (7FFFh) {OV, NC}
• overflow is set when signed goes out of range (denotes a signed arithmetic overflow)
• Example: FFFFh + FFFFh = FFFEh {(-1) + (-1)} = -2; NV, CY
22

Overflows & Carries


Example
MOV AX, 19
ADD AX, 7 ; AX <- AX + 7 = 26

NEG AX ; AX <- -AX = -26 = FFE6h

MOV AX,0FFFEh ; 65534 ; Unsigned


ADD AX,3 ;

MOV AX,0FFFEh ; -2 ; Signed


ADD AX,3 ;
; CPU sets flags for both cases (signed and unsigned)

MOV BX,6
SUB BX,7

INC BX
23

Flag Settings

FLAG Name Description Notes

ZF Zero 1:ZR:Zero 1 indicates that the result was zero


0:NZ: Non-zero

CF Carry 1:CY Unsigned Math and shifting


0:NC Needed a carry or borrow

OF Overflow 1:OV Signed Math


0:NV Also (+) or (-) to be represented as
a valid two’s complement number

SF Sign Flag 1:NG: - MSB of result


0:PL: +

You might also like