lab # 5site.iugaza.edu.ps/rsalamah/files/2015/02/assembly_la… · · 2016-02-16lab # 5...
TRANSCRIPT
Lab # 5 Addressing Modes and LOOP Instruction
March, 2014
Islamic University – Gaza
Engineering Faculty
Department of Computer Engineering
ECOM 2125: Assembly Language LAB
1 Assembly Language LAB
1. Data Addressing Modes Addressing Modes
The assembly language instructions require the specification of the location of data for source and destination operands. The specification of the location of data is called the data addressing mode. It can be classified as shown in the following diagram:
Register addressing is when a register is used to specify the source or destination of an operand. This is the most efficient addressing mode because registers are implemented inside the processor and their access is very fast. 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
The TYPE operator can make the indexing more flexible: 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
[BaseReg + (IndexReg * Scale) + Offset]
Useful in accessing two-dimensional arrays: - Offset: array address we can refer to the array by name - Base register: holds row address relative to start of array - Index register: selects an element of the row column index - Scaling factor: when array element size is 2, 4, or 8 bytes
Example: .data
matrix DWORD 0, 1, 2, 3, 4 ; 4 rows, 5 cols
DWORD 10,11,12,13,14
DWORD 20,21,22,23,24
DWORD 30,31,32,33,34
ROWSIZE EQU SIZEOF matrix ; 20 bytes per row
.code
mov ebx, 2*ROWSIZE ; row index = 2
mov esi, 3 ; col index = 3
mov eax, matrix[ebx+esi*4] ; EAX = matrix[2][3]
mov ebx, 3*ROWSIZE ; row index = 3
mov esi, 1 ; col index = 1
mov eax, matrix[ebx+esi*4] ; EAX = matrix[3][1]
4 Assembly Language LAB
LEA Instruction
LEA = Load Effective Address
LEA r32, mem
Examples on 32-bit Addressing Modes
The following program demonstrates 32-bit memory addressing modes and the LEA instruction: Open and view this program in ConTEXT. Assemble and link this program to produce the
addressing.exe executable file, by pressing F9, or pressing . You can use the make32 batch file from the command prompt. TITLE Memory Addressing Examples (File: addressing.asm)
.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]
; Register Indirect Addressing
mov ecx, OFFSET arrayB
mov edx, OFFSET arrayW
mov bx, [ecx] ; address in [ecx]
mov al, [edx] ; address in [edx]
; Indexed Addressing
mov edx, 4
mov al, arrayB[edx]
mov bx, arrayW[edx]
mov ecx,arrayD[edx]
5 Assembly Language LAB
; Scaled Indexed Addressing
mov esi, 1
mov arrayB[esi], 'S'
mov arrayW[esi*2], 102h
mov arrayD[esi*4], 0
; Based Addressing
mov esi,OFFSET arrayW
mov bx,[esi+2]
mov ecx,[esi+4]
; Load Effective Address (LEA)
lea eax, arrayB ; same as: mov eax, OFFSET arrayB
lea ebx,[eax + LENGTHOF arrayB]
lea ecx,[ebx + esi*8]
lea edx, arrayD
exit
main ENDP
END main
Lab Work: Trace the Execution of Program moves.exe
Now run the Windows debugger to watch the variables and the memory content. Run the
Windows Debugger by pressing F10 or pressing . You may run the debugger from the command prompt by typing: windbg –QY –G addressing.exe 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
The execution of the LOOP instruction involves two steps:
ECX ECX – 1 If ECX != 0, jump to destination label
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.
TITLE Reverse a String (File: ReverseStr.asm)
; Demonstrates indirect addressing, array indexing, and LOOP
.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
Lab Work: Trace the Execution of ReverseStr.exe
Run the 32-bit Windows Debugger. View the registers esi, edi, ecx and al, as well as the source and target variables in memory. You can open two Memory windows to view separately the source and target strings. Put the cursor at the beginning of main procedure and press F7 to start debugging it. Press F8 to trace the execution of the loop, iteration by iteration. If you press F10 on the LOOP instruction, it will execute ALL the loop iterations and will terminate the loop. This is why it is better here to use F8 to trace the execution of the loop. View how the esi, edi, ecx, and al registers change, as well as memory for the target variable.
Summing a Matrix of Integers
Program SumMatrix.asm uses register esi as a pointer to matrix. Register-indirect addressing is used to access the elements of matrix: Open and view this program in ConTEXT. Assemble and
link this program to produce the SumMatrix.exe executable file, by pressing F9, or pressing . You can use the make32 batch file from the command prompt.
8 Assembly Language LAB
TITLE Summing matrix (SumMatrix.asm)
.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)
L1: ; mark beginning of outer loop
mov count, ecx ; save outer loop counter
mov ecx,LENGTHOF matrix ; initialize inner loop counter
; (number of columns)
L2: ; mark beginning of inner loop
add eax,[esi] ; add an integer
add esi,TYPE matrix ; point to next element
loop L2 ; repeat until ECX
; (number of columns) = 0
mov ecx, count ; restore outer loop count
loop L1 ; repeat until ECX
; (number of rows) = 0
exit
main ENDP
END main
Lab Work: Assemble, Link, and Trace Program Execution
Run the Windows debugger to trace the execution of the above program. You need to view the registers esi, ecx, and eax. Add also a watch for the sum variable. Put the cursor at the beginning of main procedure and press F7 to start debugging this procedure. Press F8 to trace the execution of the loop, iteration by iteration. View how the esi, ecx, and eax registers change. Press F10 to exit the program.
9 Assembly Language LAB
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:
Fibonacci DWORD 1, 1, 5 dup(?)