Shift: Logical and Arithmetic
Shift: Logical and Arithmetic
Statement Purpose:
Objective of this lab is to introduce students with some new types of instructions known as shift and rotate. Student must be able to shift and rotate any type of data at bit-level along with understanding their applications.
Activity Outcomes:
This lab teaches you the following topics:
- Students will know that shifts and rotate instructions are very helpful in performing faster multiplication and divisions.
- They will also learn that how images are manipulated or distorted by shifting or rotating bits via various different ways (Shift Left, Shift Right, and Rotate Left/Right. Rotate through Carry Left/Right etc)
Instructor Note:
Shift instructions are among the most characteristic of assembly language. To shift a number means to move its bits right or left. The SHL (shift left) instruction shifts each bit in a destination operand to the left, filling the lowest bit with 0. One of the best uses of SHL is for performing high-speed multiplication by powers of 2. Shifting any operand left by n bits multiplies the operand by 2n. The SHR (shift right) instruction shifts each bit to the right, replacing the highest bit with a 0. Shifting any operand right by n bits divides the operand by 2n.
SAL (shift arithmetic left) and SAR (shift arithmetic right) are shift instructions specifically designed for shifting signed numbers.
The ROL (rotate left) instruction shifts each bit to the left and copies the highest bit to both the Carry flag and the lowest bit position. The ROR (rotate right) instruction shifts each bit to the right and copies the lowest bit to both the Carry flag and the highest bit position.
The RCL (rotate carry left) instruction shifts each bit to the left and copies the highest bit into the Carry flag, which is first copied into the lowest bit of the result. The RCR (rotate carry right) instruction shifts each bit to the right and copies the lowest bit into the Carry flag. The Carry flag is copied into the highest bit of the result.
The SHLD (shift left double) and SHRD (shift right double) instructions, available on x86 processors, are particularly effective for shifting bits in large integers.
Introduction
Shift instructions are among the most characteristic of assembly language. To shift a number means to move its bits right or left. The SHL (shift left) instruction shifts each bit in a destination operand to the left, filling the lowest bit with 0. One of the best uses of SHL is for performing high-speed multiplication by powers of 2. Shifting any operand left by n bits multiplies the
operand by 2n. The SHR (shift right) instruction shifts each bit to the right, replacing the highest bit with a 0. Shifting any operand right by n bits divides the operand by 2n.
- Students will know that shifts and rotate instructions are very helpful in performing faster multiplication and divisions.
- They will also learn that how images are manipulated or distorted by shifting or rotating bits via various different ways (Shift Left, Shift Right, and Rotate Left/Right. Rotate through Carry Left/Right etc)
Activities:
Activity 1:
Copy a number from one variable to another using shift. Solution:
TITLE Lab Shift and Rotate Instructions and Applications
; Author: Ashfaq Hussain Farooqi
Activity 2:
Reversing a number using shift. Solution:
TITLE Lab Shift and Rotate Instructions and Applications
; Author: Ashfaq Hussain Farooqi
.data
var1 DWORD 12345678h
.code
var2 DWORD ? count1 DWORD ?
main PROC MOV ECX, 8 MOV EAX, var1
l1:
mov count1, ecx mov ecx, 4
l2:
loop l2
Shr eax,1 RCL ebx,1
mov ecx, 4 l3:
shr ebx,1
rcl edx,1
loop l3
mov ecx, count1 Loop l1
call dumpregs exit
main ENDP ; End of main procedure END main
Activity 3:
Multiply 2 binary numbers using shift.
Solution:
TITLE Lab Shift and Rotate Instructions and Applications
; Author: Ashfaq Hussain Farooqi
.data
.code
Num1 DWORD 10010101b Num2 DWORD 00000100b Num3 DWORD 0d
count1 DWORD ?
msg1 BYTE “msg1”,0dh,0ah,0 msg2 BYTE “msg2”,0dh,0ah,0
main PROC
mov ebx, num2
mov ecx, 32 LL1:
SHR ebx,1 JC l1
INC Num3
loop LL1 l2:
l1:
;MOV EDX, offset msg1
;Call WriteString JMP Endd
;MOV EDX, offset msg2
;Call WriteString MOV EAX, Num3
MOV ecx, Num3 MOV EAX, Num1 LLL:
SHL eax,1 Loop LLL Call WriteInt Endd:
CALL CRLF
exit
main ENDP ; End of main procedure END main
Home Activities:
Activity # 1:
Write a program that takes input from the user and checks if the number is positive/negative and even/odd using shift commands.
Solution:
TITLE Lab Shift and Rotate Instructions and Applications
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
msg1 BYTE “Enter number: “,0 msg2 BYTE “Number is negative!”,0 msg3 BYTE “Number is positive!”,0 msg4 BYTE “Number is odd!”,0 msg5 BYTE “Number is even!”,0
main PROC
start:
MOV EDX,offset msg1 CALL writestring CALL readint
MOV EBX,EAX SHL EAX,1
JC mega
MOV EDX,offset msg3 CALL writestring CALL crlf
JMP break mega:
MOV EDX,offset msg2 CALL writestring CALL crlf
break:
exit
SHR EBX,1 MOV EAX,EBX
JC even1
MOV EDX,offset msg5 CALL writestring CALL CRLF
JMP target even1:
MOV EDX,offset msg4 CALL writestring CALL crlf
target: CALL crlf JMP Start
main ENDP END main
Activity # 2:
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.