I221154 F A2 Coal Merged

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


ID: i22-1154 NAME: Hania Aamer SECTION: F
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 0 Assign Digit Assign Digit 2 Assign Digit 3
1
Short for Assigned Digit A0 A1 A2 A3

Write Assigned Number Digit By Digit 1 1 5 4


Assigned Byte 0 Assigned Byte 1
Short for Assigned BYTE B0 B1
Byte in DECIMAL 11 54
Convert byte to HEX B0H, B1H A1 36
Convert byte OCTAL B0Q, B1Q 13 66
Convert byte BINARY B0B, B1B 1011 110110
Assigned WORD
Short for Assigned WORD W
WORD in DECIMAL(W)
1154
Convert WORD to HEX (WH) 482
Convert WORD OCTAL (WQ) 2202
Convert byte BINARY (WB) 10010000010
Assigned double WORD in HEXA (DH)

EXAMPLE: Name is HAMZA DAUD


❖ If your name starts with MUHAMMAD kindly use your second name
ZERO FIRST SECOND THIRD FOURTH FIFTH SIXTH
CHARACTER CHARACTER CHARACTER CHARACTER CHARACTER CHARACTE CHARACTE
OF YOUR OF YOUR OF YOUR OF YOUR OF YOUR R OF YOUR R OF YOUR
NAME NAME NAME NAME NAME NAME NAME

Page 1 of 2
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


Short for
C0 C1 C2 C3 C4 C5 C6
CHARACTER
YOURNAME
CHARACTER BY
CHARACTER
H A N I A A A
1. Write equivalent Assembly instructions for the C++ code given below and also explain C++ code in given
space?

++ Code Assembly Code

#include <iostream> .data


using namespace std; a dd 4578
int main() { b dd 0
int a = 4578; rightmostBit dd 0
int n = 5; .code
for (int i = 1; i <= n; ++i) { mov ecx,5
int rightmostBit = a & 1; loop_strt:
a >>= 1; mov rightmostBit,a
a |= (rightmostBit << 7); and rightmostBit,1
} shr a,1
int b = a; shl rightmostBit,1
return 0; } or a, rightmostBit
loop loop_strt
mov eax,a
mov b,eax

Explanation:
It takes an integer, extracts its rightmost digit, and then shifts it one time to the right. After that, it uses a to
execute an OR of the extracted rightmost bit's seven left shifts, saving the result in a. After completing this five
times, it initializes b using eax.

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

ID: 22i-1154 NAME: Hania Aamer SECTION:F


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 0 Assign Digit 1 Assign Digit 2 Assign Digit 3
Short for Assigned Digit A0 A1 A2 A3
Write Assigned Number Digit By Digit 7 8 2 3
Assigned Byte 0 Assigned Byte 1
Short for Assigned BYTE B0 B1
Byte in DECIMAL 78 23
Convert byte to HEX B0H, B1H

Convert byte OCTAL B0Q, B1Q

Convert byte BINARY B0B, B1B

Assigned WORD
Short for Assigned WORD W
WORD in DECIMAL(W) 7823
Convert WORD to HEX (WH)

Convert WORD OCTAL (WQ)

Convert byte BINARY (WB)

Assigned double WORD in HEXA (DH) 78237823H


EXAMPLE: Name is HAMZA DAUD
 If your name starts with MUHAMMAD kindly use your second name

ZERO FIRST SECOND THIRD FOURTH FIFTH SIXTH


CHARACTER CHARACTER CHARACTER CHARACTER CHARACTER CHARACTER CHARACTER
OF YOUR OF YOUR OF YOUR OF YOUR OF YOUR OF YOUR OF YOUR
NAME NAME NAME NAME NAME NAME NAME
Short for
C0 C1 C2 C3 C4 C5 C6
CHARACTER
YOUR NAME
CHARACTER BY
CHARACTER
H A M Z A D A

Page 1 of 15
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ASSIGNMENT#2 (FALL 2023)

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

C++ Code Assembly Code


