Professional Documents
Culture Documents
La9 Coal
La9 Coal
Class/Section Date
Grading
Report Marks Viva Marks Total
Name Registration No.
(Max. 8) (Max. 7) (Max. 15)
Muhammad Shahbaz BSCS-2022-30
Namal University
Objective
The objective of this lab is to use bitwise operator for multiplication in Assembly language
Mask: A mask is a bit pattern that is defined by a programmer, which allows specific bits in a
piece of data to be set, cleared, tested or altered.
Setting Bits to 1:
To turn on a specific bit, we can use the OR bitwise operation and a suitable mask.
For example, if we need to turn on Bit 4 and Bit 7 of a byte (remember that the bit on the right
hand side is Bit 0), you can use the mask 1001 0000 and the OR bitwise operation.
Resetting/Clear bits to 0
We can't force a bit to be 0 using the OR command. We can use the bitwise command AND with a
suitable mask,
For example, we want to reset Bits 0, 1 and 2 in a byte but leave all the other bits as they were.
You would use the mask 1111 1000 along with the AND bitwise operator.
Exercises
Exercise 1: Write assembly code to find number of ones in32-bit hex number using bit wise
operation.
INCLUDE Irvine32.inc
.data
.code
main PROC
LOOP1:
shr ebx, 1
jnc skip
inc counter
skip:
loop LOOP1
mov edx, offset show
call writestring
movzx eax, counter
call writeint
exit
main ENDP
END main
Exercise 2: In the following code sequence, show the value of AL after each shift or rotate
instruction has executed and explain:
af=01101010
It is shifted towards
write
ANS (A):
The value of al before right shifting:11010100
The value of al after right shifting:01101010
Explanation: The lsb is copied into CF and 0 is append at place of msb in case of right shift.
ANS(B):
The value of al before right shifting:11010100
The value of al after right arithmatic shifting: 11101010
Explanation: The lsb is copied into CF and 0 msb retains its position in case of sar, because here
counter is 4 that is why answer is 11101010 .
ANS(C):
The value of al before right shifting:11010100
The value of al after right arithmatic shifting: 11111101
Explanation: The lsb is copied into CF and msb retains its position in case of sar as here counter
is 4 that Is why answer is so.
ANS(D):
The value of al before right shifting:11010100
The value of al after rotate left : 01001101
Explanation: The msb is copied into CF and it is also copied at lsb in case of Rol, as here counter
is 4 that’s why answer is 01001101.
ANS(E):
The value of al before right shifting:11010100
The value of al after rotate left : 01001101
Explanation: The msb is copied into CF and it is also copied at MSB in case of Ror, as here
counter is 4 that’s why answer is 01001101.
Exercise 3: Write ASM instructions that calculate EAX * 21 using binary multiplication (shift
and add operations). Hint: 21 = 10101=24 + 22 + 20.
13*21=13*(24 + 22 + 20)=13*24 + 13*22 + 13*20
INCLUDE Irvine32.inc
.data
.code
main PROC
call readint
call crlf
shl eax, 4
shl ecx, 2
exit
main ENDP
END main
Conclusion:
In this we perform different shifting ,loke shl,shr,sar,sal and some rotations like rol,and
ror. We also perform a task which calculates the no 1’s in a integer. At the end we perform
multiplication by using shl command.