Set 6 - fleder44.net

advertisement
ICS312 Set 6
Operands
Basic Operand Types (1)
•Register Operands. An operand that refers to a register.
MOV AX, BX ; moves contents of register BX to register AX.
•Immediate Operands. A constant is an immediate operand.
MOV AX, 5
; moves the numeric value 5 into AX
MOV AX, @DATA ; moves the constant represented by @data into AX
•Direct Operands. A variable name that represents a memory address
is a direct (memory) operand.
MOV BX, NUM
; moves the contents of the memory variable
; NUM into BX
Basic Operand Types (2)
•A variable name +/- a numeric offset
MOV BX, NUM+2 ; moves 2nd byte following NUM into BX
•Useful for array addressing
•OFFSET operator: returns the 16-bit offset (address) of a memory
variable.
MOV BX, OFFSET NUM ; puts offset address of NUM into BX
•LEA instruction: stores the address of a memory variable in a
general register. Same effect as instruction above obtained by:
LEA BX, NUM ; puts offset address of NUM into BX
Basic Operand Types (3)
Examples.
following
.DATA
CRLF
VAR1
MSG
Identify the types of ALL operands used in each line of the
code:
EQU 0AH, 0DH
DW 1200H, 56H, 0FFFFH
DB 'The answer is: ', '$‘
.CODE
MOV
MOV
MOV
ADD
LEA
MOV
MOV
INT
MOV
INT
AX,
DX,
AX,
BH,
BX,
DX,
AH,
21H
AX,
21H
@DATA
AX
OFFSET VAR1
15
VAR1+2
OFFSET MSG
9
4C00H
•An indirect operand is an offset of a variable, which is contained in a
register. The register acts as a pointer to the variable involved.
•The 16-bit registers used for indirect addressing are: SI, DI, BX, and BP
•Any of the general purpose 32-bit registers may be used.
•Default segments:
For SI, DI, and BX the variable involved is assumed to be in the Data segment.
For BP, the variable involved is assumed to be in the stack segment.
• Segment Overrides: Use a Segment Override Prefix to designate
which segment to use when the default segment is not appropriate:
mov al, cs:[si]
mov dx, ds:[bp]
; uses SI as an offset into the Code segment,
;
instead of the Data segment
; uses BP as an offset into the Data segment,
;
instead of the Stack segment.
An example of the use of an indirect operand is:
mov ax, [BX]
Note that BX is enclosed within square brackets.
If BX contains e.g. 4, then the no. moved into ax is
not 4, but the value of the word located at offset 4 in
the data segment. E.g. if the data segment was:
.DATA
x
dw 100 ; x is at offset 0
y
dw 250 ; y is at offset 2
z
dw 300 ; z is at offset 4
k
dw 99
; k is at offset 6
then the no. moved into ax would be 300.
Example: To add up the items in List & store the answer in
sum.
.data
sum db 0
List db 10h, 20h, 30h, 40h
Method 1: increment bx to advance to each value
.code
lea bx, List
;in this example, sets bx to 1
mov al, [bx]
; al = 10h
inc bx
; bx is 2 and so points to 20h
add al, [bx]
; al = 30h
inc bx
; bx points to 30h
add al, [bx]
; al = 60h
inc bx
; bx points to 40h
add al, [bx]
; al = 0A0h
mov
sum, al
.data
Sum db 0
List db 10h, 20h, 30h, 40h
Method 2: use bx with displacements to
access each value
.code
lea
bx, List
mov
al, [bx]
; AL = 10h
add
al, [bx+1]
; AL = 30h
add
al, [bx+2]
; AL = 60h
add
al, [bx+3]
; AL = 0A0h
mov
sum, al
; store sum
in next memory location (sum)
Examples of different indirect operand formats
mov ax, [bx]
mov dx, [bx+10]
mov cx, array[bx]
; same as mov cx [bx+array]
; array is a constant equal to the
;
array’s offset
mov ax, [di]
mov dx, [di + 2*5]
mov cx, [array+si]
•Example:
code to display contents of an array (message)
using only function 2 and the
loop
instruction:
.data
message
count
db "Hello, World!", 0Ah, 0Dh
equ $-message
.code
mov
mov
mov
si, 0
ah, 2
cx, count
L_top:
mov
int
inc
loop
dl, message[si]
21h
si
L_top
; length of message
; initialize offset to 0
; function to display a character on screen
; cx = message length
;
;
;
;
move character into dl
display character
set si to offset of next character
repeat until count = 0
Note: instead of:
mov si,0 … mov dl,message[si],
we could have used: lea si,message … mov dl,[si]
Examples requiring use of PTR
ByteVal DB 05h, 06h, 08h, 09h, 0Ah, 0Dh, '$'
WordVal DW 5510h, 6620h, 0A0Dh, '$'
LEA BX, WordVal
; or: MOV BX, OFFSET WordVal
; BX points to WordVal
MOV WORD PTR [BX+2], 7 ; Replaces 6620h by 0007h
The PTR operator is used to explicitly state the size
of an operand that would otherwise be ambiguous, in
particular when the first operand is indirect,
and the second is immediate
LEA BX, ByteVal+2
MOV BYTE PTR [BX], 2
INC
BYTE PRT [BX]
;Puts offset of the 08h into BX
; Replace 08h by 02h
; Increments the 02H to 03H
Textbook Reading (Jones):
•Chapter 10
Arrays
Download