Coa Lab 2
Coa Lab 2
Faculty of Computing
UNIVERSITI TEKNOLOGI MALAYSIA
SEMESTER: 2 – 2023/2024
Student 1 Student 2
No. 1, 3, 5 No 2, 4, 6
2 weeks
& ITEMS: 1. Lab 2 exercise sheet/file (in .pdf), with the links for demo
MARKS:
________
1
Arithmetic Equation Coding in Assembly Language
Q1. Execute the program below. Determine output of the program by inspecting the content of the related
registers.
a) Fill in Table 1 with the content of each register or variable on every LINE, in Hexadecimal (as per the
output). Please complete the comments for every LINE.
b) Paste the screenshot of all registers’ content after each LINE is executed.
INCLUDE Irvine32.inc
.data
var1 word 1
var2 word 9
.code
main PROC
mov ax, var1 ; LINE1
mov bx, var2 ; LINE2
xchg ax, bx ; LINE3
mov var1, ax ; LINE4
mov var2, bx ; LINE5
call DumpRegs
exit
main ENDP
END main
Answer Q1
a) Fill (Write) in the contents for the related register in each line:
0009h
Move the value of var2 (9d) into register BX
0009h
0009h Swap the value inside AX and BX to one
0001h another
0009h Move the value stored inside AX into var1 (9d)
0009h
0001h Move the value stored inside BX into var1 (1d)
0001h
2
b) Paste here screenshot of all registers’ content after each LINE is executed:
LINE1:
LINE2:
LINE3:
LINE4:
LINE5:
3
Q2. Execute the program below. Determine output of the program by inspecting the content of the related
registers and watches.
a) Fill in Table 2 with the content of each register or variable on every LINE, in Hexadecimal (as per the
output). Please complete the comments for every LINE.
b) Paste the screenshot of all registers’ content after each LINE is executed.
Arithmetic expression: Rval = (-Xval + (Yval – Zval)) + 1
include irvine32.inc
.data
Rval DWORD ?
Xval DWORD 26
Yval DWORD 30
Zval DWORD 40
.code
main proc
mov eax,Xval ; LINE1
neg eax ; LINE2
mov ebx,Yval ; LINE3
sub ebx,Zval ; LINE4
add eax,ebx ; LINE5
inc eax ; LINE6
mov Rval,eax ; LINE7
exit
main endp
end main
Answer Q2
a) Fill (Write) in the contents for the related register in each line:
4
b) Paste here screenshot of all registers’ content after each LINE is executed:
LINE1:
LINE2:
LINE3:
5
LINE4:
LINE5:
LINE6:
LINE7:
6
Q3. Execute the program below. Determine output of the program by inspecting the content of the related
registers.
a) Fill in Table 3 with the content of each register or variable on every LINE, in Hexadecimal (as per the
output). Please complete the comments for every LINE.
b) Paste the screenshot of all registers’ content after each LINE is executed.
Arithmetic expression: var4 = [(var1 * var2) + var3] - 1
include irvine32.inc
.data
var1 DWORD 5
var2 DWORD 10
var3 DWORD 20
var4 DWORD ?
.code
main proc
mov eax, var1 ; LINE1
mul var2 ; LINE2
add eax, var3 ; LINE3
dec eax ; LINE4
exit
main endp
end main
Answer Q3
a) Fill (Write) in the contents for the related register in each line:
Table 3
7
b) Paste here screenshot of all registers’ content after each LINE is executed:
LINE1:
LINE2:
LINE3:
LINE4:
8
Q4. Execute the program below. Determine output of the program by inspecting the content of the related
registers.
a) Fill in Table 4 with the content of each register or variable on every LINE, in Hexadecimal (as per the
output). Please complete the comments for every LINE.
b) Paste the screenshot of all registers’ content after each LINE is executed.
include irvine32.inc
.data
var1 WORD 40
var2 WORD 10
var4 WORD ?
.code
main proc
mov ax,var1 ; LINE1
mov bx,5 ; LINE2
mul bx ; LINE3
mov bx,var2 ; LINE4
sub bx,3 ; LINE5
div bx ; LINE6
mov var4,ax ; LINE7
exit
main endp
end main
Answer Q4
a) Fill (Write) in the contents for the related register in each line:
Table 4
001Ch
0007h Divides AX by BX (200/7=28)
0004h
001Ch
Moves the value in register AX to var4
001Ch
9
b) Paste here screenshot of all registers’ content after each LINE is executed:
LINE1:
LINE2:
LINE3:
LINE4:
10
LINE5:
LINE6:
LINE7:
11
Short Notes for MUL CX and DIV BL:
MUL CX
a. MUL always uses AX (or its extended versions EAX or RAX) as the implicit destination register.
b. The operand size determines the size of the result:
c. The upper half of the result (DX or EDX or RDX) holds any overflow bits.
d. The Carry Flag (CF) is set if the upper half of the product is non-zero.
DIV BL
a. DIV always uses the DX:AX or EDX:EAX pair as the implicit dividend register.
b. The divisor is specified as the operand of the DIV instruction.
c. The quotient is stored in AX (for 16-bit division) or EAX (for 32-bit division).
d. The remainder is stored in DX.
e. Clear DX (or EDX for 32-bit division) before division to ensure a correct 16-bit or 32-bit dividend.
f. If the divisor is 0, a division error occurs.
g. The Overflow Flag (OF) is set if the quotient is too large to fit in the destination register.
________________________________________________
12
Answer Q5
13
Q6. Given the following instructions as is Code Snippet 2.
Answer Q6
14
15