- 81 Views
- Uploaded on
- Presentation posted in: General

ECE291 Computer Engineering II Lecture 4

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

ECE291Computer Engineering IILecture 4

Josh Potts

University of Illinois at Urbana- Champaign

- Logic instructions
- Shifting instructions
- Arithmetic operations
- Overflow and carries
- Important flags setting

ECE291

SEGMENT code

myvar1DW 01234h; define word variable; (value=1234h)

myvar2DW 01234; define word variable; (value=1234d = 4D2)

myvar3RESW; define word variable; (value uncertain)

myvar4DW 01BCDh

ece291msg DB 'ECE291 is great'

..start:

mov ax,cs; set up data segment

mov ds,ax; DS=CS; any memory reference we make is; assumed to reside in the DS segment

mov ax,[myvar2]; AX <- [myvar2]; == mov ax,[2]

mov si,myvar2; use SI as a pointer to myvar2; (equiv C code: SI=&myvar2 )

mov ax,[si]; read memory at myvar2; (*(&myvar2)); (indirect reference)

mov bx,ece291msg; BX is a pointer to a string; (equiv C code:;BX=&ece291msg)

dec BYTE [bx+1]; make that 'C' a 'B' !!!!

mov si, 1; Use SI as an index

inc [ece291msg+SI]; == inc [SI + 8]; == inc [9]

ECE291

;Memory can be addressed using four registers:

; SI -> Assumes DS

; DI -> Assumes DS

; BX -> Assumes DS

; BP -> Assumes SS !!!

; Examples:

mov ax,[bx]; ax <- word in memory pointed to by BX

mov al,[bx]; al <- byte in memory pointed to by BX

mov ax,[si] ; ax <- word pointed to by SI

mov ah,[si] ; ah <- byte pointed to by SI

mov cx,[di] ; cx <- word pointed to by DI

mov ax,[bp] ; ax <- [SS:BP]STACK OPERATION!

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

mov ax,[bx+si]

mov ch,[bx+di]

; Furthermore, a fixed 8-bit or 16-bit

; displacement from the registers!:

mov ax,[23h] ; ax <- word at DS:0023

mov ah,[bx+5] ; ah <- byte at DS:(BX+5)

mov ax,[bx+si+107]; ax <- word at; DS:(BX+SI+107)

mov ax,[bx+di+47]; ax <- word at; DS:(BX+DI+47)

; 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] <- SS:[SP]

ECE291

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

ECE291

- 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

ECE291

- 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 doesn’t 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 zeroand cl, 0fh
- OR forces selected bits to oneorcl, 0fh

ECE291

ECE291

- 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
shlah, 4;requires 80286 or later

oral, 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

ECE291

;multiply AX by decimal 10 (1010) (multiply by 2 and 8, then add results)

shlax, 1;AX times 2

mov bx, ax;save 2*AX

shlax, 2;2*(original)AX * 4 = 8*(original)AX

addax, bx;2*AX + 8*AX = 10*AX

;multiply AX by decimal 18 (10010) (multiply by 2 and 16, then add results)

shlax, 1;AX times 2

mov bx, ax;save 2*AX

shlax, 3;2*(original)AX times 8 = 16*(original)AX

addax, bx;2*AX + 16*AX = 18*AX

ECE291

- 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 ALregister, 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

shrah, 4;move H.O. to L.O. and clear H.O.

andal, 0fh;remove H.O. nibble from AL

ECE291

- 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.,
movax, -15

sarax, 1; Result is -8

- In 80286 and later you can use SAR to sign extend one register into another, e.g.,
movah, al

sarah, 7

If AL contains 11110001

then AH will contain sign bits extension

1111111111110001

ECE291

- 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 (the L.O. bit)
- e.g., extract bit 10 to 14 in AX and leave these bits in 0 to 4
rolax, 6

andax, 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

ECE291

- 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 operand’s L.O. bit into H.O. bit

ECE291

mov ax,3; Initial register valuesAX = 0000 0000 0000 0011

mov bx,5;BX = 0000 0000 0000 0101

or ax,9; ax <- ax | 0000 1001AX = 0000 0000 0000 1011

and ax,10101010b; ax <- ax & 1010 1010AX = 0000 0000 0000 1010

xor ax,0FFh; ax <- ax ^ 1111 1111AX = 0000 0000 1111 0101

neg ax; ax <- (-ax)AX = 1111 1111 0000 1011

not ax; ax <- (~ax)AX = 0000 0000 1111 0100

Or ax,1; ax <- ax | 0000 0001AX = 0000 0000 1111 0101

shl ax,1; logical shift left by 1 bitAX = 0000 0001 1110 1010

shr ax,1; logical shift right by 1 bitAX = 0000 0000 1111 0101

ror ax,1; rotate left (LSB=MSB)AX = 1000 0000 0111 1010

rol ax,1; rotate right (MSB=LSB)AX = 0000 0000 1111 0101

mov cl,3; Use CL to shift 3 bitsCL = 0000 0011

shr ax,cl; Divide AX by 8AX = 0000 0000 0001 1110

mov cl,3; Use CL to shift 3 bitsCL = 0000 0011

shl bx,cl; Multiply BX by 8BX = 0000 0000 0010 1000

ECE291

- 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:
movdi, NUMB;address of NUMB

moval, 0;clear sum

addal, [di];add [NUMB]

addal, [di + 1];add [NUMB + 1]

- NOTE: any ADD instruction modifies the contents of the sign, zero, carry, auxiliary carry, parity, and overflow flags

ECE291

- INC (increment addition): A++
movdi, NUMB;address of NUMB

moval, 0;clear sum

addal, [di];add [NUMB]

incdi;di = di + 1

addal, [di];add [NUMB + 1]

NOTE: The increment instruction does not affect the carry flag bit.

ECE291

- 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):
addax, cx

adcbx, dx

- addition of two 32-bit numbers (BXAX) + (DXCX):

ECE291

- SUB(subtraction): A -= B
- Register subtraction, e.g., sub cl, bl
- Immediate subtraction, e.g.,
movch, 22h

subch, 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)

ECE291

- 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):
subax, di

sbbbx, si

- subtraction of two 32-bit numbers (BXAX) - (SIDI):

ECE291

- 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 unsignedgoes 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 signedgoes out of range (denotes a signed arithmetic overflow)
- Example: FFFFh + FFFFh = FFFEh {(-1) + (-1)} = -2; NV, CY

ECE291

movax,0fffeh; 65534 interpreted as unsigned

addax,3 ; C = 1, O = 0 --- Unsigned Overflow Condition

mov ax,0FFFEh ; -2 interpreted as signed

addax,3; C = 1, O = 0 --- Okay as signed

movbx,07FFFh; 32767 interpreted as signed

addbx,1; C = 0, O = 1 --- Signed Overflow Condition

movbx,07FFFh; 32767 interpreted as unsigned

addbx,1; C = 0, O = 1 --- Okay as unsigned

Movax,07h; 7 interpreted as either signed or unsigned

Addax,03h; C = 0, O = 0 --- Okay as either

ECE291

FLAGNameDescriptionNotes

ZFZero1:ZR:Zero1 indicates that the result was zero

0:NZ: Non-zero

CFCarry1:CYUnsigned Math and shifting

0:NCNeeded a carry or borrow

OFOverflow1:OVSigned Math

0:NVAlso (+) or (-) to be represented as

a valid two’s complement number

SFSign Flag1:NG: -MSB of result

0:PL: +

ECE291