0% found this document useful (0 votes)
230 views

Assigned Byte 0 Assigned Byte 1 B0 B1: Computer Organization & Assembly Language ASSIGNMENT#2 (FALL 2023)

The code performs bitwise operations on values stored in memory. It initializes a memory array with 26 bytes and copies the values to another array, performing AND and OR operations on each byte value. The operations modify the sign, zero, and parity flags. The final memory contains the original byte values with bits masked or set according to the bitwise operations.

Uploaded by

Junaid Asrar
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)
230 views

Assigned Byte 0 Assigned Byte 1 B0 B1: Computer Organization & Assembly Language ASSIGNMENT#2 (FALL 2023)

The code performs bitwise operations on values stored in memory. It initializes a memory array with 26 bytes and copies the values to another array, performing AND and OR operations on each byte value. The operations modify the sign, zero, and parity flags. The final memory contains the original byte values with bits masked or set according to the bitwise operations.

Uploaded by

Junaid Asrar
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/ 37

COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


ID: _i220770_ NAME: _Junaid Asrar_ 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 0 8 9 9


Assigned Byte 0 Assigned Byte 1
Short for Assigned BYTE B0 B1
Byte in DECIMAL 08 99
Convert byte to HEX B0H, B1H 8 63
Convert byte OCTAL B0Q, B1Q 10 143
Convert byte BINARY B0B, B1B 1000 1100011
Assigned WORD
Short for Assigned WORD W
WORD in DECIMAL(W)
0899
Convert WORD to HEX (WH) 383
Convert WORD OCTAL (WQ) 1603
Convert byte BINARY (WB) 1110000011
Assigned double WORD in HEXA (DH)
78237823H

Page 1 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


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
Short for
C0 C1 C2 C3 C4 C5 C6
CHARACTER
YOURNAME
CHARACTER BY
CHARACTER
J U N A I D A
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> .386


using namespace std; .model flat, stdcall
int main() { int a = .stack 4096
4578; int n = 5; .data .code
for (int i = 1; i <= n; ++i) {
main Proc
int rightmostBit = a & 1;
mov ax,
a >>= 1;
4578 mov
a |= (rightmostBit << 7);
ecx, 5 l1:
}
int b = a;
mov dx, ax ; d = a and
return 0; } ax, 1 ; a = a & 1 mov bx,
ax ; b = a mov ax, dx ; a =
d mov dx, bx ; d = b = a
& 1 shr ax, 1 ; a = a >> 1
shl dx, 7 ; d = d << 7
or ax, dx ; a = a |
d loop l1 mov bx, ax main
ENDP END main

Page 2 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


Explanation:
The value of AX is initially 4578, while ECX is set to 5. After performing a bitwise AND operation with 1,
the original value of AX remains unchanged. However, when performing a right shift operation (>>1) on
AX, the original value is modified, then shifted to the right by 1 position.
The result of the bitwise AND operation (AX&1) is temporarily stored in register DX. Since the value in
DX is not crucial, it is left-shifted 7 times without regard for its initial content. Subsequently, the results
of the right shift operation (AX>>1) and the left shift operation ((AX&1)<<7) are combined using a
bitwise OR operation and stored back in register AX.
Upon the completion of the loop, the final value in AX is moved into register BX.

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
Flags
Overflow 0

Parity 1

Auxiliary 0
3. Update memory after executing code given below

CODE

Page 3 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


.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 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

4. Update Flags after executing following code


Sign 1
NOTE: OR performs a Boolean(bitwise) OR operation between each pair of
Zero 0
matching bits in two operands and place result in the destination. OR
instruction always clear overflow and carry flags. It modifies Sign, Zero and Carry 0
Parity flags. Flags
Overflow 0
5. Update
memory after Parity 1
executing code
Auxiliary 0
given below

Page 4 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

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
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

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

Page 5 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

AX a5 93
AX 5a 6c
AX 00 00
AX ff ff
AX 00 00

AX 5a 37
Al 3 7

AH 5 b
AL 6 c
Sign 0