#include <iostream> .data
using namespace std; a dd 4578
int main() { b dd 0
int a = 4578; rightmostBit dd 0
int n = 5;
.code
for (int i = 1; i <= n; ++i) { mov ecx,5
int rightmostBit = a & 1; loop_strt:
a >>= 1; mov rightmostBit,a
a |= (rightmostBit << 7); and rightmostBit,1
} shr a,1
int b = a; shl rightmostBit,1
return 0; } or a, rightmostBit
loop loop_strt
mov eax,a
mov b,eax
Explanation:
It takes an integer, extracts its rightmost digit, and then shifts it one time to the right. After that, it uses a to execute an OR of the
extracted rightmost bit's seven left shifts, saving the result in a. After completing this five times, it initializes b using eax.

2. 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 1
Zero 0
Carry 0
mov al,0AEH Flags
and al,246 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

Page 2 of 15
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 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70
4010 71 72 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
4030 57 58 59 5A 1

4. Update Flags after executing following code


Sign 1
NOTE: OR performs a Boolean(bitwise) OR operation between each pair
Zero 0
of matching bits in two operands and place result in the destination. OR
instruction always clear overflow and carry flags. It modifies Sign, Zero Carry 0
and Parity flags.
Flags
Overflow 0
mov al,11100011b Parity 1
OR al,00000100b ; setting 3rd bit
Auxiliary 0

5. Update memory after executing code given below

CODE
.data
ary db 26 dup(?)

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

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
4010 51 52 53 54 55 56 57 58 59 5A 61 62 63 64 65 66
4020 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76
4030 77 78 79 7A 01

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

mov ax,0A593H AX A5 93
XOR ax,-1 Sign AX 5A 6C
XOR ax,0 Zero AX 5A 6C
XOR ax,-1 AX A5 93
XOR ax,ax Carry AX 00 00
Flags
Overflow
mov ax, 05A37H
XOR al,0 Parity AX 5A 37
Al 5A 37
XOR ah,1 Auxiliary
XOR al,ah AH 5B 37
AL 5B 6C

Page 4 of 15
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

Answer:
.data
parity DQ 0A1B2C3D4E5F67890H
w dw 4 dup(0)

.code
main PROC
mov edi, offset w
mov ax, word ptr parity
mov [edi], ax
inc edi
inc edi
mov ax, word ptr parity + 2
mov [edi], ax
inc edi
inc edi
mov ax, word ptr parity + 4
mov [edi], ax
inc edi
inc edi
mov ax, word ptr parity + 6
mov [edi], ax

mov esi, offset w


mov edi, offset w
mov ecx, 3

Loop_strt:
inc edi
inc edi
mov dx, [esi]
mov bx, [edi]
xor [esi], bx
mov dx, [esi]
Loop Loop_strt

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

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

.code TAKEN NOT TAKEN


mov ax,0FFFEh
add ax,0FC70h Jc ----------------
jc l1 Jz ----------------
L1: jz L2
L2: jo L3 Jo ----------------
L3: js L4 Js ----------------
L4: jp L5
L5: jp ----------------

Sign 1 Calculation
Zero 0 1111 1111 1111 1110
+ 1111 1100 0111 0000
Flags Carry 1
1111 1010 0110 1110
Overflow 0
Parity 0

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 ----------------
jc l1 Jz
L1: jz L2 ----------------
L2: jo L3 Jo ----------------
L3: js L4
L4: jp L5
Js ----------------
L5: jp ----------------
Sign 1 Calculation
Zero 0 0111 1011 0001 1010
- 1100 1110 1110 1011
Carry 1
Flags 0111 1011 0001 1010
Overflow 1 + 0011 0001 0001 0101
Parity 0 1010 1100 0010 1111

10. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not taken

TAKEN NOT TAKEN

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

.code Jnc ----------------


mov cx,0FBCDh
add cx,0E4AAh
Jnz ----------------
jnc l1 Jo ----------------
L1: jnz L2
L2: jo L3 Js ----------------
L3: js L4 jnp ----------------
L4: jnp L5
L5: mov ah,04ch
Sign 1 Calculation
Zero 0 1111 1011 1100 1101
+ 1110 0100 1010 1010
Carry 1
1110 0000 0111 0111
Flags 0
Overflow
Parity 1
Auxiliary 1

