Title: ECE291 Computer Engineering II Lecture 4
1ECE291Computer Engineering IILecture 4
- Josh Potts
- University of Illinois at Urbana- Champaign
2Outline
- Logic instructions
- Shifting instructions
- Arithmetic operations
- Overflow and carries
- Important flags setting
3Memory Access Example
- mov si,myvar2 use SI as a pointer to
myvar2 (equiv C code SImyvar2 ) - mov ax,si read memory at myvar2
((myvar2)) (indirect reference) - mov bx,ece291msg BX is a pointer to a
string (equiv C code BXece291msg) - dec BYTE bx1 make that 'C' a 'B' !!!!
-
- mov si, 1 Use SI as an index
- inc ece291msgSI inc SI 8 inc
9
- SEGMENT code
- myvar1 DW 01234h define word variable
(value1234h) - myvar2 DW 01234 define word variable
(value1234d 4D2) - myvar3 RESW define word variable (value
uncertain) - myvar4 DW 01BCDh
- ece291msg DB 'ECE291 is great'
- ..start
- mov ax,cs set up data segment
- mov ds,ax DSCS any memory reference we make
is assumed to reside in the DS segment - mov ax,myvar2 AX lt- myvar2 mov
ax,2
4Memory Access Example (cont.)
- Memory can be addressed using four registers
- SI -gt Assumes DS
- DI -gt Assumes DS
- BX -gt Assumes DS
- BP -gt Assumes SS !!!
- Examples
- mov ax,bx ax lt- word in memory pointed to by
BX - mov al,bx al lt- byte in memory pointed to by
BX - mov ax,si ax lt- word pointed to by SI
- mov ah,si ah lt- byte pointed to by SI
- mov cx,di cx lt- word pointed to by DI
- mov ax,bp ax lt- SSBP STACK OPERATION!
- In addition, BXSI and BXDI are allowed
- mov ax,bxsi
- mov ch,bxdi
- Furthermore, a fixed 8-bit or 16-bit
- displacement from the registers!
-
- mov ax,23h ax lt- word at DS0023
- mov ah,bx5 ah lt- byte at DS(BX5)
- mov ax,bxsi107 ax lt- word at
DS(BXSI107) - mov ax,bxdi47 ax lt- word at
DS(BXDI47) - REMEMBER memory to memory moves are
- ILLEGAL!!!
- mov bx,si ILLEGAL
- mov di,si ILLEGAL (use movsw)
- Special case stack operations!
- pop word myvar myvar lt- SSSP
5Flag Register
6Logic 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
7Logic 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 same as AND instruction but
doesnt change al - 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, 0fh
- OR forces selected bits to one or cl, 0fh
8Shifting Instructions
9Shifting 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) use of
immediate shift count (8086, 8088 allow an
immediate shift of 1 only,
e.g., shl ax, 1)2) use of register CL to
hold the shift count
10Example
- multiply AX by decimal 10 (1010) (multiply by 2
and 8, then add results) - shl ax, 1 AX times 2
- mov bx, ax save 2AX
- shl ax, 2 2(original)AX 4
8(original)AX - add ax, bx 2AX 8AX 10AX
- multiply AX by decimal 18 (10010) (multiply by 2
and 16, then add results) - shl ax, 1 AX times 2
- mov bx, ax save 2AX
- shl ax, 3 2(original)AX times 8
16(original)AX - add ax, bx 2AX 16AX 18AX
11Shifting 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
12Shifting 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 e.g., - 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
- sar ah, 7
If AL contains 11110001 then
AH will contain sign bits extension 11111111
11110001
13Shifting 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 operands H.O. bit into bit zero (the L.O.
bit) - 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
14Shifting 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 right
- shifts operands L.O. bit into H.O. bit
15Shifting OperationsExample
- mov ax,3 Initial register values AX 0000
0000 0000 0011 - mov bx,5 BX 0000 0000 0000 0101
- or ax,9 ax lt- ax 0000 1001 AX 0000 0000
0000 1011 - and ax,10101010b ax lt- ax 1010 1010 AX
0000 0000 0000 1010 - xor ax,0FFh ax lt- ax 1111 1111 AX 0000
0000 1111 0101 - neg ax ax lt- (-ax) AX 1111 1111 0000 1011
- not ax ax lt- (ax) AX 0000 0000 1111 0100
- Or ax,1 ax lt- ax 0000 0001 AX 0000 0000
1111 0101 - shl ax,1 logical shift left by 1 bit AX
0000 0001 1110 1010 - shr ax,1 logical shift right by 1 bit AX
0000 0000 1111 0101 - ror ax,1 rotate left (LSBMSB) AX 1000 0000
0111 1010 - rol ax,1 rotate right (MSBLSB) AX 0000
0000 1111 0101 - mov cl,3 Use CL to shift 3 bits CL 0000
0011 - shr ax,cl Divide AX by 8 AX 0000 0000 0001
1110 - mov cl,3 Use CL to shift 3 bits CL 0000
0011 - shl bx,cl Multiply BX by 8 BX 0000 0000
0010 1000
16Simple 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, NUMB address of 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
17Simple Arithmetic Instructions (cont.)
- INC (increment addition) A
- mov di, NUMB address of NUMB
- mov al, 0 clear sum
- add al, di add NUMB
- inc di di di 1
- add al, di add NUMB 1
- NOTE The increment instruction does not affect
the carry flag bit.
18Simple 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, Pentium) - Example
- addition of two 32-bit numbers (BXAX) (DXCX)
- add ax, cx
- adc bx, dx
19Simple Arithmetic Instructions (cont.)
- SUB (subtraction) A - B
- Register subtraction, e.g., sub cl, bl
- Immediate subtraction, e.g.,
- mov ch, 22h
- sub ch, 34h
- NOTE any SUB instruction modifies the contents
of the sign, zero, carry, auxiliary carry,
parity, and overflow flags
Result is -12 (1110 1110) Flags change Z 0
(result not zero) C 1 (borrow) A 1
(half-borrow) S 1 (result negative) P 0 (even
parity) 0 0 (no overflow)
20Simple 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 instruction does not affect
the carry flag bit. - 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, Pentium) - Example
- subtraction of two 32-bit numbers (BXAX) -
(SIDI) - sub ax, di
- sbb bx, si
21Overflow 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) gt 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) gt 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
22Overflows Carries Example
- mov ax,0fffeh 65534 interpreted as unsigned
- add ax,3 C 1, O 0 --- Unsigned
Overflow Condition - mov ax,0FFFEh -2 interpreted as signed
- add ax,3 C 1, O 0 --- Okay as signed
- mov bx,07FFFh 32767 interpreted as signed
- add bx,1 C 0, O 1 --- Signed Overflow
Condition - mov bx,07FFFh 32767 interpreted as unsigned
- add bx,1 C 0, O 1 --- Okay as unsigned
- Mov ax,07h 7 interpreted as either signed or
unsigned - Add ax,03h C 0, O 0 --- Okay as either
23Flag Setting
FLAG Name Description Notes ZF Zero 1ZRZero
1 indicates that the result was zero 0NZ
Non-zero CF Carry 1CY Unsigned Math and
shifting 0NC Needed a carry or
borrow OF Overflow 1OV Signed
Math 0NV Also () or (-) to be represented
as a valid twos complement number SF Sign
Flag 1NG - MSB of result 0PL