Zero 0
Carry 0
Flags
Overflow 0
Parity 1

Auxiliary 0
7. Write a program that finds parity of number given below? HINT: Use XOR and LOOP to find parity

Answer:
.386
.model flat, stdcall
.stack 4096

.data parity dq 0a1b2c3d4e5f67890h ;


pe=1

.code
main PROC

Page 6 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


mov esi, offset
parity mov ecx, type
parity xor eax, eax
mov al, [esi] inc esi
dec ecx

l1:
mov ah,
[esi] xor al,
ah inc esi
loop l1

and al, 11111111b


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

TAKEN NOT TAKEN


Jc ✓
Jz ✓
Jo ✓

Js ✓

jp ✓

Sign 1 Calculation
Flags Zero 0 1111 1111 1111 1110
+ 1111 1100 0111 0000
Carry 1 ———————————————
1 | 1111 1100 0110 1110 PE=0, ZF=0
Overflow 0

Page 7 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


Parity 1 ^ CV=1 ^PL=1 ^ AC=0

OV = CVxorPL = 0 also, adding 2 negative numbers and getting a negative answer, so OV=
also, answer = -914 > -32,768, so OV=0

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

TAKEN NOT TAKEN


Jc ✓

Jz ✓

Jo ✓

Js ✓

jp ✓
Sign 1 Calculation
Zero 0 0111 1011 0001 1010
+ 0011 0001 0001 0101
Carry 1 ———————————————
0 | 1010 1100 0010 1111 PE=0, ZF=0
Overflow 1
Flags ^ CV=0 ^PL=1 ^ AC=0
Parity 0
OV = CVxorPL = 1
also, subtracting a negative number from a positive number (adding 2 positive numbe
answer, so OV=1 since 7b1a < ceeb, CV=1

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

TAKEN NOT TAKEN


Jnc ✓

Jnz ✓

Jo ✓
Js ✓

jnp ✓

Page 8 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


Sign 1 Calculation
Zero 0 1111 1011 1100 1101
+ 1110 0100 1010 1010
Carry 1 ———————————————
1 | 1110 0000 0111 0111 PE=1, ZF=0
0
Flags Overflow ^ CV=1 ^PL=1 ^ AC=1
Parity 1
OV = CVxorPL = 0 also, adding 2 negative numbers and getting a negative answer, so O
Auxiliary 1 also answer = -8073 > -32,768, so OV=0

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

TAKEN NOT TAKEN


Jc ✓

Jz ✓

Jno ✓

Jns ✓

jp ✓

Sign 0 Calculation
Zero 0 1111 1010 1011 1101
+ 0110 1000 0100 1010
Carr 1
———————————————
y 1 | 0110 0011 0000 0111 PE=0, ZF=0
Fla Overf 0 ^ CV=1 ^PL=0 ^ AC=1
gs low
Parit 0 answer = 25,351 < 32,767, so OV=0

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

Page 9 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

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

Sign 0 Calculation
Zero 1 The final calculation is ECX being decremented to 0

Carry 0
Fla Overfl 0
gs ow
Parity 1

Auxili 0
ary
13. Fill flag after every CMP instruction

1 Calculation
Sign
N
TAOT 0 0111 1111
ro
Ze + 1000 0000
KEN ——————————

arry 1 0 | 1111 1111 PE=1, ZF=0


TAKEN
^CV=0 ^PL=1 ^AC=0
Ja C
verflow 1 OV=CVxorPL=1 also,

Jg O answer=255 > 128, so OV=1

Flags Parity 1
since smaller number (127) bigger
Auxiliary 0 number (128 {2’s complement of -
128=128}), so
CV=1

Page 10 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

NOT
TAK EN TA KENSig n 1 C
Jnl Ze ro 0 1111 1

+ 0000 0
Jnle ✓ Ca rry 0
———————
JL Ov erflow 0 0 | 1111 1

^ CV=0 ^PL=1
Flags Parity 1

Auxiliary 0 PE=1, ZF=0

since negative
(0 is just 0) = ne

since bigger nu
1=65,535) -

