Data Operators and Directives

OFFSET Operator OFFSET returns the offset of the data label from the beginning of the data segment. In Real Mode, the offset is 16-bits and in Protected Mode, the offset is 32-bits.

Example

.data
v1 WORD 1,2
v2 BYTE 3,4
.code
mov esi, OFFSET v2
mov edi, OFFSET v1 + 2
PTR Operator PTR overrides the size of an operand.

.data
v1 WORD 1234h
v2 DWORD 12345678h
.code
mov al, BYTE PTR v1  ; moves 34h into al
mov bx, WORD PTR v2 + 2 ; moves 1234h into bx
TYPE Operator TYPE returns the size (in bytes) of a single element of the variable.

.data
v1 BYTE ?
v2 WORD ?
v3 DWORD ?

...

TYPE v1 is 1, v2 is 2, and v3 is 4
LENGTHOF Operator LENGTHOF returns the number of elements in the array.

.data
v1 BYTE 9,8,7,6
v2 WORD 1,2,3
v3 DWORD 20 DUP(?)
v4 BYTE "abcdef"
...

LENGTHOF v1 is 4, v2 is 3, v3 is 20, and v4 is 6
SIZEOF Operator SIZEOF returns LENGTHOF times TYPE.

v1 WORD 3,2,1

...

SIZEOF v1 is 3 (number of operands) times 2 (size of word in bytes) = 6

Loop Instruction

Many times a loop is controlled by count. The count is used to specify the number of times the loop will execute.

The loop instruction can be used in many cases for count controlled loops. The general form is as follows:

(i11) <label> LOOP <short-label>

This instruction uses the CX-register as the counter variable and short-label is the label of an instruction that is within -128 to 127 from the end of the loop instruction.

Note: The CX register is used in Real Mode while ECX is used in Protected Mode. In any mode, LOOPD uses ECX and LOOPW uses CX.

When the loop instruction is encountered the following happens:

Decrement the CX-register by 1
If CX != 0 goto short-label Else Execution continues with instruction following loop

Note: None of the flags in the flags register are affected.

P#1: Write an assembly language program segment that will find the sum of the first 100 positive integers.
P#2: Consider the following declaration:

.data
src BYTE "I love assembly!!"
dest BYTE SIZEOF src dup(0)
Write the assembly language code to store the reverse of the string src into dest.

©Douglas J. Ryan/ryandj@pacificu.edu