11. Update flags after arithmetic instruction? Also state which of the following jumps will taken or not taken

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

Sign 0 Calculation
Zero 0 1111 1010 1011 1101
+ 0110 1000 0100 1010
Carry 1
0110 0011 0000 0111
Flags 0
Overflow
Parity 0
Auxiliary 1

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

1 2 3 4 5 6
CX 5 4 3 2 1 0

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

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

Sign 0 Calculation
Zero 1 -

Carry 0
Flags 0
Overflow
Parity 1
Auxiliary 0

13. Fill flag after every CMP instruction

NOT Sign 1 Calculation


TAKEN TAKEN Zero 0 0111 1111
Ja ------- 1
- 1000 0000
Carry ------------------------
Jg ------- Flags 1 1111 1111
Overflow
Parity 1
Auxiliary 0

NOT Sign 1 Calculation


TAKEN TAKEN Zero 0 1111 1111 1111 1111
- 0000 0000 0000 0000
Jnl ------- Carry 0
-------------------------------
Jnle Flags 0 1111 1111 1111 1111
------- Overflow
JL ------- Parity 1
Auxiliary 0

NOT
TAKEN TAKEN
JNG -------
JNGE -------

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

JGE -------
Sign 0 Calculation
Zero 0 0000000000100000
- 1111111111011101
Carry 1
-------------------------
Flags 0 0000000001000011
Overflow
Parity 0
Auxiliary 1

NOT Sign 0 Calculation


TAKEN TAKEN Zero 1 0000000000000000
- 0000000000000000
JG ------- Carry 0
----------------------------
Flags
JNL ------- Overflow 0 0000000000000000
JLE ------- Parity 1
Auxiliary 0

NOT Sign 0 Calculation


TAKEN TAKEN 0000000000000000
Zero 1
JL ------- Flags - 0000000000000000
Carry 0
JNG ----------------------------
-------
Overflow 0 0000000000000000
JGE -------

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,01h,045h,01h,040h,05h,-025h,04h,-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
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

mov dx,[ebx+data+2] ;load second element in dx


mov [ebx+data+2],ax ;store first number in second
mov[ebx+data],dx ;store second number in first
mov swap,1 ;flag that a swap has been done
noswap:

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

add bx,2 ;advance bx to next index


cmp bx,18 ;are we at last index
jne loop1 ;if not than compare next 2

cmp swap,1 ;check if swap has been done


je start ;if yes than make another pass

INVOKE ExitProcess,0
main ENDP
END main

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

COUNT SWAP
66 1 45 1 40 5 -25 4 -10 11 0 0

66 1 45 1 40 5 -25 4 -10 11 1 0

66 45 1 1 40 5 -25 4 -10 11 2 1

66 45 1 1 40 5 -25 4 -10 11 3 0

66 45 1 40 1 5 -25 4 -10 11 4 1

66 45 1 40 5 1 -25 4 -10 11 5 1

66 45 1 40 5 1 -25 4 -10 11 6 0

66 45 1 40 5 1 4 -25 -10 11 7 0

66 45 1 40 5 1 4 -10 -25 11 8 1

66 45 1 40 5 1 4 -10 11 -25 9 1

66 45 1 40 5 1 4 -10 11 -25 10 0

66 45 1 40 5 1 4 -10 11 -25 11 0

66 45 40 1 5 1 4 -10 11 -25 12 1

66 45 40 5 1 1 4 -10 11 -25 13 1

66 45 40 5 1 1 4 -10 11 -25 14 0

66 45 40 5 1 4 1 -10 11 -25 15 1

66 45 40 5 1 4 1 -10 11 -25 16 0

66 45 40 5 1 4 1 11 -10 -25 17 1

66 45 40 5 1 4 1 11 -10 -25 18 0

66 45 40 5 1 4 1 11 -10 -25 19 0

66 45 40 5 1 4 1 11 -10 -25 20 0

66 45 40 5 1 4 1 11 -10 -25 21 0

66 45 40 5 1 4 1 11 -10 -25 22 0

66 45 40 5 4 1 1 11 -10 -25 23 1

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

66 45 40 5 4 1 1 11 -10 -25 24 0

