Lab Manual 05

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

Computer Organization and Assembly Language

Lab Manual (Lab 5)

Course Instructor:
Lab Instructor:

Session: Fall 2024

School of Systems and Technology


UMT Lahore Pakistan
Objectives:
 Bit Manipulation
 Using Shift, Rotate with Signed & Unsigned Data
 4-Bit Multiplication
 8-Bit Multiplication

Shift Operations:

Shift operations move bits to the left or right within a binary number. Shifting left multi-
plies the number by powers of 2, while shifting right divides it.

o SHL (Shift Left): Shifts bits to the left, filling with 0s on the right. It multiplies the
number by 2 for each shift.

mov ax, 2 ; AX = 2 (binary: 0010)


shl ax, 1 ; Shift left by 1
; Result: AX = 4 (binary: 0100)
Explanation: Each left shift doubles the value. Here, 2 becomes 4 after one shift.

o SHR (Shift Right): Shifts bits to the right, filling with 0s on the left. It divides the
number by 2 for each shift.

mov ax, 8 ; AX = 8 (binary: 1000)


shr ax, 1 ; Shift right by 1
; Result: AX = 4 (binary: 0100)
Explanation: Each right shift halves the value. Here, 8 becomes 4 after one shift.

o SAL (Shift Arithmetic Left): Identical to SHL, shifts bits to the left, filling with 0s
on the right, used for signed numbers.
o SAR (Shift Arithmetic Right): Shifts bits to the right, preserving the sign bit (most
significant bit) for signed numbers.

Example:

o SHL AX, 1: Shift the bits in register AX one position to the left.
o If AX = 0010 (binary for 2), after SHL AX, 1, AX becomes 0100 (binary for 4).

For unsigned data, the logical shift fills vacated bits with 0s.

For signed data, arithmetic shift preserves the sign bit.

Rotate Operations:

Rotate operations move bits to the left or right, with the bit that goes off the end being moved
to the opposite side. It doesn’t lose any bits in the process.
o ROL (Rotate Left): Rotates bits to the left, with the most significant bit (MSB) mov-
ing to the least significant bit (LSB) position.

mov ax, 9 ; AX = 9 (binary: 1001)


rol ax, 1 ; Rotate left by 1
; Result: AX = 3 (binary: 0011)
Explanation: Bits are cyclically rotated, with the MSB wrapping around to the LSB.

o ROR (Rotate Right): Rotates bits to the right, with the least significant bit (LSB)
moving to the most significant bit (MSB) position.

mov ax, 6 ; AX = 6 (binary: 0110)


ror ax, 1 ; Rotate right by 1
; Result: AX = 3 (binary: 0011)
Explanation: The least significant bit is moved to the most significant position.

o RCL (Rotate through Carry Left): Similar to ROL, but includes the carry flag in
the rotation, shifting the carry flag into the LSB.
o RCR (Rotate through Carry Right): Similar to ROR, but includes the carry flag in
the rotation, shifting the carry flag into the MSB.

Example:

o ROL AX, 1: Rotate bits in AX to the left by 1 position, with the most significant bit
moved to the least significant position.
o If AX = 1001 (binary for 9), after ROL AX, 1, AX becomes 0011.

In unsigned operations, rotating can be used to cyclically permute bits.

For signed data, rotating ensures no change in the sign of the number as the operation is
cyclic.

These operations are foundational for low-level bitwise manipulation in assembly programs.
4-Bit Multiplication Program

;4 bit multiplication algorithm


[ORG 0x100]

jmp start
multiplicand: db 13 ;4bit multiplicand (8bit space)
multiplier: db 5 ;4bit multiplier
result: db 0 ;8bit result

start:
mov cl, 4 ;initialize bit count to 4
mov bl, [multiplicand] ;load multiplicand in bl
mov dl, [multiplier] ;load multiplier in dl

checkbit:
shr dl, 1 ;move right most bit in carry
jnc skip ;skip the addition if bit is zero

add [result], bl ;accumulate result

skip:
shl bl, 1 ;shift multiplicand left
dec cl ;decrement bit count
jnz checkbit ;repeat if bits left

mov ax, 0x4c00 ;terminate the program


int 0x21

CL SHR DL, 1 (DL) Carry Flag SHL BL, 1 (BL) [RESULT]


- 00000101 (5) - 00001101 (13) 00000000 (0)
3 00000010 (2) 1 00011010 (26) 00001101 (13)
2 00000001 (1) 0 00110100 (52) 00001101 (13)
1 00000000 (0) 1 01101000 (104) 01000001 (65)
0 00000000 (0) 0 11010000 (208) 01000001 (65)

Practice Questions

Write an assembly program to perform multiplication of two 3-bit numbers


Write an assembly program to perform multiplication of two 8-bit numbers

You might also like