NFC-IET Institute of Engineering and Technology
Department of Computer Science
Computer Organization
&
Assembly Language
Assembly Language Operators - Working and Usage
Submitted to Sir M. Akhtar
Submitted by Abdullah Ibrahim
Roll no. 226
Section G
Session 2K24 (BSCS)
NFC-IET INSTITUTE OF ENGINEERING AND TECHNOLOGY
Assembly Language Operators - Working and
Usage
This assignment explores key operators in assembly language, focusing on their functionality,
syntax, and practical application through code examples. Understanding these operators is
crucial for efficient memory management, data manipulation, and writing robust assembly
programs.
INTRODUCTION TO ASSEMBLY LANGUAGE OPERATORS
Assembly language provides several operators that allow programmers to interact directly with
memory addresses, data types, and sizes. These operators are essential for low-level
programming tasks where precise control over data is required. Below, we delve into six
fundamental operators.
1. OFFSET Operator
The OFFSET operator returns the memory offset (address relative to the beginning of the
segment) of a variable, label, or procedure. It is commonly used to pass the address of data to
procedures, especially when working with pointers.
Syntax: OFFSET variable_name
.data
myVar DWORD 1234h
myArray BYTE 10, 20, 30
.code
MOV EAX, OFFSET myVar ; EAX now holds the offset of myVar
MOV EBX, OFFSET myArray ; EBX now holds the offset of myArray
2. PTR Operator
The PTR (pointer) operator overrides the default size of an operand. This is particularly useful
when accessing memory at a size different from its declared type, for example, reading a byte
from a word-sized variable, or when dereferencing an untyped pointer.
Syntax: type PTR operand (where 'type' can be BYTE, WORD, DWORD, QWORD, etc.)
.data
myWord WORD 0ABCDh
myDword DWORD 12345678h
myPointer DWORD? ; An untyped pointer
.code
MOV AL, BYTE PTR myWord ; AL gets 0CDh (low byte of myWord)
MOV AX, WORD PTR myDword ; AX gets 5678h (low word of myDword)
MOV EAX, OFFSET myDword
MOV myPointer, EAX ; myPointer now points to myDword
MOV BL, BYTE PTR [myPointer] ; BL gets 78h (first byte of myDword via pointer)
3. TYPE Operator
The TYPE operator returns the size, in bytes, of a single element of a data type or variable. For a
variable, it returns the size of its declared type. For arrays, it returns the size of one element, not
the whole array.
Syntax: TYPE variable_name
.data
byteVar BYTE 'A'
wordVar WORD 1234h
dwordVar DWORD 12345678h
myArray WORD 10, 20, 30
. code
MOV EAX, TYPE byteVar ; EAX gets 1 (size of BYTE)
MOV EBX, TYPE wordVar ; EBX gets 2 (size of WORD)
MOV ECX, TYPE dwordVar ; ECX gets 4 (size of DWORD)
MOV EDX, TYPE myArray ; EDX gets 2 (size of one WORD element)
4. LENGTHOF Operator
The LENGTHOF operator returns the number of elements in an array. It is only applicable to
variables that are declared as arrays.
Syntax: LENGTHOF array_name
.data
myBytes BYTE 1, 2, 3, 4, 5
myWords WORD 10h, 20h, 30h
myDwords DWORD 1, 2, 3
. code
MOV EAX, LENGTHOF myBytes ; EAX gets 5 (number of BYTE elements)
MOV EBX, LENGTHOF myWords ; EBX gets 3 (number of WORD elements)
MOV ECX, LENGTHOF myDwords ; ECX gets 3 (number of DWORD elements)
5. SIZEOF Operator
The SIZEOF operator returns the total size, in bytes, of a variable or data type. For arrays, it returns
the total number of bytes occupied by all elements in the array. This is equivalent to LENGTHOF
* TYPE for an array.
Syntax: SIZEOF variable_name
.data
singleByte BYTE 'Z'
myWords WORD 10h, 20h, 30h ; 3 words * 2 bytes/word = 6 bytes
myDwords DWORD 1, 2 ; 2 dwords * 4 bytes/dword = 8 bytes
. code
MOV EAX, SIZEOF singleByte ; EAX gets 1
MOV EBX, SIZEOF myWords ; EBX gets 6
MOV ECX, SIZEOF myDwords ; ECX gets 8
6. LABEL Operator
The LABEL operator is used to assign a name and a size attribute to a memory location without
allocating any storage for it. It allows different parts of memory to be referenced by different data
types, which can be useful for viewing a block of memory in various ways.
Syntax: name LABEL type
.data
; Define a block of memory
myBuffer BYTE 10h, 20h, 30h, 40h, 50h, 60h, 70h, 80h
; Use LABEL to refer to myBuffer as different types
bufferWords LABEL WORD
bufferDwords LABEL DWORD
. code
; Access the first byte of myBuffer (normal)
MOV AL, myBuffer
; Access the first word of myBuffer
MOV AX, WORD PTR myBuffer ; or MOV AX, bufferWords
; Access the first dword of myBuffer
MOV EAX, DWORD PTR myBuffer ; or MOV EAX, bufferDwords