Upload
landry
View
42
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Today's topics. Data related operators More arrays Quiz #3. Data-Related Operators and Directives. OFFSET Operator PTR Operator TYPE Operator LENGTHOF Operator SIZEOF Operator LABEL Directive. OFFSET Operator. - PowerPoint PPT Presentation
Citation preview
Today's topicsToday's topics
Data related operatorsData related operators More arraysMore arrays
Quiz #3Quiz #3
Data-Related Operators and Data-Related Operators and DirectivesDirectives
OFFSET OperatorOFFSET Operator PTR OperatorPTR Operator TYPE OperatorTYPE Operator LENGTHOF OperatorLENGTHOF Operator SIZEOF OperatorSIZEOF Operator LABEL DirectiveLABEL Directive
OFFSET OperatorOFFSET Operator
OFFSET returns OFFSET returns the distance in bytes, of a label the distance in bytes, of a label from the beginning of its enclosing segmentfrom the beginning of its enclosing segment Protected mode: 32 bitsProtected mode: 32 bits Real mode: 16 bitsReal mode: 16 bits
For now, we use only protected mode (default)
OFFSET ExamplesOFFSET Examples
.databVal BYTE ?wVal WORD ?dVal DWORD ?dVal2 DWORD ?.code
...mov esi,OFFSET bVal ; ESI = 00404000mov esi,OFFSET wVal ; ESI = 00404001mov esi,OFFSET dVal ; ESI = 00404003mov esi,OFFSET dVal2 ; ESI = 00404007
Assume that the data segment begins at 00404000h:
Relating to C/C++Relating to C/C++
; C++ version:char list[1000];char* p = list;
The value returned by OFFSET is a pointer. Compare the following code written for both C++ and assembly language:
.datalist BYTE 1000 DUP(?).code
...mov esi,OFFSET list ; ESI is used like p
PTR OperatorPTR Operator
.datamyDouble DWORD 12345678h.code
...mov ax,myDouble ; error – why?
mov ax,WORD PTR myDouble ; loads 5678h
mov WORD PTR myDouble,1357h ; saves 1357h
Overrides the default type of a label (variable). Provides the flexibility to access part of a variable.
PTR Operator ExamplesPTR Operator Examples.datamyDouble DWORD 12345678h
mov al,BYTE PTR myDouble ; AL = 78hmov al,BYTE PTR [myDouble+1] ; AL = 56hmov al,BYTE PTR [myDouble+2] ; AL = 34hmov ax,WORD PTR myDouble ; AX = 5678hmov ax,WORD PTR [myDouble+2] ; AX = 1234h
In memory:In memory: 78h78h 56h56h 34h34h 12h12h
Recall that little endian order is used when storing data in memory.
PTR Operator PTR Operator (cont)(cont)
.datamyBytes BYTE 12h,34h,56h,78h.code
...mov ax,WORD PTR myBytes ; AX = 3412hmov ax,WORD PTR [myBytes+2] ; AX = 7856hmov eax,DWORD PTR myBytes ; EAX = 78563412h
PTR can also be used to combine elements of a smaller data type and move them into a larger operand. The IA-32 CPU will automatically reverse the bytes.
TYPE OperatorTYPE Operator
The The TYPETYPE operator returns the size, in operator returns the size, in bytes, of a single element of a data bytes, of a single element of a data declaration.declaration..datavar1 BYTE ?var2 WORD ?var3 DWORD ?var4 QWORD ?.code
...mov eax,TYPE var1 ; 1mov eax,TYPE var2 ; 2mov eax,TYPE var3 ; 4mov eax,TYPE var4 ; 8
LENGTHOF OperatorLENGTHOF Operator
.data byte1 BYTE 10,20,30 ; 3list1 WORD 30 DUP(?),0,0 ; 32list2 WORD 5 DUP(3 DUP(?)) ; 15list3 DWORD 1,2,3,4 ; 4digitStr BYTE "123456789",0 ; 10.code
...mov ecx,LENGTHOF list1 ; ecx contains 32
The LENGTHOF operator counts the number of elements in a single data declaration.
LENGTHOF
SIZEOF OperatorSIZEOF Operator
.databyte1 BYTE 10,20,30 ; 3list1 WORD 30 DUP(?),0,0 ; 64list2 WORD 5 DUP(3 DUP(?)) ; 30list3 DWORD 1,2,3,4 ; 16digitStr BYTE "123456789",0 ; 10.code
...mov ecx,SIZEOF list1 ; ecx contains 64
The SIZEOF operator returns a value that is equivalent to multiplying LENGTHOF by TYPE i.e., size in bytes. SIZEOF
Spanning Multiple LinesSpanning Multiple Lines
.datalist WORD 10,20,
30,40,50,60
.code...
mov eax,LENGTHOF list ; 6mov ebx,SIZEOF list ; 12
• 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:
Spanning Multiple LinesSpanning Multiple Lines
.datalist WORD 10,20
WORD 30,40WORD 50,60
.code...mov eax,LENGTHOF list ; 2mov ebx,SIZEOF list ; 4
• 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:
Index ScalingIndex Scaling
.data
listB BYTE 1,2,3,4,5,6,7
listW WORD 8,9,10,11,12,13
listD DWORD 14,15,16,17,18
.code
...
mov esi,4
mov al,listB[esi*TYPE listB] ; 05
mov bx,listW[esi*TYPE listW] ; 0012
mov edx,listD[esi*TYPE listD] ; 00000018
You can scale an indirect or indexed operand to the offset of an array element. This is done by multiplying the index by the array's TYPE:
PointersPointers
.datalistW DWORD 1000h,2000h,3000hptrW DWORD listW.code
...mov esi,ptrWmov eax,[esi] ; EAX = 1000h
You can declare a pointer variable that contains the offset of another variable.
Note: The effect is the same as mov esi,OFFSET listW
Summing an Integer ArraySumming an Integer Array
.data
intList DWORD 100h,200h,300h,400hptrW DWORD listW
.code
...
mov esi,ptrW ; address of intList
mov ecx,LENGTHOF intList ; loop counter
mov eax,0 ; init the accumulator
L1:
add eax,[esi] ; add an integer
add esi,TYPE intList ; point to next integer
loop L1 ; repeat until ECX = 0
The following code calculates the sum of an array of 32-bit integers (register indirect mode).
Summing an Integer ArraySumming an Integer Array
.data
intList DWORD 100h,200h,300h,400h
.code
...
mov esi,0
mov eax,0 ; zero the accumulator
L1:
add eax,intList[esi*TYPE intList]
inc esi
loop L1
Alternate code (indexed mode)
Questions before Quiz #3?Questions before Quiz #3?
Keep moving on program #4Keep moving on program #4