Loading in 5 sec....

ECE291 Computer Engineering II Lecture 4PowerPoint Presentation

ECE291 Computer Engineering II Lecture 4

- By
**matia** - Follow User

- 89 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' ECE291 Computer Engineering II Lecture 4' - matia

**An Image/Link below is provided (as is) to download presentation**

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

Presentation Transcript

Outline

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

ECE291

myvar1 DW 01234h; define word variable ; (value=1234h)

myvar2 DW 01234; define word variable ; (value=1234d = 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 ; 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]

Memory Access ExampleECE291

;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]

Memory Access Example (cont.)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

Flag RegisterECE291

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

ECE291

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 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 zero and cl, 0fh
- OR forces selected bits to one or cl, 0fh

ECE291

Shifting Instructions

ECE291

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

Example

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

shl ax, 1 ;AX times 2

mov bx, ax ;save 2*AX

shl ax, 2 ;2*(original)AX * 4 = 8*(original)AX

add ax, bx ;2*AX + 8*AX = 10*AX

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

shl ax, 1 ;AX times 2

mov bx, ax ;save 2*AX

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

add ax, bx ;2*AX + 16*AX = 18*AX

ECE291

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

shr ah, 4 ;move H.O. to L.O. and clear H.O.

and al, 0fh ;remove H.O. nibble from AL

ECE291

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

1111111111110001

ECE291

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 (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

ECE291

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

ECE291

Shifting OperationsExample

mov ax,3 ; Initial register values AX = 0000 0000 0000 0011

mov bx,5 ; BX = 0000 0000 0000 0101

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

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

xor ax,0FFh ; ax <- ax ^ 1111 1111 AX = 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 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 (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 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

ECE291

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

ECE291

Simple 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.

ECE291

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

- Example:
- addition of two 32-bit numbers (BXAX) + (DXCX):
add ax, cx

adc bx, dx

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

ECE291

Simple 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)

ECE291

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

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

ECE291

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

Overflows & 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

ECE291

Flag Setting

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: +

ECE291

Download Presentation

Connecting to Server..