COAL Lab#05
COAL Lab#05
Contents
4.1. Data Transfer Instructions
4.2. Addition and Subtraction
4.3. Data Addressing Modes
4.4. LOOP Instruction
4.5. Copying a String
4.6. Summing an Array of Integers
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data var1
WORD 1000h var2
WORD 2000h
.code
main PROC
; Demonstrating MOV and MOVZX
mov ax, 0A69Bh movzx
bx, al movzx ecx,ah
movzx edx,ax
; Demonstrating MOVSX
movsx bx, al movsx
ecx,ah movsx
edx,ax
; Demonstrating XCHG
xchg ax,var1 xchg
ax,var2 xchg ax,var1
exit
main ENDP END
main
4.1.1 Lab Work: Assemble and Link moves.asm
Open file moves.asm and assemble and link the file. You can use the make32 batch file from
the command prompt. And then we linked/assembled the file debugging with the keyword
windbg –QY–G moves.exe. which is showing down below:
3. Now Demonstrating MOVSX .The value of bx in hex after execution is bx= 0xff9b.
4. Now Demonstrating XCHG .Now we can see the changing in var1, ax, al and a and also
we can see the changing in var2.
Open the Watch window to view the var1 and var2 variables. You can also watch registers
under the Watch window by typing their names preceded with the @ symbol as shown below.
Observe that the type of registers is unsigned int and the value is shown in hexadecimal. You
can change the type of a register to char, short, or int (depending on its size), to see its value
as a signed decimal.
5. Now we can see the changing in var1, ax, al and ah.
MOV and MOVZX
1) al, ah (hex) = 0x9b, 0xa6 2) bx (hex) = 0x9b
5) bx (hex) = 0xff9b
; SUB
mov eax, 91ab0748h
sub eax, ebx
; NEG
mov eax, 91ab0748h
neg eax
; INC
clc ; clear carry flag to show that it is not affected mov
eax,7fffffffh
inc eax
; DEC
mov eax,0
dec eax
exit
main ENDP END
main
2. SUB
We can see the value of eax after execution is eax=91ab0748.
3. ; INC Now we can see the value of eax after execution is eax= 7ffffff.
4. Now we can see the value of eax after decrement eax= ffffffff.
4.2.2 Lab Work: Trace the Execution of SimpleArith.exe
First, guess the values of the eax register (in hexadecimal) and the cf, of, sf, zf, and pf flags
after executing the add, sub, neg, inc, and dec instructions. Write these values below:
CF= OF= SF= ZF= PF=
1) EAX after ADD (hex) = d100003a
0 0 1 0 1
CF= OF= SF= ZF= PF=
2) EAX after SUB (hex) = 52560e56
0 1 0 0 1
CF= OF= SF= ZF= PF=
3) EAX after NEG (hex) = 6e54f8b8
1 0 0 0 1
CF= OF= SF= ZF= PF=
4) EAX after INC (hex) = 80000000
0 1 1 0 1
CF= OF= SF= ZF= PF=
5) EAX after DEC (hex) = ffffffff
0 0 1 0 1
Run the 32-bit Windows Debugger. Open the source file SimpleArith.asm from the File menu
if it is not already opened. Watch the registers by selecting Registers from the View menu.
Have the registers eax, ebx and the flags cf, of, sf, zf, and pf on top of the list. Place the
cursor at the beginning of main procedure and press F7 to start debugging it. Press F10 to
step through the execution of the program. Watch the changes in the registers and flags.
Direct Indirect
The LEA instruction provides more flexibility in terms of computing complex addresses at
runtime. However, the OFFSET operator is used to determine the address of named variables
at assembly-time.
; Based Addressing
mov edx, 4 mov al,
arrayB[edx] mov bx,
arrayW[edx] mov
ecx,arrayD[edx]
; Scaled Indexed Addressing
mov esi, 1 mov
arrayB[esi*2], 'S' mov
arrayW[esi*2], 102h mov
arrayD[esi*4], 0
2.
3.
4.
5.
Based Addressing
.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, 0 ; used to index source and target
mov ecx, SIZEOF source ; loop counter
L1:
mov al, source[esi] ; get a character from source
mov target[esi], al ; store it in the target
inc esi ; increment index
loop L1 ; repeat for entire string
exit
main ENDP END
main
What is the value of the target string in memory after finishing the execution of loop
L1?
Answer: After the execution of loop L1, the value of the target string in memory will be a
copy of the source string, which is "Twinkle twinkle little star".
Answer: The number of iterations for loop L1 is determined by the SIZEOF source
instruction. In this case, the source string is "Twinkle twinkle little star" followed by a null
terminator (0).
Therefore, the number of iterations for loop L1 is 30 (the number of characters in the source
string) + 1 (for the null terminator) = 31.
4.5.1 Lab Work: Assemble and Link CopyStr.asm 4.5.2 Lab Work: Trace the
Execution of CopyStr.exe
1.First of all we go in the loop.
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
intarray SWORD 5,7,-3,100,0,-9, 10 DUP(-999) sum
SWORD ?
.code
main PROC
mov esi,OFFSET intarray ; esi = pointer to intarray
mov ecx,LENGTHOF intarray ; ecx = loop counter
mov ax,0 ; zero the accumulator
L1:
add ax,[esi] ; register esi is a pointer add
esi,TYPE intarray ; point to next integer
loop L1 ; repeat until ECX = 0
mov sum, ax
exit
main ENDP END
main
4.6.1 Lab Work: Assemble, Link, and Trace Program Execution
Open file SumArray.asm and assemble and link the file. Now run the Windows debugger to
trace the execution of the above program. You need to view the registers esi, ecx, and ax. Add
also a watch for the sum variable.
1.
2.
3.
4.
Q. What is the value of sum (in decimal) at the end of the program?
Answer:
LENGTHOF intarray is the number of elements in intarray, which is 13.
Q. Assuming the initial value of ESI is 404000h before starting loop L1, what is the
final value of ESI (in hex) after finishing loop L1?
Answer: Assuming the initial value of ESI is 404000h, and each element in intarray is of
type SWORD (2 bytes), the final value of ESI can be calculated as follows:
Review Questions
1. For each of the following, write the destination register value (in hexadecimal) if the
instruction is valid. Otherwise, indicate that the instruction is invalid. Assume that var1 is
at virtual address 404000h.
var1 SBYTE -4, 2
var2 WORD 1000h, 2000h,3000h
var3 DWORD 1, 2, 3, 4, 5
a. mov ax, var1 Invalid because var1 is at a virtual
address (404000h) and not a register.
b. movzx ax, var1 Invalid because movzx requires a
register or memory operand as the source, and var1 is at a
virtual address.
c. movsx eax, var1 Invalid for the same reason as b
d. mov ax, var2[2] Valid. It moves the value at the third
element (index 2) of the array var2 into the ax register.
e. mov bx, var3 Invalid because var3 is an array, and
you can't directly move an array into a register.
f. mov edx, [var3+4] Valid. It moves the double-word at the
address var3 + 4 into the edx register.
g. lea esi, var2 Valid. It loads the effective address
(address of the first element) of the array var2 into the
esi register.
h. mov al, [esi] Valid. It moves the byte at the address
stored in esi into the al register.
i. mov ax, [esi] Valid. It moves the word at the address
stored in esi into the ax register.
j. mov eax, [esi] Valid. It moves the double-word at the
address stored in esi into the eax register.
k. inc [esi] Valid. It increments the value at the
address stored in esi.
2. Is it possible to set the Overflow flag if you add a positive to a negative integer?
Yes
3. Is it possible for the NEG instruction to set the Overflow flag?
Yes
4. Is it possible for both the Sign and Zero flags to be set at the same time?
No
5. Can any 16-bit general-purpose register be used for indirect addressing?
Yes
6. Can any 32-bit general-purpose register be used for indirect addressing?
Yes
Programming Exercises
1. Write a program that does the following:
• Use the ADD and SUB instructions to set and clear the Carry flag.
• Use the ADD and SUB instructions to set and clear the Zero and Sign flags.
• Use the ADD and SUB instruction to set and clear the Overflow flag.
• Use the ADD instruction to set and clear both the Carry and Overflow flags.
Trace program execution and Explain why the flags are affected by each instruction.
2. Write a program that uses a loop to calculate the first ten values in the Fibonacci number
sequence {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}. Place each value in the EAX register inside the
loop, and trace it with the Windows debugger.
TITLE Fibonacci Series (File: fibonacci.asm)
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
fib DWORD 10 DUP(0)
.code
main PROC
mov ecx, 10 ; Number of Fibonacci values to calculate
mov ebx, 1 ; First Fibonacci number
mov esi, 1 ; Second Fibonacci number
Loop1:
add eax, esi ; Calculate the next Fibonacci number
mov esi, ebx ; Update the second Fibonacci number
mov ebx, eax ; Update the first Fibonacci number
main ENDP
END main
3. Modify Program SumArray.asm to use the register esi as an index to intarray with a scale
factor of 2, instead of a pointer. Trace it with the Windows debugger.
4. Modify Program CopyStr.asm to use the register esi as a pointer (indirect addressing),
instead of an index, to copy the characters from source to target, but in reverse order.