0% found this document useful (0 votes)
60 views14 pages

A2 Coal

The document provides instructions for Assignment #2 on computer organization and assembly language. It includes 7 questions asking students to write assembly code that performs various tasks like updating the stack, passing arguments through registers and stack, writing recursive procedures, and sorting arrays by modifying given bubble sort code. Students are asked to use their roll number to declare unique variables and identify characters of their name.

Uploaded by

Rizwan razi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views14 pages

A2 Coal

The document provides instructions for Assignment #2 on computer organization and assembly language. It includes 7 questions asking students to write assembly code that performs various tasks like updating the stack, passing arguments through registers and stack, writing recursive procedures, and sorting arrays by modifying given bubble sort code. Students are asked to use their roll number to declare unique variables and identify characters of their name.

Uploaded by

Rizwan razi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2024)


))
ID: __________ NAME: __________________ SECTION: ____

Read the Instructions Carefully


 Understanding of the questions is also a part of this assignment.
 You are required to solve the assignment on this document and submit it on GCR (SoftCopy)
 You have to use your Roll No and consider it as decimal for the unique declaration purpose.
 Exclude the characters and the first two digits from your Roll number.
FOR EXAMPLE: If your number is 22i-7823, use 7823, where A0 assign the first digit of it.
Assign Digit Assign Assign Digit 2 Assign Digit 3
0 Digit 1
Short for Assigned Digit A0 A1 A2 A3
Write Assigned Number Digit By 7 8 2 3
Digit
Assigned Byte 0 Assigned Byte 1
Short for Assigned BYTE B0 B1
Byte in DECIMAL 78 23
Assigned WORD
Short for Assigned WORD W
WORD in DECIMAL(W) 7823
Convert WORD to HEX (WH)
Convert byte BINARY (WB)
Assigned double WORD in HEXA 78237823H
(DH)

EXAMPLE: Name is HAMZA DAUD


 If your name starts with MUHAMMAD kindly use your second name

ZERO FIRST SECOND THIRD FOURTH FIFTH SIXTH


CHARACTE CHARACTE CHARACTE CHARACTE CHARACTE CHARACTE CHARACTE
R OF YOUR R OF YOUR R OF YOUR R OF YOUR R OF YOUR R OF YOUR R OF YOUR
NAME NAME NAME NAME NAME NAME NAME
Short for
CHARACTE C0 C1 C2 C3 C4 C5 C6
R
YOUR
NAME
CHARACTE
H A M Z A D A
R BY
CHARACTE
R
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2024)
1. Update stack after running assembly code given below:
))

STACK AT
END OF
FILL PROGRAM
STACK

00C6 00C6

00C8 00C8

00CA 00CA

00CC 00CC

00CE 00CE

00E0 00E0

00E2 00E2

00E4 00E4

00E6 00E6

00E8 00E8

00EA 00EA

00EC 00EC

00FE 00FE

00F0 00F0

00F2 00F2

00F4 00F4

00F6 00F6

00F8 00F8

00FA 00FA

00FC 00FC

00FE 00FE
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

2. Convert the following code such that instead of storing the operand values in the registers, it
uses the stack to pass the arguments and get the result and Update the stack.
.data
result db 0
.code
mov eax, A0
mov ebx, B0
call add_numbers
mov [result], eax
mov eax, A0
mov ebx, B0
call subtract_numbers
mov [result], eax
mov eax, A0
mov ebx, B0
call multiply_numbers
mov [result], eax
mov eax, A0
mov ebx, B0
call divide_numbers
mov [result], eax
add_numbers proc
add eax, ebx
ret
add_numbers endp
subtract_numbers proc
sub eax, ebx
ret
subtract_numbers endp
multiply_numbers proc
imul eax, ebx ; Signed multiplication
ret
multiply_numbers endp
divide_numbers proc
idiv ebx ; Signed division
ret
divide_numbers endp

Page 3 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

3. Write a procedure ARRAY_SUM that sums the ARRAY given below. Parameters are
passed through registers. Parameters include ARRAY, ARRAY SIZE and the total is
saved in SUM.

ARRAY dw 5h, A0H,6, 7, B0b


SUM db 0

Page 4 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

4. Create a procedure named find_smallest that receives two parameters (using registers):
a pointer to a signed word array, and a count of the array’s length. The procedure must
return the value of the smallest array member in AX. Preserve all registers (except AX)
that are modified by the procedure.

Page 5 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

5. Convert the below C++ code to assembly code. Store the arguments in registers.

int findSum(int Arr[], int N) {


if (N <= 0) {
return 0;
}
return (findSum(Arr, N-1) + Arr[N-1]);
}

int main() {
int A[] = {A0, A1, B0, B1, 65, 76};
int N = sizeof(A)/sizeof(A[0]);
int sum = findSum(A,N);
}

Page 6 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

6. Write a recursive procedure in assembly language that finds xy. Use stack to pass the
arguments (x and y) to the procedure. Also, update the below stack after running your
code.

FILL STACK STACK AT END OF


PROGRAM

00C6 00C6

00C8 00C8

00CA 00CA

00CC 00CC

00CE 00CE

00E0 00E0

00E2 00E2

00E4 00E4

00E6 00E6

00E8 00E8

00EA 00EA

00EC 00EC

00FE 00FE

00F0 00F0

00F2 00F2

00F4 00F4

00F6 00F6

00F8 00F8

00FA 00FA

00FC 00FC

00FE 00FE

Page 7 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