66 45 40 5 4 1 11 1 -10 -25 25 1

66 45 40 5 4 1 11 1 -10 -25 26 0

66 45 40 5 4 1 11 1 -10 -25 27 0

66 45 40 5 4 1 11 1 -10 -25 28 0

66 45 40 5 4 1 11 1 -10 -25 29 0

66 45 40 5 4 1 11 1 -10 -25 30 0

66 45 40 5 4 1 11 1 -10 -25 31 0

66 45 40 5 4 1 11 1 -10 -25 32 0

66 45 40 5 4 11 1 1 -10 -25 33 0

66 45 40 5 4 11 1 1 -10 -25 34 0

66 45 40 5 4 11 1 1 -10 -25 35 0

66 45 40 5 4 11 1 1 -10 -25 36 0

66 45 40 5 4 11 1 1 -10 -25 37 0

66 45 40 5 4 11 1 1 -10 -25 38 0

66 45 40 5 4 11 1 1 -10 -25 39 0

66 45 40 5 4 11 1 1 -10 -25 40 0
66 45 40 5 11 4 1 1 -10 -25 41 1

66 45 40 5 11 4 1 1 -10 -25 42 0

66 45 40 5 11 4 1 1 -10 -25 43 0

66 45 40 5 11 4 1 1 -10 -25 44 0

66 45 40 5 11 4 1 1 -10 -25 45 0

66 45 40 5 11 4 1 1 -10 -25 46 0

66 45 40 5 11 4 1 1 -10 -25 47 0

66 45 40 5 11 4 1 1 -10 -25 48 0

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

66 45 40 11 5 4 1 1 -10 -25 49 1
66 45 40 11 5 4 1 1 -10 -25 50 0
66 45 40 11 5 4 1 1 -10 -25 51 0
66 45 40 11 5 4 1 1 -10 -25 52 0
66 45 40 11 5 4 1 1 -10 -25 53 0
66 45 40 11 5 4 1 1 -10 -25 54 0
66 45 40 11 5 4 1 1 -10 -25 55 0
66 45 40 11 5 4 1 1 -10 -25 56 0
66 45 40 11 5 4 1 1 -10 -25 57 0
66 45 40 11 5 4 1 1 -10 -25 58 0
66 45 40 11 5 4 1 1 -10 -25 59 0
66 45 40 11 5 4 1 1 -10 -25 60 0
66 45 40 11 5 4 1 1 -10 -25 61 0
66 45 40 11 5 4 1 1 -10 -25 62 0
66 45 40 11 5 4 1 1 -10 -25 63 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:

Operation Bit Position Operation Description


# in mFLAGS
(MSB to
LSB)
1 2 (2nd MSB) Bitwise LEFT Rotation:
Perform the left bitwise rotation if the bit is ON. The rotations must be
performed X times. The number X must be extracted from the mFLAGS using
its last 4 bits (LSB). 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.)

2 3 Bitwise RIGHT Rotation:


Perform the right bitwise rotation if the bit is ON. The rotations must be
performed X times. The number X must be extracted from the mFLAGS using
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.)

3 6 Bitwise Key Mixing:

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

In key mixing operation, you must update the mDATA by performing XOR
operation with the a key value. A 32-bit key value must be generated by using
mFLAGS in such a way that mFLAGS is concatenated with mFLAGS.
i.e., if mFLAG is 1010110110101101 the key must be
10101101101011011010110110101101.

The key mixing will only be performed if the bit is ON.


4 LSB Bitwise Permutation:

If this bit is ON, perform bitwise permutation operation on a mDATA using a


predefined permutation table. The permutation table specifies the new positions
of the bits. For example:
Permutation Table:
Bit Position Permutated Position
0 16
1 07
2 09
… …
31 15

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 14 of 15
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 which is 4 bit
Multiplier db 23 ; Consider Assigned Number Digit A3
; A0 which is 4 bit
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 1 0 0 0 1 1 0 0 0 1

0 0 0 1 0 0 0 0 0 0 0 0 0 1

1 0 1 0 0 0 0 0 0 0 0 0 0 1

0 1 0 0 0 0 0 0 0 0 0 0 0 1

Page 15 of 15

You might also like