Page 11 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


smaller number (2’s complement
of 0=0), so CV=0

Page 12 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


NOT
Sign 0 Calculation
TAKEN TAKEN
JNG Zero 0 0000 0000 0010 0000

1 + 0000 0000 0010 0011
JNGE Carry
✓ ———————————————
JGE ✓ Overflow 0
0 | 0000 0000 0100 0011
Parity 0 ^ CV=0 ^PL=0 ^ AC=0
Auxiliary 0
Flags PE=0, ZF=0

OV=CVxorPL=0
also, answer=67 < 32767, so OV=0

since smaller number (32) - bigger


number (2’s complement of -35 =
65501), so CV=1

NOT
0 Calculation
TAKEN T AKENSign
Zero 1 0000 0000 0000 0000
JG ✓ + 0000 0000 0000 0000
JNL Carr y 0 ——————————————
✓ —
JLE Ove rflow 0 0 | 0000 0000 0000 0000
✓ ^ CV=0 ^PL=0 ^ AC=0
Parity 1
Flags Auxiliary 0 PE=1, ZF=1

OV=CVxorPL=0
NOT also, 0 < 32,767 and 0>-32768,
TAKEN T AKEN so
OV=0
JL ✓
JNG ✓
JGE Sign 0 Calculation

Flags Zero 1 same as above

Carry 0

Page 13 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


Overflow 0

Page 14 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


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,0,045h,8,040h,9,-025h,9,-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:
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
COUNT SWAP

Page 15 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

(after last
switch, swap
resets to 0 if

Page 16 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

resets to 0 if
it was 1
before)

(after last
switch, swap
resets to 0 if

Page 17 of 37
37 0
38 LANGUAGE
COMPUTER ORGANIZATION & ASSEMBLY 0

ASSIGNMENT#2 (FALL 2023)


66 45 40 9 9 11 8 0 -10 -25
42 1
40 0

66 45 40 9 9 11 8 0 -10 -25 41 1
39 0
it was 1
before)
66 45 40 9 9 11 8 0 -10 -25 (carrying

43 1
from above)

66 45 40 9 9 11 8 0 -10 -25
44 1
66 45 40 9 11 9 8 0 -10 -25 (carrying

45 0
from above)
(carrying
66 45 40 9 11 9 8 0 -10 -25 from above)

(after last
switch, swap
resets to 0 if
66 45 40 9 11 9 8 0 -10 -25 it was 1
before)

66 45 40 9 11 9 8 0 -10 -25

66 45 40 9 11 9 8 0 -10 -25

66 45 40 9 11 9 8 0 -10 -25 46 0

66 45 40 9 11 9 8 0 -10 -25 47 0

66 45 40 9 11 9 8 0 -10 -25 48 0

66 45 40 11 9 9 8 0 -10 -25 49 1

66 45 40 11 9 9 8 0 -10 -25 50 1

66 45 40 11 9 9 8 0 -10 -25 51 1

66 45 40 11 9 9 8 0 -10 -25 52 1

66 45 40 11 9 9 8 0 -10 -25 53 1

66 45 40 11 9 9 8 0 -10 -25 54 0

66 45 40 11 9 9 8 0 -10 -25 55 0

66 45 40 11 9 9 8 0 -10 -25 56 0

66 45 40 11 9 9 8 0 -10 -25 57 0

66 45 40 11 9 9 8 0 -10 -25 58 0

Page 18 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


66 45 40 11 9 9 8 0 -10 -25 59 0

66 45 40 11 9 9 8 0 -10 -25 60 0

66 45 40 11 9 9 8 0 -10 -25 61 0

66 45 40 11 9 9 8 0 -10 -25 62 0

66 45 40 11 9 9 8 0 -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:

Operatio Bit Position Operation Description


n# 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:


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.

Page 19 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


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:
.386
.model flat, stdcall
.stack 4096

