0% found this document useful (0 votes)
14 views7 pages

Part I

assemble

Uploaded by

Mofawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Part I

assemble

Uploaded by

Mofawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Part I: Data Addressing Modes

Addressing Mode: is a method for specifying an address within an instruction,


how an operand address is determined? And how to obtain the effective address?

Effective Address: is the address of the operand which denotes the address
where data is located and it is the resulting address after all calculations.

Three basic types of operands:

• Immediate – a constant integer (8, 16, or 32 bits)


• value is encoded within the instruction
• Register – the name of a register.
• register name is converted to a number and encoded within
the instruction
• Memory – reference to a location in memory.
• memory address is encoded within the instruction, or a
register holds the address of a memory location

Assembly Language Addressing Modes:

1. Immediate Addressing: the operand is contained directly in the


instruction.

Opcode Operand

MOV reg, immediate (constant)

Ex: MOV eax, 2000

2. Register Addressing: the operand is contained in a register.

Opcode Reg

Ex: MOV eax, ebx

3. Direct Addressing: the address of the operand (Effective Address, EA) is


contained in the instruction
Opcode EA

Operand

Ex: MOV ax, [3000]

3000 Store (2 bytes) in ax register.

DS

Data Segment

4. Indirect Addressing: the instruction specifies a register (can be any 32-bit


general purpose register surrounded by brackets), which contains an
address, which contains the effective address of the operand

Opcode Reg

Address

EA

Operand

Ex: MOV ax, [esi]

5. Index or Relative Addressing (Base-Displacement): the EA is the sum


of the contents of a register (the base) plus a constant contained in the
instruction (the displacement)

Opcode Reg Constant

Address +

EA
Ex: MOV ax, [esi+1]

Data-Related Operators

1. OFFSET Operator
2. TYPE Operator
3. LENGTHOF Operator
4. SIZEOF Operator

1. OFFSET Operator: OFFSET returns the distance in bytes, of a label from


the beginning of its enclosing segment.

offset

data segment:

myByte

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

.data
bVal BYTE ?
wVal WORD ?
dVal DWORD ?
dVal2 DWORD ?

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

2. TYPE Operator: tTYPE operator returns the size, in bytes, of a single


element of a data declaration.

.data
var1 BYTE ?
var2 WORD ?
var3 DWORD ?
var4 QWORD ?

.code
3. LENGTHOF Operator: The LENGTHOF operator counts the number of
elements in a single data declaration.

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

.code
mov ecx, LENGTHOF array1 ; 32

4. SIZEOF Operator: The SIZEOF operator returns a value that is equivalent


to multiplying LENGTHOF by TYPE.

.data SIZEOF
byte1 BYTE 10,20,30 ;3
array1 WORD 30 DUP(?),0,0 ; 64
array2 WORD 5 DUP(3 DUP(?)) ; 30
array3 DWORD 1,2,3,4 ; 16
digitStr BYTE "12345678",0 ;9

.code
mov ecx, SIZEOF array1 ; 64

Part II: Arrays

You can address array elements as the following:

1.
.data
array1 BYTE 10h,20h,30h
.code
mov esi,OFFSET array1
mov al,[esi] ; dereference ESI (AL = 10h)

inc esi ; or add,type array1


mov al,[esi] ; AL = 20h

inc esi
mov al,[esi] ; AL = 30h

.data
array2 word 100h,200h,300h
.code
mov esi,OFFSET array2
mov ax,[esi] ; dereference ESI (AL = 100h)

add esi,2
mov ax,[esi] ; AL = 200h

add esi,2
mov ax,[esi] ; AL = 300h

2.
.data
arrayW WORD 1000h, 2000h, 3000h

.code
mov esi,0
mov ax, arrayW[esi] ; AX = 1000h

add esi, type arrayw


add ax, arrayW[esi] ; AX= 2000h

add esi, type arrayw


add ax, arrayW[esi] ; AX= 3000h

3.

.data
arrayb BYTE 10h, 20h, 30h
arrayw WORD 100h,200h,300h
arrayd dword 1000h, 2000h

.code
mov al, arrayb ; AL = 10h
mov al, [arrayb+1] ; AL = 20h
mov al, [arrayb+2] ; AL = 30h
.data
wordList word 1000h,2000h,3000h, 0

.code
mov esi,offset wordList
mov ax, [esi] ; first number
add ax, [esi +2] ; second number
add ax, [esi +4] ; third number
mov [esi +6], ax ; store the sum

Problem 1
Write a program to compute the number of elements in the
following array.
Source byte “Computer Science”, 0
Problem 2
Write a program to sum the elements of array1.
Array1 word 6, 12h, 63, 1001b,’A’

You might also like