MP 6
MP 6
MP 6
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]
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
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
Shifting Instructions
/SAL
9
Example
;multiply AX by 18 (10010)
Shifting Operations
Example
mov ax,3 ; Initial register values
mov bx,5
• 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
MOV BX,6
SUB BX,7
INC BX
23
Flag Settings