Download presentation
Presentation is loading. Please wait.
Published byHerbert Harvey Modified over 9 years ago
1
ICS312 Set 6 Operands
2
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
3
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
4
Basic Operand Types (3) Examples. Identify the types of ALL operands used in each line of the following code:.DATA CRLF EQU 0AH, 0DH VAR1 DW 1200H, 56H, 0FFFFH MSG DB 'The answer is: ', '$‘.CODE MOV AX, @DATA MOV DX, AX MOV AX, OFFSET VAR1 ADD BH, 15 LEA BX, VAR1+2 MOV DX, OFFSET MSG MOV AH, 9 INT 21H MOV AX, 4C00H INT 21H
5
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] ; uses SI as an offset into the Code segment, ; instead of the Data segment mov dx, ds:[bp] ; uses BP as an offset into the Data segment, ; instead of the Stack segment.
6
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.
7
Indirect Operands (2) 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 movsum, al
8
.data Sum db 0 List db10h, 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)
9
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]
10
Example: code to display contents of an array (message) using only function 2 and the loop instruction:.data message db "Hello, World!", 0Ah, 0Dh count equ $-message ; length of message.code mov si, 0 ; initialize offset to 0 mov ah, 2 ; function to display a character on screen mov cx, count ; cx = message length L_top: mov dl, message[si] ; move character into dl int 21h ; display character inc si ; set si to offset of next character loop L_top ; repeat until count = 0 Note: instead of: mov si,0 … mov dl,message[si], we could have used: lea si,message … mov dl,[si]
11
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 ;Puts offset of the 08h into BX MOV BYTE PTR [BX], 2 ; Replace 08h by 02h INC BYTE PRT [BX] ; Increments the 02H to 03H
12
Textbook Reading (Jones): Chapter 10 Arrays
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.