ICS 312 Set 6 Operands Basic Operand Types
ICS 312 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 2 nd 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 VAR 1 MSG Identify the types of ALL operands used in each line of the code: EQU 0 AH, 0 DH DW 1200 H, 56 H, 0 FFFFH DB 'The answer is: ', '$‘ . CODE MOV MOV ADD LEA MOV INT AX, DX, AX, BH, BX, DX, AH, 21 H AX, 21 H @DATA AX OFFSET VAR 1 15 VAR 1+2 OFFSET MSG 9 4 C 00 H
Indirect Operands (1) • An indirect operand is a register that contains the (offset) memory address, which means the register is acting as a pointer to a variable (or procedure). • 16 -bit registers for used indirect addressing: SI, DI, BX, and BP • 32 -bit registers for used indirect addressing: any of the general purpose 32 -bit registers may be used. • Default segments: For SI, DI, and BX the DS is the default base segment for the address. For BP, the SS is the default base segment for the address. • 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 CS ; uses BP as an offset into the DS, ; instead of the SS.
Indirect Operands (2) • Example: To add up the items in List & store the answer in sum. . data List db 10 h, 20 h, 30 h, 40 h sum db 0 ; ; method 1: increment bx to advance to each value. code lea bx, List mov al, [bx] ; AL = 10 h inc bx ; BX points to 20 h add al, [bx] ; AL = 30 h inc bx ; BX points to 30 h add al, [bx] ; AL = 60 h inc bx ; BX points to 40 h add al, [bx] ; AL = 0 A 0 h mov sum, al
Indirect Operands (3) List Sum . data db db 10 h, 20 h, 30 h, 40 h 0 ; ; method 2: use bx with displacements to access each value. code lea bx, List mov al, [bx] ; AL = 10 h add al, [bx+1] ; AL = 30 h add al, [bx+2] ; AL = 60 h add al, [bx+3] ; AL = 0 A 0 h mov sum, al ; store sum in next memory location (sum)
Indirect Operands (4). Examples of different formats: mov ax, [bx] mov dx, [bx+10] mov cx, array[bx] ; same as mov cx [bx+array] ; array is a constant equal to array’s offset mov ax, [di] mov dx, [di + 2*5] mov cx, [array+si]
Indirect Operands (5) • Example: code to display contents of an array (message) using only function 2 and the loop instruction: . data message count db "Hello, World!", 0 Ah, 0 Dh equ $-message . code mov mov si, 0 ah, 2 cx, count L_top: mov int inc loop dl, message[si] 21 h si L_top ; length of message ; initialize offset to 0 ; function to display a character on screen ; cx = message length ; ; move current value into dl display character next offset position in array 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 Byte. Val DB 05 h, 06 h, 08 h, 09 h, 0 Ah, 0 Dh, '$' Word. Val DW 5510 h, 6620 h, 0 A 0 Dh, '$' LEA BX, Word. Val ; or: MOV BX, OFFSET Word. Val ; BX points to Word. Val MOV WORD PTR [BX+2], 7 ; Replaces 6620 h by 0007 h 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, Byte. Val+2 MOV BYTE PTR [BX], 2 INC BYTE PRT [BX] ; Puts offset of the 08 h into BX ; Replace 08 h by 02 h ; Increments the 02 H to 03 H
Textbook Reading (Jones): • Chapter 10 Arrays
- Slides: 11