lab # 4site.iugaza.edu.ps/rsalamah/files/2015/02/assembly_lab4.pdf · lab work: assemble and link...
TRANSCRIPT
Lab # 4 Data Transfers and Arithmetic
March, 2014
Islamic University – Gaza
Engineering Faculty
Department of Computer Engineering
ECOM 2125: Assembly Language LAB
1 Assembly Language LAB
1. Data Transfer Instructions MOV Instruction
The MOV instruction copies data from a source operand to a destination operand.
MOV destination,source destination = source
The destination operand’s contents change, but the source operand is unchanged. MOV rules:
Both operands must be the same size.
Both operands cannot be memory operands.
CS, EIP, and IP cannot be destination operands.
An immediate value cannot be moved to a segment register. The variants of MOV:
MOV reg,reg
MOV mem,reg
MOV reg,mem
MOV mem,imm
MOV reg,imm
MOV reg/mem16,sreg
MOV sreg,reg/mem16
MOVZX Instruction The MOVZX instruction (move with zero-extend) copies the contents of a source operand into a destination operand and zero-extends the value to 16 or 32 bits. (Fills the upper part of the destination with zeros)
MOVZX destination, source The variants of MOVZX:
MOVZX reg32,reg/mem8
MOVZX reg32,reg/mem16
MOVZX reg16,reg/mem8
2 Assembly Language LAB
MOVSX Instruction The MOVSX instruction (move with sign-extend) copies the contents of a source operand into a destination operand and sign-extends the value to 16 or 32 bits. (Fills the upper part of the destination with a copy of the source operand's highest bit)
MOVSX destination, source The variants of MOVSX:
MOVSX reg32,reg/mem8
MOVSX reg32,reg/mem16
MOVSX reg16,reg/mem8
XCHG Instruction The XCHG (exchange data) instruction exchanges the contents of two operands. The variants of XCHG:
XCHG reg,reg
XCHG reg,mem
XCHG mem,reg
The rules for operands in the XCHG instruction are the same as those for the MOV instruction except that XCHG does not accept immediate operands.
Direct-Offset Operands You can add a displacement to the name of a variable, creating a direct-offset operand. This lets you access memory locations that may not have explicit labels. arrayB BYTE 10h,20h,30h,40h,50h
If we use MOV with arrayB as the source operand, we automatically move the first byte in the array: mov al,arrayB ; AL = 10h
We can access the second byte in the array by adding 1 to the offset of arrayB: mov al,[arrayB+1] ; AL = 20h
3 Assembly Language LAB
The third byte is accessed by adding 2: mov al,[arrayB+2] ; AL = 30h
The brackets are not required by MASM, so the following statements are equivalent: mov al,[arrayB+1]
mov al,arrayB+1
Word and Doubleword Arrays: In an array of 16-bit words, the offset of each array element is 2 bytes beyond the previous one. That is why we add 2 to ArrayW in the next example to reach the second element: .data
arrayW WORD 100h,200h,300h
.code
mov ax,arrayW ; AX = 100h
mov ax,[arrayW+2] ; AX = 200h
Similarly, the second element in a doubleword array is 4 bytes beyond the first one: .data
arrayD DWORD 10000h,20000h
.code
mov eax,arrayD ; EAX = 10000h
mov eax,[arrayD+4] ; EAX = 20000h
Note: The MOV instruction never affects the flags.
Lab Work: Assemble and Link moves.asm
The following program demonstrates the MOV, MOVZX, MOVSX, and XCHG instructions: Open and view this program in ConTEXT. Assemble and link this program to produce the moves.exe
executable file, by pressing F9, or pressing . You can use the make32 batch file from the command prompt. TITLE Data Transfer Examples (File: moves.asm)
; Demonstration of MOV, MOVZX, MOVSX, and XCHG
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
var1 WORD 1000h
var2 WORD 2000h
4 Assembly Language LAB
.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
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 moves.exe 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 Assembly Language LAB
Try first to guess the values of the registers and memory variables in the above program after the execution of each instruction. Write your answers in hexadecimal in the specified boxes. Place the cursor at the beginning of the main procedure and press F7. Now step through the program by pressing F10 and watch the changes in registers and variables. Make the necessary corrections to your answers. MOV and MOVZX
1) ax(hex) = 2) ecx (hex) = 3) bx (hex) = 4) edx (hex) =
MOVSX
5) bx (hex) = 6) ecx (hex) = 7) edx (hex) =
XCHG 8) ax (hex) = var1 (hex) = 9) ax (hex) = var2 (hex) = 10) ax (hex) = var1 (hex) =
6 Assembly Language LAB
2. Addition and Subtraction
INC and DEC Instructions
The INC (increment) adds 1 to a single operand. INC reg/mem
The DEC (decrement) subtracts 1 from a single operand. DEC reg/mem
The INC and DEC instructions affect Overflow, Sign, Zero, Auxiliary Carry, and Parity flags. Do not affect the Carry flag.
ADD Instruction
The ADD instruction adds a source operand to a destination operand of the same size.
ADD dest, source
Source is unchanged by the operation, and the sum is stored in the destination operand. The set of possible operands is the same as for the MOV instruction.
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed according to the value that is placed in the destination operand.
SUB Instruction The SUB instruction subtracts a source operand from a destination operand.
SUB dest, source
Internally, the CPU can implement subtraction as a combination of negation and addition. Two’s-complement notation is used for negative numbers. 4 - 1 can be rewritten as 4 + (-1). -1 is represented by 11111111.
7 Assembly Language LAB
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed according to the value that is placed in the destination operand.
NEG Instruction The NEG (negate) instruction reverses the sign of a number by converting the number to its two’s complement.
NEG reg
NEG mem The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed according to the value that is placed in the destination operand. Notes:
- The Carry and Auxiliary Carry flags always set after NEG instruction, except NEG 0 the Carry and Auxiliary Carry flags are zeros, and NEG any number its least 4 bits are zeros the Auxiliary Carry flags is zero.
- The Overflow flag always zero after NEG instruction, except NEG the smallest signed number the Overflow flag is set.
Flags Affected by Addition and Subtraction The Carry flag indicates unsigned integer overflow. For example, if an instruction has an
8-bit destination operand but the instruction generates a result larger than 11111111 binary, the Carry flag is set. - In ADD CF = (carry out of the MSB) - In SUB CF = INVERT (carry out of the MSB)
The Overflow flag indicates signed integer overflow. For example, if an instruction has an 8-bit destination operand but it generates a negative result smaller than 10000000 binary, the Overflow flag is set. The Overflow flag is only set when: - Two positive operands are added and their sum is negative. - Two negative operands are added and their sum is positive. - OF = (carry out of the MSB) XOR (carry into the MSB).
The Zero flag indicates that an operation produced zero. For example, if an operand is subtracted from another of equal value, the Zero flag is set.
The Sign flag indicates that an operation produced a negative result. If the most significant bit (MSB) of the destination operand is set, the Sign flag is set.
The Parity flag indicates whether or not an even number of 1 bits occurs in the least significant byte of the destination operand, immediately after an arithmetic or boolean instruction has executed.
The Auxiliary Carry flag is set when a 1 bit carries out of position 3 in the least significant byte of the destination operand. - In ADD AF = (carry out of the third bit) - In SUB AF = INVERT (carry out of the third bit)
8 Assembly Language LAB
Lab Work: Assemble and Link SimpleArith.asm
The following program demonstrates the ADD, SUB, NEG, INC and DEC instructions: Open and view this program in ConTEXT. Assemble and link this program to produce the moves.exe
executable file, by pressing F9, or pressing . You can use the make32 batch file from the command prompt. TITLE Simple Arithmetic (SimpleArith.asm)
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
; No data
.code
main PROC
; ADD
mov eax, 91ab0748h
mov ebx, 3f54f8f2h
add eax, ebx
; 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
9 Assembly Language LAB
Lab Work: Trace the Execution of Program SimpleArith.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 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: 1) EAX after ADD (hex) = CF= OF= SF= ZF= PF= 2) EAX after SUB (hex) = CF= OF= SF= ZF= PF= 3) EAX after NEG (hex) = CF= OF= SF= ZF= PF= 4) EAX after INC (hex) = CF= OF= SF= ZF= PF= 5) EAX after DEC (hex) = CF= OF= SF= ZF= PF= 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. Check the answers that you have guessed above.
10 Assembly Language LAB
Lab Exercise
Write a program that implements the following arithmetic expression:
EAX = −val2 + 7 − val3 + val1
Use the following data definitions:
val1 SDWORD 8
val2 SDWORD -15
val3 SDWORD 20
Trace the execution of the program and view the Registers window using the windows debugger.
Homework Exercises
Write a program that implements the following arithmetic expression:
EAX = 2 – array[0] – (7 – array[1]) + array[2]
Use the following data definition:
array SBYTE 27, 10, 90
Trace the execution of the program and view the Registers window using the windows debugger. (write the final value of EAX, cf, of, sf, zf, and pf flags)
Best Wishes