MP_Lect_5
MP_Lect_5
Lecture 5
Contents (Chapter 4: part 2)
Data Transfers, Addressing and Arithmetic
Data-Related Operators
Indirect Operands
Indexed Operands
Unconditional JMP and LOOP Instructions
Data-Related Operators and Directives
OFFSET Operator
PTR Operator
TYPE Operator
LENGTHOF Operator
SIZEOF Operator
(1) OFFSET Operator
OFFSET returns the distance in bytes, of a
label from the beginning of its enclosing
segment. (see examples in next slide).
To illustrate, the following figure shows a
variable named myByte inside the data
segment: offset
data segment:
myByte
; C++ version:
char array[1000];
char *p = array;
.data
array BYTE 1000 DUP(?)
.code
mov esi,OFFSET array ; ESI is p
(2) PTR Operator
Overrides the default type of a label (variable).
56 0001 myDouble + 1
12 0003 myDouble + 3
.data
varB BYTE 65h,31h,02h,05h
varW WORD 6543h,1202h
varD DWORD 12345678h
.code
mov ax,WORD PTR [varB+2] ; a.
mov bl,BYTE PTR varD ; b. 0502h
mov bl,BYTE PTR [varW+2] ; c. 78h
mov ax,WORD PTR [varD+2] ; d. 02h
mov eax,DWORD PTR varW ; e. 1234h
12026543h
(3) TYPE Operator
The TYPE operator returns the size, in bytes,
of a single element of a data declaration.
.data
var1 BYTE ?
var2 WORD ?
var3 DWORD ?
var4 QWORD ?
.code
mov eax,TYPE var1 ; 1
mov eax,TYPE var2 ; 2
mov eax,TYPE var3 ; 4
mov eax,TYPE var4 ; 8
(4) LENGTHOF Operator
The LENGTHOF operator counts the
number of elements in a single data
declaration (array).
.data LENGTHOF
byte1 BYTE 10,20,30 ; 3
array1 WORD 30 DUP(?),0,0 ; 32
array2 WORD 5 DUP(3 DUP(?)) ; 15
array3 DWORD 1,2,3,4 ; 4
digitStr BYTE "12345678",0 ; 9
.code
mov ecx,LENGTHOF array1 ; 32
(5) SIZEOF Operator
The SIZEOF operator returns a value that is
equivalent to multiplying LENGTHOF by TYPE.
.data SIZEOF
byte1 BYTE 10,20,30 ; 3
array1 WORD 30 DUP(?),0,0 ; 64
array2 WORD 5 DUP(3 DUP(?)) ; 30
array3 DWORD 1,2,3,4 ; 16
digitStr BYTE "12345678",0 ; 9
.code
mov ecx,SIZEOF array1 ; 64
Spanning Multiple Lines (1 of 2)
A data declaration covers multiple lines if each
line (except the last) ends with a comma.
.data
val1 BYTE 10h,20h,30h
.code
mov esi,OFFSET val1
mov al,[esi] ; dereference ESI (AL = 10h)
inc esi
mov al,[esi] ; AL = 30h
[3] Indirect Operands (2 of 2)
Alternate format:
ptrVar DWORD OFFSET arrayW
Section Review
4.4.5
Unconditional JMP and LOOP
Instructions
JMP Instruction
LOOP Instruction
LOOP Example
Summing an Integer Array
Copying a String
JMP Instruction
• JMP is an unconditional jump to a label that is
usually within the same procedure.
• Syntax: JMP target (EIP: Exteneded Instruction Pointer Reg 3
mov ecx,0
How many times will the loop X2:
execute? inc ax
4,294,967,296 loop X2
Nested Loop
If you need to code a loop within a loop, you
must save the outer loop counter's ECX value.
In the following example, the outer loop
executes 100 times, and the inner loop 20 times.
.data
count DWORD ?
.code
mov ecx,100 ; set outer loop count
L1:
mov count,ecx ; save outer loop count
mov ecx,20 ; set inner loop count
L2: .
.
loop L2 ; repeat the inner loop
mov ecx,count ; restore outer loop
count
loop L1 ; repeat the outer loop
Summing an Integer Array
The following code calculates the sum of an
array of 16-bit integers.
.data
intarray WORD 100h,200h,300h,400h
.code
mov edi,OFFSET intarray ; address of intarray
mov ecx,LENGTHOF intarray ; loop counter
mov ax,0 ; zero the accumulator
L1:
add ax,[edi] ; add an integer
add edi,TYPE intarray ; or(2),point to next
integer
loop L1 ; repeat until ECX = 0
Copying a String
The following code copies a string from source
to target: (strcpy function in C++)
.data
source BYTE "This is the source string",0 good use
of SIZEOF
target BYTE SIZEOF source DUP(0)
.code
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter (26)
L1:
mov al,source[esi] ; get char from source
mov target[esi],al ; store it in the target
inc esi ; move to next character
loop L1 ; repeat for entire string
Section Review
4.5.5
Programming Exercises