18 Data Related Operators
18 Data Related Operators
18 Data Related Operators
Addressing and Data
Related Operators
Computer Organization and Assembly Language
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 1
Indirect Addressing
Data Related Operators
Indirect Operands
Array Sum Example
Indexed Operands
Pointers
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 2
Data‐Related Operators and Directives
OFFSET Operator
PTR Operator
TYPE Operator
LENGTHOF Operator
SIZEOF Operator
LABEL Directive
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 3
OFFSET Operator
OFFSET returns the distance in bytes, of a label from the
beginning of its enclosing segment
¾Protected mode: 32 bits
¾Real mode: 16 bits
offset
data segment:
myByte
.data
bVal db ?
wVal dw ?
dVal dd ?
dVal2 dd ?
.code
mov si,OFFSET bVal ; SI = 4000
mov si,OFFSET wVal ; SI = 4001
mov si,OFFSET dVal ; SI = 4003
mov si,OFFSET dVal2 ; SI = 4007
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 5
Relating to C/C++
The value returned by OFFSET is a pointer. Compare
the following code written for both C++ and assembly
language:
; C++ version:
char array[1000];
char * p = array;
.data
array db 1000 DUP(?)
.code
mov si,OFFSET array ; SI is p
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 6
PTR Operator
Overrides the default type of a label (variable).
Provides the flexibility to access part of a variable.
.data
myDouble dd 12345678h
.code
mov ax,myDouble ; error – why?
56 0001 myDouble + 1
12 0003 myDouble + 3
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 8
PTR Operator (cont.)
PTR can also be used to combine elements of a
smaller data type and move them into a larger operand.
The CPU will automatically reverse the bytes.
.data
myBytes db 12h,34h,56h,78h
.code
mov ax,WORD PTR [myBytes] ; AX = 3412h
mov ax,WORD PTR [myBytes+2] ; AX = 7856h
mov eax,DWORD PTR myBytes ; EAX = 78563412h
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 9
Your Turn . . .
Write down the value of each destination operand:
.data
varB db 65h,31h,02h,05h
varW dw 6543h,1202h
varD dd 12345678h
.code
mov ax,WORD PTR [varB+2] ; a. 0502h
mov bl,BYTE PTR varD ; b. 78h
mov bl,BYTE PTR [varW+2] ; c. 02h
mov ax,WORD PTR [varD+2] ; d. 1234h
mov eax,DWORD PTR varW ; e. 12026543h
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 10
TYPE Operator
The TYPE operator returns the size, in bytes, of a single element of a
data declaration.
.data
var1 db ?
var2 dw ?
var3 dd ?
var4 dq ?
.code
mov ax,TYPE var1 ; 1
mov ax,TYPE var2 ; 2
mov ax,TYPE var3 ; 4
mov ax,TYPE var4 ; 8
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 11
LENGTHOF Operator
The LENGTHOF operator counts the number of
elements in a single data declaration.
.data LENGTHOF
byte1 db 10,20,30 ; 3
array1 dw 30 DUP(?),0,0 ; 32
array2 dw 5 DUP(3 DUP(?)) ; 15
array3 dd 1,2,3,4 ; 4
digitStr db "12345678",0 ; 9
.code
mov cx,LENGTHOF array1 ; 32
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 12
SIZEOF Operator
The SIZEOF operator returns a value that is equivalent
to multiplying LENGTHOF by TYPE.
.data SIZEOF
byte1 db 10,20,30 ; 3
array1 dw 30 DUP(?),0,0 ; 64
array2 dw5 DUP(3 DUP(?)) ; 30
array3 dd 1,2,3,4 ; 16
digitStr db "12345678",0 ; 9
.code
mov cx,SIZEOF array1 ; 64
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 13
Spanning Multiple Lines
A data declaration spans multiple lines if each line
(except the last) ends with a comma. The LENGTHOF
and SIZEOF operators include all lines belonging to the
declaration:
.data
array WORD 10,20,
30,40,
50,60
.code
mov ax,LENGTHOF array ; 6
mov bx,SIZEOF array ; 12
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 14
Spanning Multiple Lines (cont.)
In the following example, array identifies only the first
WORD declaration. Compare the values returned by
LENGTHOF and SIZEOF here to those in the
previous slide:
.data
array WORD 10,20
WORD 30,40
WORD 50,60
.code
mov ax,LENGTHOF array ; 2
mov bx,SIZEOF array ; 4
Data Related Operators Computer Organization and Assembly Language – NUCES Hina Anwar – slide 15