.data
mData dd 1378cef6h
separate1 db 0
mFLAGS dw 20899
separate2 db 0 permPosition db 14, 15, 29, 3, 20, 4, 0, 6, 23, 5, 25, 7, 10, 11, 8, 28, 1, 24, 21, 9, 30, 16, 2, 18,
22, 17, 31, 26, 13, 12, 27, 19

Page 20 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


.code
main Proc

mov ax, mFLAGS


shl ax, 2

jnc step2

mov ax, mFLAGS


and ax, 0000000000001111b
mov cl, al

mov ebx, 0 mov


edx, mDATA
shld edx, ebx, cl
or edx, ebx mov
mDATA, edx

step2:
mov ax, mFLAGS
shl ax, 3

jnc step3

and ax, 0000000000011111b


mov cl, al

mov ebx,
mDATA mov
edx, 0 shrd ebx,
edx, cl or ebx,
edx mov
mDATA, ebx

step3:

mov ax, mFLAGS


shl ax, 6

jnc step4

Page 21 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


movzx ebx, mFLAGS
shl ebx, 16 or bx, ax
xor mDATA, ebx

step4:

mov ax, mFLAGS


shr ax, 1 jnc

endCode

mov ecx, 32
mov esi, 3
xor eax, eax
xor ebx, ebx
xor edx, edx

l1:
push eax
test ax, 0FFh jz
doNotDecrementESI
mov bl, 8 div bl
test ah, ah jnz

doNotDecrementESI

dec esi

doNotDecrementESI:
pop eax

mov bl, byte ptr [mData + esi] ; byte0,1,2...


push eax movzx eax, byte ptr [permPosition + eax] ; index from
permArray push ebx
mov bl, 8 div bl ; index/8 = byte (al).
index%8 = bit (ah) movzx edx, al neg edx
add edx, 3
pop ebx mov edi, edx mov bh,
byte ptr [mDATA + edx] mov
dh, 10000000b push ecx
mov cl, ah shr dh, cl ; shift 1 in
10000000b to index pop ecx

Page 22 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


pop eax mov dl,
10000000b push
ecx
mov cl, al ror dl, cl ; shift 1 in
10000000b to bit
pop ecx

test bl, dl

jp replacing0
jnp replacing1

replacing0: test bh, dh


jp replacing0with0
jnp replacing0with1

replacing1: test bh, dh


jp replacing1with0
jnp replacing1with1
replacing0with1:
replacing1with0: xor byte ptr
[mData + edi], dh

replacing0with0:
replacing1with1:

inc eax
loop l1

endCode:

main ENDP
END main

Page 23 of 37
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 0 ; Consider Assigned Number Digit
; A0
Multiplier db 9 ; 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 01 0 0 0 0
(carr ying from above)

0 0 0 0 0 0
0 0 0 00 0 1 0
(carr ying from above)

0 0 0 0 0 0
0 0 0 00 0 0 1

0 1 0 0 0 0
0 0 0 00 0 0 0
17. Consider the following code and fill given registers and memory accordingly after each step?
Note: Considering it as a 16 bit architecture

CODE
NOTE
; ;ADC is add through carry

ADC ax, bx ;AX+BX+CF(carry flag)

Page 24 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


.data
multiplicand dd 383h ;Hexa of assigned word
multiplier dw 005AAH result dd 0

.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

mov ax, word ptr[multiplicand] ;mov LSW of


multiplicand to ax add word ptr[result],ax ;add LSW of
multiplicand to
result mov bx, word ptr[multiplicand+2] ;mov MSW of multiplicand
to bx adc word ptr[result+2],bx ;add MSW of multiplicand
to result noadd: shl word ptr[multiplicand],1 ;shift LSW
multiplicand to left
rcl word ptr[multiplicand+2],1 ;rotate MSW of
multiplicand to left
Loop checkbit

ECX DX CF AX [result] CF BX [result+2] CF [mulp] CF [mulp+2]

10 02d5 0 0 0 0 - 0 0 0706 0 0

f 016a 1 0706 0706 0 0 0 0 0e0c 0 0

