Addressing Modes and LOOP Instruction
Addressing Modes and LOOP Instruction
Engineering Faculty
Department of Computer Engineering
ECOM 2125: Assembly Language LAB
Lab # 5
Addressing Modes and
LOOP Instruction
March, 2014
1 Assembly Language LAB
Immediate addressing is when an immediate value (a constant) is used for a source operand.
It cannot be used to specify a destination operand. The immediate constant is part of the
instruction itself.
Memory addressing is used to specify the address of the source and destination operands
located in memory. It can be divided into direct and indirect memory addressing.
Direct memory addressing is when the address of a memory operand is specified directly by
name.
For example:
mov sum, eax ; sum is a variable in memory
Direct memory addressing is useful for accessing simple variables in memory, but it is useless
for addressing arrays or data structures. To address the elements of an array, we need to use a
register as a pointer to the array elements. This is called indirect memory addressing.
Register Indirect
Register Indirect can be any 32-bit general-purpose register (EAX, EBX, ECX, EDX, ESI, EDI, EBP,
and ESP) surrounded by brackets. In real-address mode, a 16-bit register holds the offset of a
2 Assembly Language LAB
variable. If the register is used as an indirect operand, it may only be SI, DI, BX, or BP. Avoid BP
unless you are using it to index into the stack. The register is assumed to contain the address of
some data.
Example:
.data
byteVal BYTE 10h
.code
mov esi,OFFSET byteVal
mov al,[esi] ; AL = 10h
The size of an operand may not be evident from the context of an instruction. The following
instruction causes the assembler to generate an “operand must have size” error message:
inc [esi] ; error: operand must have size
The assembler does not know whether ESI points to a byte, word, doubleword, or some other
size. The PTR operator confirms the operand size:
inc BYTE PTR [esi]
Indexed Addressing
Indexed Addressing adds a constant to a register to generate an effective address.
constant[indexReg]
[constant + indexReg]
Example:
.data
arrayB BYTE 10h,20h,30h
.code
mov esi,0
mov al,[arrayB + esi] ; AL = 10h
Index Scaling
The scale factor is the size of the array component (word = 2, doubleword = 4, quadword = 8).
constant[indexReg * scale]
[constant + indexReg * scale]
Example:
.data
arrayD DWORD 1,2,3,4
.code
mov esi,3
mov eax,arrayD[esi*4] ; EAX = 4
3 Assembly Language LAB
mov esi,3
mov eax,arrayD[esi*TYPE arrayD] ; EAX = 4
Based Addressing
The based addressing combines a register with a constant offset. The base register holds the
base address of an array or structure, and the constant identifies offsets of various array
elements.
[BaseReg + Offset]
Example:
.data
arrayW WORD 1000h,2000h,3000h
.code
mov esi,OFFSET arrayW
mov ax,[esi] ; AX = 1000h
mov ax,[esi+2] ; AX = 2000h
Based-Indexed Addressing
LEA Instruction
LEA = Load Effective Address
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
arrayB BYTE "WELCOME", 0
arrayW WORD 100h, 200h, 300h, 400h
arrayD DWORD 01234567h, 89ABCDEFh
.code
main PROC
; Direct Memory Addressing
mov al, arrayB ; same as [arrayB]
mov ah, arrayB[5] ; same as [arrayB+5]
mov bx, arrayW[2] ; same as [arrayW+2]
mov ecx,[arrayD] ; same as arrayD
mov edx,[arrayD+2] ; same as arrayD[2]
; Indexed Addressing
mov edx, 4
mov al, arrayB[edx]
mov bx, arrayW[edx]
mov ecx,arrayD[edx]
5 Assembly Language LAB
; Based Addressing
mov esi,OFFSET arrayW
mov bx,[esi+2]
mov ecx,[esi+4]
exit
main ENDP
END main
First, guess the values of the registers and memory variables in program addressing.asm. Run
the Windows Debugger. Open the source file addressing.asm from the File menu if it is not
already opened. Watch the registers and memory by selecting them in the View menu. In the
Memory window, write the name of the first variable arrayB in the Virtual address box. You
may resize the Memory window so that exactly 16 bytes are displayed on each line. Place the
cursor at the beginning of main procedure and press F7. Press F10 to step through the
execution of the program. Watch the changes in the registers and memory.
2. LOOP Instruction
The LOOP instruction, formally known as Loop According to ECX Counter, repeats a block of
statements a specific number of times. ECX is automatically used as a counter and is
decremented each time the loop repeats.
LOOP destination
The loop destination must be within -128 to +127 bytes of the current location counter.
6 Assembly Language LAB
Nested Loops
If you need to code a loop within a loop, you must save the outer loop counter's ECX value.
.DATA
count DWORD ?
.CODE
mov ecx, 100 ; set outer loop count to 100
L1:
mov count, ecx ; save outer loop count
mov ecx, 20 ; set inner loop count to 20
L2: .
.
loop L2 ; repeat the inner loop
mov ecx, count ; restore outer loop count
loop L1 ; repeat the outer loop
Reverse a String
The following program demonstrates indirect addressing, array indexing, and LOOP: Open and
view this program in ConTEXT. Assemble and link this program to produce the ReverseStr.exe
executable file, by pressing F9, or pressing . You can use the make32 batch file from the
command prompt.
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0)
.code
main PROC
mov esi, SIZEOF source ; used to index source
mov edi, 0 ; used to index target
dec esi ; do not copy 0
mov ecx, esi ; loop counter
7 Assembly Language LAB
L1:
mov al, source[esi-1] ; get a character from source
mov target[edi], al ; store it in the target
inc edi ; increment target index
dec esi ; decrement source index
loop L1 ; repeat for entire string
exit
main ENDP
END main
.686
.MODEL flat, stdcall
INCLUDE Irvine32.inc
.data
matrix DWORD 0, 1, 2, 3, 4 ; 4 rows, 5 columns
DWORD 10,11,12,13,14
DWORD 20,21,22,23,24
DWORD 30,31,32,33,34
count DWORD ?
.code
main PROC
mov esi,OFFSET matrix ; ESI = address of intarray
mov eax,0 ; sum = 0
mov ecx, 4 ; initialize outer loop counter
; (number of rows)
exit
main ENDP
END main
Lab Exercise
Write a program that uses a loop to calculate the first seven values in the Fibonacci number
sequence { 1, 1, 2, 3, 5, 8, 13 } where The Rule is Fn = Fn-1 + Fn-2. The Fibonacci sequence is
referenced in the memory by the byte memory array called Fibonacci save the remaining five
elements in the same array.
Fibonacci BYTE 1, 1, 5 dup(?)
Trace the execution of the program and view the Registers and Memory windows after each
Step into (F8) using the windows debugger.
Homework Exercise
1. Rewrite the Lab Exercise using the array:
2. Write an assembly language program using the Loop instruction to store all letters in
memory at Letters as follow:
Letters BYTE 26 dup(?)
Trace the execution of the program and view the Memory window using the windows
debugger.
Best Wishes