7. Modify the bubble sort code given below for array1 & array2 so that parameters (offset &
length) are passed through stack and create the local variable (swap) on the stack. Also, write
the main procedure to call this procedure.
.data
array1 db 8,-6,3,4
array2 db 8,7,6,5,4,3,2
.code
sort_ary proc
iteration:
mov [bp-2], 0
mov si,0

loop1:
mov al, [bx+si]
cmp al, [bx+si+1]
jbe noswap
mov dl, [bx+si+1]
mov [bx+si+1], al
mov [bx+si],dl
mov [bp-2],1
noswap:
add si,1
cmp si, cx
jne loop1

cmp [bp-2],1
je iteration
pop bp
ret 4

sort_ary ENDP

Page 8 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

8. Convert the below C++ recursive code to assembly code. Store the arguments and
result in Stack. Also show the stack.

// Recursive C++ program to find minimum


#include <iostream>
using namespace std;

// function to print Minimum element using recursion


int findMinRec(int A[], int n)
{
// if size = 0 means whole array has been traversed
if (n == 1)
return A[0];
return min(A[n-1], findMinRec(A, n-1));
}
// driver code to test above function
int main()
{
int A[] = {1, 4, 45, 6, -50, A0, 2};
int n = sizeof(A)/sizeof(A[0]);
cout << findMinRec(A, n);
return 0;
}

Page 9 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

9. Write equivalent Assembly instructions for the C++ code given below and also explain C++
code in given space?

C++ Code Assembly Code


#include <iostream>
using namespace std;
int main() {
int a = 4578;
int n = 4;
for (int i = 1; i <= n; ++i) {
int rightmostBit = a & 1;
a >>= 1;
a |= (rightmostBit << 5);
}
int b = a;
return 0; }
Explanation:

10. Update Flags after executing following code?


NOTE: AND performs a Boolean(bitwise) AND operation between each pair of matching bits in two
operands and place result in the destination. AND instruction always clear overflow and carry flags. It
modifies Sign, Zero and Parity flags.

Sign
mov al,0AEH Zero
and al,246
Carry
Flags
Overflow
Parity
Auxiliary

11. Update Flags after executing following code


NOTE: OR performs a Boolean(bitwise) OR operation between each pair of matching bits in two operands and
place result in the destination. OR instruction always clear overflow and carry flags. It modifies Sign, Zero and
Parity flags.

mov al,11100011b
OR al,00000100b ; setting 3rd bit

Page 10 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

12. Update memory after executing code given below


CODE
.data
ary db 26 dup(?)
arysize= $-ary
ary_copy db arysize dup(0)
endmem db 1
.code

main PROC
mov esi,OFFSET ary
mov edi,OFFSET ary_copy
mov ax,000FFh
mov ecx,arysize
and al,061h
mov bl,al
and bl,0CFH ;masking
L1:
mov [esi],al
mov [edi],bl
inc esi
inc edi
inc al
inc bl
LOOP L1
INVOKE ExitProcess,0
main ENDP
END main
MEMORY

0 1 2 3 4 5 6 7 8 9 A B C D E F
4000
4010
4020

Sign
Zero
Carry
Flags
Overflow
Parity
Auxiliary

Page 11 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

13. Update memory after executing code given below


CODE
.data
ary db 26 dup(?)
arysize= $-ary
ary_copy db arysize dup(0)
endmem db 1

.code
mov esi,OFFSET ary
mov edi,OFFSET ary_copy
mov ax,0041h
mov ecx,arysize
mov bl,al
OR bl,00100000b
L1:
mov [esi],al
mov [edi],bl
inc esi
inc edi
inc al
inc bl
LOOP L1

MEMORY

0 1 2 3 4 5 6 7 8 9 A B C D E F
4000
4010
4020

Page 12 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

14. Update register after each line of code and update Flags after executing the following code

mov ax,0A593H AX
XOR ax,-1 Sign AX
XOR ax,0 AX
Zero
XOR ax,-1 AX
XOR ax,ax Carry
Flags AX
mov ax, 05A37H Overflow
XOR al,0 Parity AX
Al
XOR ah,1 Auxiliary
XOR al,ah
AH
AL

INSTRUCTIONS: (Question 15-16) Perform each of the following operations on word size 2’s complement
numbers and update the answer and value of flags after performing the arithmetic. Perform all the steps in the
“calculation” box, only filling the answer will not get any credit.
15. Update flags after arithmetic instruction? Also state which of the following jumps will taken or
not taken

mov bx,0FABDh
TAKEN NOT TAKEN
add bx,0684Ah
jc l1 Jc
L1: jz L2 Jz
L2: jno L3 Jno
L3: jns L4
L4: jp L5 Jns
L5: mov ah,04ch jp

Sign Calculation
Zero
Carry
Flags
Overflow
Parity
Auxiliary

Page 13 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

16. Update value of ax and cx registers after every iteration. Update any changes to the done to
flag

mov ecx,5
mov ax,1 1 2 3 4 5 6
L1: CX
inc ax
AX
dec ecx
jcxz end_loop
jmp L1
end_loop:

Sign Calculation
Zero
Carry
Flags
Overflow
Parity
Auxiliary

17. Fill flag after every CMP instruction

+40 NOT Sign Calculation


TAKEN TAKEN
-31 Zero
Ja
Jg Carry
Flags
Overflow
Parity
Auxiliary

+95
NOT Sign Calculation
-100
TAKEN TAKEN Zero
JNG
Carry
JNGE Flags
Overflow
JGE
Parity
Auxiliary

Page 14 of 14

You might also like