e 00b5 0 0706 0706 0 0 0 0 1c18 0 0

d 005a 1 1c18 231e 0 0 0 0 3830 0 0

c 002d 0 1c18 231e 0 0 0 0 7060 0 0

b 0016 1 7060 937e 0 0 0 0 e0c0 0 0

a 000b 0 7060 937e 0 0 0 0 c180 1 0001

9 0005 1 c180 54fe 1 0001 0002 0 8300 1 0003

8 0002 1 8300 d7fe 0 0003 0005 0 0600 1 0007

7 0001 0 8300 d7fe 0 0003 0005 0 0c00 0 000e

6 0 1 0c00 e3fe 0 000e 0013 0 1800 0 001c

Page 25 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


5 0 0 0c00 e3fe 0 000e 0013 0 3000 0 0038

4 0 0 0c00 e3fe 0 000e 0013 0 6000 0 0070

3 0 0 0c00 e3fe 0 000e 0013 0 c000 0 00e0


2 0 0 0c00 e3fe 0 000e 0013 0 8000 1 01c1

1 0 0 0c00 e3fe 0 000e 0013 0 0 1 0383

multiplicand = 03830000
multiplier = 05aa result
= 0013e3fe

18. Modify and Rewrite the code given above for following data declaration?
.data
multiplicand DQ 78237823h ;value of Assigned doubleword in hexa
multiplier DW 383h ;Assigned word in hexa
result DQ 0 ;result of the multiplication
Answer:

Page 26 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


.code
main PROC mov eax,0
mov ecx,16 mov
dx, multiplier

checkbit:
shr dx,1
jnc noadd

mov eax, dword ptr[multiplicand]


add dword ptr[result], eax mov
ebx, dword ptr[multiplicand+4] adc
dword ptr[result+4],ebx noadd:
shl dword ptr[multiplicand],1
rcl dword ptr[multiplicand+4],1
Loop
checkbit main ENDP
END main

19. Perform unsigned binary multiplication using following given flow chart?
NOTE: Your computer width is 8-bit, Multiplicand is (1000)2 of the assigned number and Multiplier is (1100011)2 of
the assigned number

C A (ACCUMULATOR) Q (MULTIPLIER) M (MULTIPLICAND)

Page 27 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 Add

0 0 0 0 1 0 00

M (MULTIPLICAND)
0 0 0 0 1 0 00

0 0 0 0 1 0 00

M (MULTIPLICAND)
0 0 0 0 1 0 00

0 0 0 0 1 0 00

M (MULTIPLICAND)
0 0 0 0 1 0 00

0 0 0 0 1 0 00

M (MULTIPLICAND)
0 0 0 0 1 0 00

0 0 0 0 1 0 00

M (MULTIPLICAND)
0 0 0 0 1 0 00

0 0 0 0 1 0 00

M (MULTIPLICAND)
0 0 0 0 1 0 00

0 0 0 0 1 0 00

M (MULTIPLICAND)
0 0 0 0 1 0 00

Page 28 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

0 0 0 0 1 0 00

Page 29 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


0 0 1 1 0 0 0 1

Q (MULTIPLIER)
0 0 1 1 0 0 0 1

0 0 0 1 1 0 0 0

Q (MULTIPLIER)

0 0 0 0 1 1 0 0

Q (MULTIPLIER)

1 0 0 0 0 1 1 0

Q (MULTIPLIER)

1 1 0 0 0 0 1 1

Q (MULTIPLIER)
1 1 0 0 0 0 1 1

0 1 1 0 0 0 0 1

Q (MULTIPLIER)
0 1 1 0 0 0 0 1

0 0 1 1 0 0 0 0

Page 30 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

Q (MULTIPLIER)

0 0 0 1 1 0 0 0

Page 31 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


0 0 0 0 0 1 0 0

A (ACCUMULATOR)
0 0 0 0 1 1 0 0

0 0 0 0 0 1 1 0

A (ACCUMULATOR)

0 0 0 0 0 0 1 1

