Lab-09 and 10 PDF
Lab-09 and 10 PDF
• Shift Instructions:
Shift instructions move bits a specified number of places to the right or left. The last in
the direction of the shift goes into the carry flag, and the first bit is filled with 0 or with the
previous value of the first bit.
• SHL Instruction
This instruction performs a logical left shift on the destination operand, filling the lowest bit with
0. The highest bit is moved to the Carry flag, and the bit that was in the Carry flag is discarded.
SHL reg,imm8
SHL mem,imm8
SHL reg,CL
SHL mem,CL
Example:
mov bl,8Fh ;BL=10001111b
SHL bl,1 ;CF=1, BL=00011110b
Page | 1
mov al,10000000b ;AL=10000000b
SHL al,2 ;CF=0, AL=00000000b
• SHR Instruction
The SHR (shift right) instruction performs a logical right shift on the destination operand,
replacing the highest bit with a 0. The lowest bit is copied into the Carry flag, and the bit that was
previously in the Carry flag is lost.
Examples:
mov al,0D0h ; AL = 11010000b
shr al,1 ; AL = 01101000b, CF = 0
mov al,00000010b
shr al,2 ; AL = 00000000b, CF = 1
The following example shows how SAR duplicates the sign bit. AL is negative before and
after it is shifted to the right:
mov al,0F0h ; AL = 11110000b (-16)
sar al,1 ; AL = 11111000b (-8), CF = 0
Page | 2
Sign division:
mov dl,-128 ; DL = 10000000b
sar dl,3 ; DL = 11110000b
Sign-Extend AX into EAX:
• Rotate Instructions:
Rotate instructions also move bits a specified number of places to the right or left. For
each bit rotated the last bit in the direction of the rotate operation moves into the first bit
position at the other end of the operand. With some variations, the carry bit is used as an
additional bit of the operand. RCR (Rotate Carry Right) and RCL (Rotate Carry Left) instructions
carry values from the first register to the second by passing the leftmost or rightmost bit through
the carry flag.
• ROL Instruction
The ROL (rotate left) instruction shifts each bit to the left. The highest bit is copied into the
Carry flag and the lowest bit position. The instruction format is the same as for SHL:
Example:
Page | 3
rol al,1 ; AL = 10000000b, CF = 0
rol al,1 ; AL = 00000001b, CF = 1
rol al,1 ; AL = 00000010b, CF = 0
mov al,26h
rol al,4 ; AL = 62h
• ROR Instruction
The ROR (rotate right) instruction shifts each bit to the right and copies the lowest bit into the
Carry flag and the highest bit position.
Example:
• RCL Instructions
The RCL (rotate carry left) instruction shifts each bit to the left, copies the Carry flag to the LSB,
and copies the MSB into the Carry flag:
Example:
clc ; CF = 0
mov bl,88h ; CF,BL = 0 10001000b
rcl bl,1 ; CF,BL = 1 00010000b
• RCR Instruction:
The RCR (rotate carry right) instruction shifts each bit to the right, copies the Carry flag into the
MSB, and copies the LSB into the Carry flag
Page | 4
Example:
stc ; CF = 1
mov ah,10h ; AH, CF = 00010000 1
rcr ah,1 ; AH, CF = 10001000 0
• SHLD Instruction
The SHLD (shift left double) instruction shifts a destination operand a given number of bits to the
left. The bit positions opened up by the shift are filled by the most significant bits of the source
operand.
Format:
SHLD reg16, reg16, CL/imm8
SHLD mem16, reg16, CL/imm8
SHLD reg32, reg32, CL/imm8
SHLD mem32, reg32, CL/imm8
Example:
.data
a WORD 9BA6h
.code
mov ax, 0AC36h
shld a, ax, 4 ;a=BA6Ah
Page | 5
• SHRD Instruction
The SHRD (shift right double) instruction shifts a destination operand a given number of bits to
the right. The bit positions opened by the shift are filled by the least significant bits of the source
operand.
Example:
.code
mov ax,234Bh
mov dx,7654h
shrd ax,dx,4 ;ax=4234h
• MUL Instruction
The MUL instruction is for unsigned multiplication. Operands are treated as unsigned numbers.
The three formats accept register and memory operands, but not immediate operands. The Carry
flag is clear (CF = 0) because AH (the upper half of the product) equals zero.
Syntax:
MUL reg/mem8
MUL reg/mem16
MUL reg/mem32
EXAMPLE # 01:
mov al,5h
mov bl,10h
mul bl ; AX = 0050h, CF = 0
Page | 6
EXAMPLE # 02:
.data
val1 WORD 2000h
val2 WORD 0100h
.code
mov ax,val1 ; AX = 2000h
mul val2 ; DX:AX = 00200000h, CF = 1
EXAMPLE # 03:
mov eax,12345h
mov ebx,1000h
mul ebx ; EDX:EAX = 0000000012345000h, CF = 0
• IMUL Instruction
The IMUL instruction is for signed multiplication. Operands are treated as signed numbers and
result is positive or negative depending on the signs of the operands.
The x86 instruction set supports three formats for the IMUL instruction: one operand, two
operands, and three operands.
- One-Operand Formats:
- Two-Operand Formats
IMUL reg16, reg/mem16
IMUL reg16, imm8
IMUL reg16, imm16
- Three-Operand Formats
IMUL reg16, reg/mem16, imm8
IMUL reg16, reg/mem16, imm16
IMUL reg32, reg/mem32, imm8
IMUL reg32, reg/mem32, imm32
Example:
The following instructions multiply 48 by 4, producing +192 in AX. Although the product is correct,
AH is not a sign extension of AL, so the Overflow flag is set:
mov al,48
mov bl,4
Page | 7
imul bl ;AX = 00C0h, OF = 1
.data
word1 SWORD 4
dword1 SDWORD 4
.code
mov ax,-16 ; AX = -16
mov bx,2 ; BX = 2
imul bx,ax ; BX = -32
imul bx,2 ; BX = -64
imul bx,word1 ; BX = -256
mov eax,-16 ; EAX = -16
mov ebx,2 ; EBX = 2
imul ebx,eax ; EBX = -32
imul ebx,2 ; EBX = -64
imul ebx,dword1 ; EBX = -256
• DIV Instruction
The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit unsigned integer division.
The single register or memory operand is the divisor. The formats are
DIV reg/mem8
DIV reg/mem16
DIV reg/mem32
The following table shows the relationship between the dividend, divisor, quotient, and
remainder:
Example:
Page | 8
mov ax,0083h ; dividend
mov bl,2 ; divisor
div bl ; AL = 41h, AH = 01h
EXAMPLE:
.data
byteVal SBYTE -101 ; 9Bh
.code
mov al,byteVal ; AL = 9Bh
cbw ; AX = FF9Bh
The CWD (convert word to doubleword) instruction extends the sign bit of AX into DX:
.data
wordVal SWORD -101 ; FF9Bh
.code
mov ax,wordVal ; AX = FF9Bh
cwd ; DX:AX = FFFFFF9Bh
The CDQ (convert doubleword to quadword) instruction extends the sign bit of EAX into
EDX:
.data
dwordVal SDWORD -101 ; FFFFFF9Bh
.code
mov eax,dwordVal
cdq ; EDX:EAX = FFFFFFFFFFFFFF9Bh
• IDIV Instruction
The IDIV (signed divide) instruction performs signed integer division, using the same operands
as DIV.
Page | 9
Example: The following instructions divide -48 by 5.
.data
byteVal SBYTE -48 ; D0 hexadecimal
.code
mov al,byteVal ; lower half of dividend
cbw ; extend AL into AH
mov bl,+5 ; divisor
idiv bl ; AL = -9, AH = -3
• ADC Instructions:
The ADC (add with carry) instruction adds both a source operand and the contents of the
Carry flag to a destination operand.
ADC reg,reg
ADC mem,reg
ADC reg,mem
ADC mem,imm
ADC reg,imm
EXAMPLE # 01:
mov dl,0
mov al,0FFh
add al,0FFh ; AL = FEh
adc dl,0 ; DL/AL = 01FEh
Page | 10
• SBB Instructions:
The SBB (subtract with borrow) instruction subtracts both a source operand and
the value of the Carry flag from a destination operand.
EXAMPLE:
Page | 11
ACTIVITIES:
1. Implement the following C++ expression in assembly language, using 32-bit signed
operands:
val1 = (val2 / val3) * (val1 % val2);
var4 = (var1 * -5) / (-var2 % var3);
2. Write ASM instructions that calculate EAX * 21 using binary multiplication. Hint:
21 = 24 + 22 + 20.
3. Give an assembly language program to move -128 in ax and expand eax. Using
shift and rotate instruction.
4. The greatest common divisor (GCD) of two integers is the largest integer that will
evenly divide both integers. The GCD algorithm involves integer division in a loop,
described by the following C++ code:
int GCD(int x, int y)
{
x = abs(x); // absolute value
y = abs(y);
do
{
int n = x % y;
x = y; y = n;
} while (y > 0);
return x;
}
Implement this function in assembly language and write a test program that calls
the function several times, passing it different values. Display all results on the
screen.
5. Create a procedure Extended_Sbb procedure to subtract two 64-bit (8-byte)
integers.
6. Create a procedure Extended_Add procedure to add two 64-bit (8-byte) integers.
7. Write a program that performs simple encryption by rotating each plaintext byte
a varying number of positions in different directions. For example, in the following
array that represents the encryption key, a negative value indicates a rotation to
the left and a positive value indicates a rotation to the right. The integer in each
position indicates the magnitude of the rotation:
key BYTE -2, 4, 1, 0, -3, 5, 2, -4, -4, 6
Your program should loop through a plaintext message and align the key
to the first 10 bytes of the message. Rotate each plaintext byte by the amount
indicated by its matching key array value.
Then, align the key to the next 10 bytes of the message and repeat the process.
Page | 12
Page | 13