ECE291 Computer Engineering II Lecture 4 Josh Potts University of Illinois at Urbana- Champaign Outline • • • • • • Memory Examples Logic instructions Shifting instructions Arithmetic operations Overflow and carries Important flags setting Josh Potts ECE291 Memory Access Example myvar1 DW 01234h myvar2 DW 01234 myvar3 DW ? myvar4 DW 01BCDh ; define word variable ; (value=1234h) ; define word variable ;(value=1234d = 4D2) ; define word variable ;(value uncertain) mov si,OFFSET myvar2 mov ax,[si] ; use SI as a pointer to myvar2 ; (equiv C code: SI=&myvar2 ) ; read memory at myvar2 (*(&myvar2)) ; (indirect reference) mov bx,OFFSET ece291msg ; BX is a pointer to a string ; (equiv C code: BX=&ece291msg) ece291msg DB 'ECE291 is great' dec BYTE PTR [bx+1] ; make that 'C' a 'B' !!!! start: mov ax,cs mov ds,ax ; set up data segment ; DS=CS mov si, 1 inc ece291msg[SI] ; any memory reference we make is assumed to reside in ;the DS segment mov ax,myvar2 ; AX <- myvar2 ; == mov ax,[offset myvar2] ; == mov ax,[2] Josh Potts ECE291 ; Use SI as an index ; == inc [SI + offset ece291msg] ; == inc [SI + 8] ; == inc [9] Memory Access Example (cont.) ;Memory can be addressed using four registers: ; SI -> Assumes DS ; DI -> Assumes DS ; BX -> Assumes DS ; BP -> Assumes SS !!! (this is why it isn't frequently used) ; 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] ; di <- word pointed to by DI mov ax,[bp] ; AX <- SS:[BP] STACK OPERATION!!!! ; Furthermore, a fixed 8-bit or 16-bit displacement from the index and base registers is allowed. mov ax,[23h] ; ax <- word in memory DS:0023 mov ah,[bx+5] ; ah <- byte in memory 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 myvar ; myvar <- SS:[SP] ; In addition, BX+SI and BX+DI are allowed: mov ax,[bx+si] mov ch,[bx+di] Josh Potts ECE291 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 80286 Josh Potts 80386, 80486DX 80486SX ECE291 Logic Instructions • Logic instructions operate on a bit-by-bit basis NOT: AND: OR: XOR: A =~A A &= B A |= B 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 Josh Potts 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 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 Josh Potts ECE291 Shifting Instructions Josh Potts 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 OR AH, 4 AL, AH ;requires 80286 or later 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) Josh Potts ECE291 Example ;multiply AX by decimal 10 (1010) SHL AX, 1 MOV BX, AX SHL AX, 2 ADD AX, BX (Same as AX times 2+8 – associate prop) ;AX times 2 ;AX times 8 ;10 x AX ;multiply AX by 18 (10010) SHL MOV SHL ADD Josh Potts AX, 1 BX, AX AX, 3 AX, BX ;AX times 2 ;AX times 16 ;18 x 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 AL register, leaving the H.O. nibble in AH and the L.O. nibble in AL: MOV SHR AND Josh Potts AH, AL AH, 4 AL, 0Fh ;get a copy of the H.O. nibble ;move H.O. to L.O. and clear H.O. ;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 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 SAR Josh Potts AH, AL AH, 8 If then ECE291 AL contains 11110001 AH will contain sign bits extension 11111111 11110001 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 Josh Potts 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 rights – shifts operand’s L.O. bit into bit zero Josh Potts ECE291 Shifting Operations Example mov ax,3 mov bx,5 ; Initial register values or ax,9 and ax,10101010b xor ax,0FFh neg ax not ax ; ; ; ; ; ax <- ax | 00001001 (bitwise OR) ax <- ax & 10101010 (bitwise AND) ax <- ax ^ 11111111 (bitwise XOR) ax <- (-ax) (2's complement) ax <- (~ax) (bitwise inversion) shl ax,1 shr ax,1 rol ax,1 ror ax,1 ; ; ; ; logical shift left by 1 bit logical shift right by 1 bit rotate left (LSB=MSB) rotate right (MSB=LSB) mov cl,3 shr ax,cl shl bx,cl ; Use CL to shift 3 bits ; Divide AX by 8 ; Multiply BX by 8 Josh Potts 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, 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 Josh Potts ECE291 Simple Arithmetic Instructions (cont.) • INC (increment addition): A++, e.g., memory data added to AL: MOV MOV ADD INC ADD DI, OFFSET NUMB AL, 0 AL, [DI] DI AL, [DI] ;address NUMB ;clear sum ;add NUMB ;add NUMB + 1 NOTE: The increment instructions do not affect the carry flag bit. Josh Potts 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) • Example: – addition of two 32-bit numbers (BX:AX) + (DX:CX): ADD AX, CX ADC BX, DX Josh Potts ECE291 Simple Arithmetic Instructions (cont.) • SUB (subtraction): A -= B – Register subtraction, e.g., SUB CL, BL – Immediate subtraction, e.g., MOV CH, 22h Result is -34 (1101 1110) Flags change: SUB CH, 44h 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 Josh Potts 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 increment instructions do 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) • Example: – subtraction of two 32-bit numbers (BX:AX) - (SI:DI): SUB AX, DI SBB BX, SI Josh Potts 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 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 Josh Potts ECE291 Overflows & Carries Example MOV AX, 19 ADD AX, 7 ; AX <- AX + 7 = 26 NEG AX ; AX <- -AX = -26 = 0FFE6h MOV AX,0FFFEh ADD AX,3 ; 65534 ; Unsigned ; 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 Josh Potts ECE291 Flag Settings FLAG Name Description ZF Zero 1:ZR:Zero 0:NZ: Non-zero 1 indicates that the result was zero CF Carry 1:CY 0:NC Unsigned Math and shifting Needed a carry or borrow OF Overflow 1:OV 0:NV Signed Math Also (+) or (-) to be represented as a valid two’s complement number SF Sign Flag 1:NG: 0:PL: + MSB of result Josh Potts ECE291 Notes