Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Namal University Mianwali

Department of Computer Science

CS-240: Computer Organization and Assembly Language


Faculty Member Semester
4th
Dr.Muhammad Sadiq Amin

Class/Section Date

Lab 9: Bit wise and Shift operations in Assembly


Language

Grading
Report Marks Viva Marks Total
Name Registration No.
(Max. 8) (Max. 7) (Max. 15)
Muhammad Shahbaz BSCS-2022-30
Namal University

Lab 9: Bit wise and Shift operations in Assembly Language

Objective

The objective of this lab is to use bitwise operator for multiplication in Assembly language

CS-240: Computer Organization and Assembly Language 2


Namal University

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.

CS-240: Computer Organization and Assembly Language 3


Namal University

The TEST instruction


The test instruction performs a bitwise AND on two operands. The flags SF , ZF , PF are modified
while the result of the AND is discarded.

• TEST destination, source


o Performs AND, does notstore the result o
Flags are set

CS-240: Computer Organization and Assembly Language 4


Namal University

CS-240: Computer Organization and Assembly Language 5


Namal University

Exercises

Exercise 1: Write assembly code to find number of ones in32-bit hex number using bit wise
operation.

INCLUDE Irvine32.inc

.data

ask BYTE "Enter a number to calculate number of 1's it has:", 0


show BYTE "The no of 1's it has :",0
counter BYTE 0

.code
main PROC

CS-240: Computer Organization and Assembly Language 6


Namal University

mov edx, OFFSET ask


call writestring
call crlf
call readhex
call crlf
call crlf
mov ecx, 32
mov ebx, eax
mov counter, 0

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:

CS-240: Computer Organization and Assembly Language 7


Namal University

a. mov al,0D4h shr


al,1
bf=11010100

af=01101010

It is shifted towards
write

b. mov al,0D4h sar


al,1
c. mov al,0D4h sar
al,4
d. mov al,0D4h rol
al,4
e. mov al,0D4h ror
al,4

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.

CS-240: Computer Organization and Assembly Language 8


Namal University

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

ASK BYTE "Enter any number:", 0


show BYTE "Result after multiplaying the no with 21 is :",0

.code
main PROC

mov edx, OFFSET ASK


call writestring

call readint
call crlf

mov ebx, eax


mov ecx, eax

shl eax, 4

CS-240: Computer Organization and Assembly Language 9


Namal University

shl ecx, 2

add eax, ebx


add eax, ecx
mov edx,OFFSET show
call writestring
call writeint

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.

CS-240: Computer Organization and Assembly Language 10

You might also like