A (ACCUMULATOR)

0 0 0 0 0 0 0 1

A (ACCUMULATOR)

0 0 0 0 0 0 0 0

A (ACCUMULATOR)
0 0 0 0 1 0 0 0

0 0 0 0 0 1 0 0

A (ACCUMULATOR)
0 0 0 0 1 1 0 0

0 0 0 0 0 1 1 0

Page 32 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


0Shift
A (ACCUMULATOR)
C
0Add

0Shift

0 0 0 0 0 0 1 1
C
Add

0Shift

C
Add

Shift

C
Add

0Shift

C
0Add

0Shift

C
0Add

0Shift

0 C
Add

Shift

C A (ACCUMULATOR) Q (MULTIPLIER) M (MULTIPLICAND)

20. Perform Multiplication using Booth’s algorithm?

Page 33 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


NOTE: Your computer width is 10-bit, Multiplicand is 63H assigned number, Multiplier is 0A5H

AX Q Q-1 M Count

11 1001 1101 00 1010 0101 0 00 0110 0011 Subtract 10

11 1100 1110 10 0101 0010 1 00 0110 0011 Shift

00 0011 0001 ^ ^ 00 0110 0011 Add 9

00 0001 1000 11 0010 1001 0 00 0110 0011 Shift

11 1011 0101 ^ ^ 00 0110 0011 Subtract 8

11 1101 1010 11 1001 0100 1 00 0110 0011 Shift

00 0011 1101 ^ ^ 00 0110 0011 Add 7

00 0001 1110 11 1100 1010 0 00 0110 0011 Shift

Page 34 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


00 0110 0011 - 6

00 0000 1111 01 1110 0101 0 00 0110 0011 Shift

11 1010 1100 ^ ^ 00 0110 0011 Subtract 5

11 1101 0110 00 1111 0010 1 00 0110 0011 Shift

00 0011 1001 ^ ^ 00 0110 0011 Add 4

00 0001 1100 10 0111 1001 0 00 0110 0011 Shift

11 1011 1001 ^ ^ 00 0110 0011 Subtract 3

11 1101 1100 11 0011 1100 1 00 0110 0011 Shift

00 0011 1111 ^ ^ 00 0110 0011 Add 2

00 0001 1111 11 1001 1110 0 00 0110 0011 Shift

00 0110 0011 - 1

00 0000 1111 11 1100 1111 0 00 0110 0011 Shift

Page 35 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)


21. Perform Unsigned binary division?
NOTE: Your computer width is 8-bit, where dividend 63H of assigned number, Divisor is 003H

Answer:

AX Q M Count

0000 0000 0110 0011 0000 0011

0000 0000 1100 0110 ^ 8 SHL

1111 1101 ^ ^ A-M

0000 0000 1100 0110 ^ Qo=0, A+M

0000 0001 1000 1100 ^ 7 SHL

1111 1110 ^ ^ A-M

0000 0001 1000 1100 ^ Qo=0, A+M

Page 36 of 37
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE

ASSIGNMENT#2 (FALL 2023)

0000 0011 0001 1000 ^ 6 SHL

0000 0000 ^ ^ A-M

^ 0001 1001 ^ Qo=1

0000 0000 0011 0010 ^ 5 SHL

1111 1101 ^ ^ A-M

0000 0000 0011 0010 ^ Qo=0, A+M

0000 0000 0110 0100 ^ 4 SHL

1111 1101 ^ ^ A-M

0000 0000 0110 0100 ^ Qo=0, A+M

0000 0001 1100 1000 ^ 3 SHL

1111 1110 ^ ^ A-M

0000 0001 1100 1000 ^ Qo=0, A+M

0000 0011 1001 0000 ^ 2 SHL

0000 0000 ^ ^ A-M

^ 1001 0000 ^ Qo=0, A+M

0000 0000 0010 0001 ^ 1 SHL

1111 1101 ^ ^ A-M

0000 0000 0010 0001 ^ Qo=0, A+M

Page 37 of 37

You might also like