Transcript Chapter 1

CS2422 Assembly Language and System Programming

Week 10

Data-Related Operators and Directives

0

What's Next

     Data Transfer Instructions Addition and Subtraction Data-Related Operators and Directives   OFFSET Operator PTR Operator   TYPE Operator LENGTHOF Operator Interpreted by assembler   SIZEOF Operator LABEL Directive Indirect Addressing JMP and LOOP Instructions 1

OFFSET Operator

  OFFSET is to

get the memory address of variables

during program execution It returns the distance in bytes of a label from the beginning of its enclosing segment  

Protected mode

: 32 bits

Real mode

: 16 bits offset For Example: If your computer is in real mode, software communicates directly with the computer's ports and devices. However, If your computer is in protected mode, the system's ports and devices are protected from the applications that use them. The software thinks it's sending data to a port, but it's a virtual port. The OS is grabbing the data stream and managing it, to ensure that all applications have equal access and to ensure that data from each application is appropriately preserved.

data segment: myByte The protected-mode programs that we write only have a single segment (we use the flat memory model) 2

OFFSET Examples

Let's assume that the data segment begins at 00404000h:

.data

bVal BYTE ?

wVal WORD ?

dVal DWORD ?

dVal2 DWORD ?

.code

mov esi,OFFSET bVal mov esi,OFFSET wVal mov esi,OFFSET dVal mov esi,OFFSET dVal2 ; ESI = 00404000 ; ESI = 00404001 ; ESI = 00404003 ; ESI = 00404007

3

OFFSET Example

.data

bVal wVal byte word 1 2 dVal dword 3 dVal2 dword 4 .code

main PROC mov al, bval mov bx, wVal mov ecx, dVal mov edx, dVal2 call DumpRegs mov eax, offset bval mov ebx, offset wVal mov ecx, offset dVal mov edx, offset dVal2 call DumpRegs exit main ENDP

4

OFFSET Example

 Let's assume that the data segment begins at 00404000h  Result of execution:

… EAX=75944801 ESI=00000000 EIP=0040102D EBX=7FFD0002 EDI=00000000 EFL=00000246 ECX=00000003 EBP=0012FF94 CF=0 SF=0 EDX=00000004 ESP=0012FF8C ZF=1 OF=0 EAX=00404000 ESI=00000000 EIP=00401046 … EBX=00404001 EDI=00000000 EFL=00000246 ECX=00404003 EBP=0012FF94 CF=0 SF=0 EDX=00404007 ESP=0012FF8C ZF=1 OF=0

5

PTR Operator

Overrides default type of a label (variable)

and provides the flexibility to access part of a variable

.data

myDouble DWORD 12345678h .code

mov ax,myDouble mov ax,WORD PTR myDouble mov WORD PTR myDouble,4321h ; error – why?

; loads 5678h ; saves 4321h

Recall that little endian order is used when storing data in memory (see next slides) 6

Little Endian Order

doubleword 12345678    Little endian order refers to the way Intel stores integers in memory.

Multi-byte integers are stored in reverse order, with the least significant byte stored at the lowest address For example, the doubleword 12345678h would be stored as: word byte offset 5678 78 0000 myDouble When integers are loaded from memory into registers, the bytes 56 0001 1234 34 0002 their correct positions.

myDouble + 2 12 0003 myDouble + 3 7

PTR Operator Examples

.data

myDouble DWORD 12345678h

doubleword 12345678 word 5678 byte 78 56 1234 34 12 offset 0000 myDouble 0001 myDouble + 1 0002 myDouble + 2 0003 myDouble + 3

mov al,BYTE PTR myDouble mov al,BYTE PTR [myDouble+1] mov al,BYTE PTR [myDouble+2] mov ax,WORD PTR myDouble mov ax,WORD PTR [myDouble+2] ; AL = 78h ; AL = 56h ; AL = 34h ; AX = 5678h ; AX = 1234h

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 processor will automatically reverse the bytes

.data

myBytes BYTE 12h,34h,56h,78h .code

mov ax,WORD PTR [myBytes] mov ax,WORD PTR [myBytes+2] mov eax,DWORD PTR myBytes ; AX = 3412h ; AX = 7856h ; EAX = 78563412h

9

Your Turn . . .

• Write down value of each destination operand:

.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.

mov bl,BYTE PTR [varW+2] ; c.

mov ax,WORD PTR [varD+2] ; d.

mov eax,DWORD PTR varW ; e.

0502h 78h 02h 1234h 12026543h

10

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 mov eax,TYPE var2 mov eax,TYPE var3 mov eax,TYPE var4 ; 1 ; 2 ; 4 ; 8

11

LENGTHOF Operator

 The LENGTHOF operator

counts the number of elements in a single data declaration .data

byte1 BYTE 10,20,30

LENGTHOF

; 3 array1 WORD 30 DUP(?),0,0 array2 WORD 5 DUP(3 DUP(?)) ; 15 array3 DWORD 1,2,3,4 ; 32 ; 4 digitStr BYTE "12345678",0 ; 9 .code

mov ecx,LENGTHOF array1 ; 32

12

SIZEOF Operator

 SIZEOF

returns a value that is equivalent to multiplying LENGTHOF by TYPE

.

.data

byte1 BYTE 10,20,30 digitStr BYTE "12345678",0

SIZEOF

; 3 array1 WORD 30 DUP(?),0,0 array2 WORD 5 DUP(3 DUP(?)) ; 30 array3 DWORD 1,2,3,4 ; 64 ; 16 ; 9 .code

mov ecx,SIZEOF array1 ; 64

13

Spanning Multiple Lines (1 of 2)

 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 eax,LENGTHOF array mov ebx,SIZEOF array ; 6 ; 12

14

Spanning Multiple Lines (2 of 2)

 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 eax,LENGTHOF array mov ebx,SIZEOF array ; 2 ; 4

15

Relationships Between Assembly Lang. Concept

16