Professional Documents
Culture Documents
Coal Assignment
Coal Assignment
Page 1 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
Short for
C0 C1 C2 C3 C4 C5 C6
CHARACTER
YOUR NAME
CHARACTER BY
CHARACTER
M I S H A L A
1. Write equivalent Assembly instructions for the C++ code given below and also explain C++ code in
given space?
#include <iostream>
using namespace std;
int main() {
.data
int a = 4578; a dd 4578
int n = 5; n dd 5
for (int i = 1; i <= n; ++i) { rightMostBit d 0
int rightmostBit = a & 1;
a >>= 1; b dd 0
a |= (rightmostBit << 7); .code
} main PROC
int b = a;
return 0; }
mov ecx, n
L1:
mov ax, a
mov rightMostBit, ax
and rightMostBit, 1
sar a, 1
sal rightMostBit, 7
mov ax, rightMostBit
or a, ax
loop L1
mov ax, a
mov b, ax
Move n to ecx to set the counter loop. Then moving ax to rightmostbit copies the value of a in it. Sar
divides a by 2 followed by Sal to mov the rightmostbit to leftmost position by 6bits. Finally the or
Page 2 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
command replaces the rihgtmostbit with the previous val of riightmostbit variable. After the loop
ends final value of a is copied to b.
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 1
mov al,0AEH Zero 0
and al,246 Carry 0
Flags
Overflow 0
Parity 1
Auxiliary 0
3. 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
Page 3 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
0 1 2 3 4 5 6 7 8 9 A B C D E F
6
4000 1 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70
7 7
4010 1 2 73 74 75 76 77 78 79 7a 41 42 43 44 45 46
4020 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56
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 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50
5
4010 1 52 53 54 55 56 57 58 59 5a 61 62 63 64 65 66
6
4020 7 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76
Page 5 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
6. Update register after each line of code and update Flags after executing the following code
AX 42387 0xa593
Sign 0 AX 23148 0x5a6c
Zero 0 AX 23148 0x5a6c
Carry 0 AX 42387 0xa593
Flags AX 0 0
Overflow 0
Parity 1 AX 23095 0x5a37
Auxiliary 0 Al 55 0x37
AH 91 0x5b
AL 108 0x6c
mov ax,0A593H
XOR ax,-1
XOR ax,0
XOR ax,-1
XOR ax,ax Page 6 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
7. Write a program that finds parity of number given below? HINT: Use XOR and LOOP to find parity
.data
parity DQ 0A1B2C3D4E5F67890H
result Db 0
Answer:
.code
Main PROC
Loop1:
Movzx edi, byte ptr [esi]
Xor al, al
Mov al, byte ptr [esi]
Shr edi,8
Dec ecx
Jnz loop1
Test al,al
Jnz loop2
Jmp loop3
Page 7 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
Done:
INVOKE ExitProcess , 0
Main ENDP
END main
INSTRUCTIONS: (Question 8-11) 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.
8. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not
taken
Sign 1 Calculation
Zero 0 FFFE
+1FC70
Carry 1
FC6E
Flags Overflow 0 CF = 1, jumps to L1
0 ZF=0, move to L2
Parity OF=0, move to L3
SF=1, jump to L4
PF=0, move to L5
Page 8 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
9. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not
taken
.code TAKEN NOT TAKEN
mov ax,07B1Ah
sub ax,0CEEBh Jc 1 0
jc l1 Jz 0 1
L1: jz L2
L2: jo L3 Jo 1 0
L3: js L4 Js 1 0
L4: jp L5
L5: jp 0 1
Sign 1 Calculation
Zero 0 7B1A
- CEEB
Carry 1
AC2F
Flags Overflow 1 CF = 1, jump to L1
0 ZF=0, move to L2
Parity OF=1, jump to L3
SF=1, jump to L4
PF=0, move to L5
10. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not taken
Page 9 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
Page 10 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
11. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not taken
Sign 0 Calculation
Zero 0 "mov ax,OFABDh" command moves 1111 1010 1011 1101 to ax
"add ax,0684Ah"command adds 0110 1000 0100 1010 to ax
Carry 1
Giving answer 1110 0000 0111 0111 with, CF=1 and AF=1
Flags Overflow 0
Parity 0
Auxiliar 1
y
12. Update value of ax and cx registers after every iteration. Update any changes to the done to flag
mov ecx,5
1 2 3 4 5 6
mov ax,1
L1: CX 5 4 3 2 1 0
inc ax AX 1 2 3 4 5 6
dec ecx
jcxz end_loop
jmp L1
end_loop:
Page 11 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
Sign 0 Calculation
Zero 0 Bx is +32
And is compared
Carry 1
with -35
Flags Overflow 0 Jge is taken as
0 32>=-35
Parity Hence it jumps to
Auxiliar 1 L5
NOT y
TAKEN TAKEN
JNG 0 1
JNG
E 0 1 Page 12 of 26
JGE 1 0
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
N Sign 0 Calculation
O Zero 1 Cx is zero and
compared with
T Carry 0
zero
T Overflow 0 Jg is false hence
A T Flags
1 itskips
Parity
K A Jnl also skipped as
Auxiliar 0 its not less
E K
y Jge takes it as 0>=0
N E hence it jumps to
N L2
Sign 0 Calculation
J
0 1 Cx is taken as 0 and
Zero 1
J 1 0 compared to 0
Flags Carry J 1 Jl is skipped NOT
as its
0
0 TAKEN
not less TAKEN
Overflow 0 JL Jng0is also skipped
1
Jge takes it as 0>=0
JNG Hence
1 it jumps 0 to L2
Parity 1 0JGE 0 1
14. Dry run program gives below update table for each iteration count total number of iterations took by
the program
CODE
.data
data dw 66h,AO,045h,A1,040h,A2,-025h,A3,-010h,011h
swap db 0
.code
main PROC
start:
mov swap,0 ;Reset swap flag to no swap
mov ebx,0 ;initialize array index to zero
Page 13 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
loop1:
mov ax,[ebx+data] ;load number to ax
cmp ax,[ebx+data+2] ;compare with next number
jge noswap ;no swap if already inorder
INVOKE ExitProcess,0
main ENDP
END main
COUNT SWAP
66 1 45 2 40 9 -25 1 -10 11 0 0
66 1 45 2 40 9 -25 1 -10 11 2 0
66 45 1 2 40 9 -25 1 -10 11 4 1
66 45 2 1 40 9 -25 1 -10 11 6 1
66 45 2 40 1 9 -25 1 -10 11 8 1
66 45 2 40 9 1 -25 1 -10 11 10 1
66 45 2 40 9 1 -25 1 -10 11 12 0
66 45 2 40 9 1 1 -25 -10 11 14 1
66 45 2 40 9 1 1 -10 -25 11 16 1
66 45 2 40 9 1 1 -10 11 -25 18 1
66 45 2 40 9 1 1 -10 11 -25 2 0
66 45 2 40 9 1 1 -10 11 -25 4 0
66 45 40 2 9 1 1 -10 11 -25 6 1
66 45 40 9 2 1 1 -10 11 -25 8 1
Page 14 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
66 45 40 9 2 1 1 -10 11 -25 10 0
66 45 40 9 2 1 1 -10 11 -25 11 0
66 45 40 9 2 1 1 -10 11 -25 12 0
66 45 40 9 2 1 1 11 -10 -25 14 0
66 45 40 9 2 1 1 11 -10 -25 16 1
66 45 40 9 2 1 1 11 -10 -25 18 0
66 45 40 9 2 1 1 11 -10 -25 2 0
66 45 40 9 2 1 1 11 -10 -25 4 0
66 45 40 9 2 1 1 11 -10 -25 6 0
66 45 40 9 2 1 1 11 -10 -25 8 0
66 45 40 9 2 1 1 11 -10 -25 10 0
66 45 40 9 2 1 1 11 -10 -25 12 0
66 45 40 9 2 1 11 1 -10 -25 14 1
66 45 40 9 2 1 11 1 -10 -25 16 0
66 45 40 9 2 1 11 1 -10 -25 18 0
66 45 40 9 2 1 11 1 -10 -25 2 0
66 45 40 9 2 1 11 1 -10 -25 4 0
66 45 40 9 2 1 11 1 -10 -25 6 0
66 45 40 9 2 1 11 1 -10 -25 8 0
66 45 40 9 2 1 11 1 -10 -25 10 0
66 45 40 9 2 11 1 1 -10 -25 12 1
66 45 40 9 2 11 1 1 -10 -25 14 0
66 45 40 9 2 11 1 1 -10 -25 16 0
66 45 40 9 2 11 1 1 -10 -25 18 0
66 45 40 9 2 11 1 1 --10 -25 2 0
66 45 40 9 2 11 1 1 -10 -25 4 0
Page 15 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
66 45 40 9 2 11 1 1 -10 -25 6 0
66 45 40 9 2 11 1 1 -10 -25 8 0
66 45 40 9 11 2 1 1 -10 -25 10 1
66 45 40 9 11 2 1 1 -10 -25 12 0
66 45 40 9 11 2 1 1 -10 -25 14 0
66 45 40 9 11 2 1 1 -10 -25 16 0
66 45 40 9 11 2 1 1 -10 -25 18 0
66 45 40 9 11 2 1 1 -10 -25 2 0
66 45 40 9 11 2 1 1 -10 -25 4 0
66 45 40 9 11 20 1 1 -10 -25 6 0
66 45 40 11 9 2 1 1 -10 -25 8 1
66 45 40 11 9 2 1 1 -10 -25 10 0
66 45 40 11 9 2 1 1 -10 -25 12 0
66 45 40 11 9 2 1 1 -10 -25 14 0
66 45 40 11 9 2 1 1 -10 -25 16 0
66 45 40 11 9 2 1 1 -10 -25 18 0
15. You are working on the development of an encryption process for a secure communication protocol for a
highly sensitive application. As part of the encryption process, the system needs to perform a series of
bitwise operations on the provided data. Your task is to design and implement an assembly program that
can do these series of transformations on a 32-bit integer number mDATA.
Suppose an 16-bit number mFLAGS, you have to transform the data by applying the series of following 4-
bitwise operations on mDATA according to the specific bit value in mFLAGS as defined in the operation
description provided below:
Note: Not allowed to use any predefined rotate instruction like (ROL,ROR etc.)
Page 16 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
its first 5 bits. Ensure that the rotation is circular, meaning bits shifted out from
one end are rotated back to the other end.
Note: Not allowed to use any predefined rotate instruction like (ROL,ROR etc.)
You must provide your permutation table in a char array in the data section.
For Example:
the above permutation table can be represented in char array as “160709 … 15”,
where the bit positions are in ascending order,
i.e., 16 is the new position for bit 0,
and 07 is the new position for bit 1,
and so on.
Note: You have to generate 16-bit number mFLAGS using your Roll Number excluding the character and the
first digit. For example if the number is 22i-9986 the mFLAGS must be 29986.
Answer:
Page 17 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
16. Dry run program gives below update table for each iteration count total number of iterations took by
the program
CODE
.data
Multiplicand db 78 ; Consider Assigned Number Digit
A0
Multiplier db 23 ; Consider Assigned Number Digit A3
Result db 0
.code
mov ax,0
mov cl,4
mov al,multiplicand
mov bl,multiplier
checkbit:
shr bl,1
jnc skip
add result,al
skip:
shl al,1
loop checkbit
al bl
CF multiplicand Multiplier CF result
0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0
17. Consider the following code and fill given registers and memory accordingly after each step?
CODE
; NOTE ;ADC is add through carry
Page 18 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
.code
mov ax,0
mov ecx,16 ;initialize bit count to 16
mov dx, multiplier ;initialize bit mask
checkbit:
shr dx,1
jnc noadd ;skip addition if no carry
bx ax
C C
Multiplicand+2 Multiplicand
F F
0A 0C
0
1B 3D
dx
C
multiplier Result+2 Result
F
00 0
55 0
bx ax
C C
Multiplicand+2 Multiplicand
F F
0A1 086
1
B A
dx
C
multiplier Result+2 Result
F
00
0
2A 0
bx ax
C C
Multiplicand+2 Multiplicand
F F
0A 00
0
1B D4
Page 19 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
dx
C
multiplier Result+2 Result
F
00
0
15 0
bx ax
C C
Multiplicand+2 Multiplicand
F F
0A 01
1 1B A8
dx
C
multiplier Result+2 Result
F
00
00
0A 0
bx ax
C C
Multiplicand+2 Multiplicand
F F
0A1 03
0 B 50
dx
C
multiplier Result+2 Result
F
00 0
0
05
bx ax
C C
Multiplicand+2 Multiplicand
F F
dx
C
multiplier Result+2 Result
F
bx ax
C C
Multiplicand+2 Multiplicand
F F
dx
C
multiplier Result+2 Result
F
bx ax
C C
Multiplicand+2 Multiplicand
F F
Page 20 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
dx
C
multiplier Result+2 Result
F
18. Modify and Rewrite the code given above for following data declaration?
.data
multiplicand DQ DH ;value of Assigned doubleword in hexa
multiplier DW WH ;Assigned word in hexa
result DQ 0 ;result of the multiplication
Answer:
.code
Main PROC
Main ENDP
END main
Page 21 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
19. Perform unsigned binary multiplication using following given flow chart?
NOTE: Your computer width is 8-bit, Multiplicand is (B0B)2 of the assigned number and Multiplier is (B1B)2 of the
assigned number
0 0 00001000 00110001
0
C A (ACCUMULATOR) Q (MULTIPLIER) M (MULTIPLICAN
0
0000010 0011000
0 0 0 1
C A (ACCUMULATOR) Q (MULTIPLIER) M (MULTIPLICAN
0
0011000
0 0 00000010 1
C A (ACCUMULATOR) Q (MULTIPLIER) M (MULTIPLICAN
0
0000000 0011000
0 0 1 1
C A (ACCUMULATOR) Q (MULTIPLIER) M (MULTIPLICAN
0011000 0000000 0011000
0 1 1 1
Page 22 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
Page 23 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
Q-
A (ACCUMULATOR) Q (MULTIPLIER) M (MULTIPLICA
1
001010010 111100111
0000110001 1 1
100101001
0000011000 0 1
100101001 111100111
1111100111 0 1
110010100 111100111
1111110011 1 0 1
111100111
0000100100 1100101001 1
011001010 111100111
0000010010 0 1 1
011001010 111100111
1111100001 0 1
101100101 111100111
1111110000 0 0 1
111100111
1
010110010 111100111
1111111000 1 0 1
010110010
0000101001 1 1111001111
101011001 111100111
0000010100 0 1 1
101011001 111100111
1111100011 0 1
110101100 111100111
1111110001 1 0 1
111100111
0000100010 110101001 1
011010110 111100111
0000010001 0 1 1
011010110 111100111
1111100000 0 1
001101011 111100111
111111000 0 0 1
Page 24 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
000110101 111100111
1111111000 1 0 1
Page 25 of 26
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)
Answer:
Page 26 of 26