A2 Coal
A2 Coal
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.
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 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.
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.
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?
Sign
mov al,0AEH Zero
and al,246
Carry
Flags
Overflow
Parity
Auxiliary
mov al,11100011b
OR al,00000100b ; setting 3rd bit
Page 10 of 14
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
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)
.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
+95
NOT Sign Calculation
-100
TAKEN TAKEN Zero
JNG
Carry
JNGE Flags
Overflow
JGE
Parity
Auxiliary
Page 14 of 14