sahar mosleh california state university san marcospage 1 applications of shift and rotate...
TRANSCRIPT
Sahar Mosleh California State University San Marcos Page 1
Applications of
Shift
and
Rotate Instructions
Sahar Mosleh California State University San Marcos Page 2
SHLD/SHRD instructions
• The SHLD (Shift Left Double) instructions 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.
• The source operand is not affected, but the sign, zero, auxiliary, parity, and carry flags are affected.
SHLD destination, source, count
• The SHRD (Shift Right Double) instructions shifts a destination operand a given number of bits to the right.
• The bit positions opened up by the shift are filled by the least significant bits of the source operand.
SHRD destination, source, count
Sahar Mosleh California State University San Marcos Page 3
• The following instruction formats apply to both SHLD and SHRD.
• The destination operand can be a register or memory operand and the source operand must be a register.
SHLD reg16, reg16, cl/imm8SHLD mem16, reg16, cl/imm8SHLD reg32, reg32, cl/imm8SHLD mem32, reg32, cl/imm8
• The count operand can be either the CL register or a 8-bits immediate operand
Sahar Mosleh California State University San Marcos Page 4
Example 1:
• The following statements shift wval to the left 4 bits and insert the high 4 bits of ax into the low 4-bit position of wval
.data wval word 9BA6h
.codemov ax, 0AC36hshld wval, ax, 4 ; wval = BA6Ah
• The data movement is shown in the following figure
AC36
AC36
wval AX
Sahar Mosleh California State University San Marcos Page 5
Example 2:
• In the following example, ax is shifted to the right 4-bits and the low 4-bits of dx are shifted into the high 4-bit positions of ax
mov ax 234Bhmov dx, 7654hshrd ax, dx, 4 ; AX = 4234h
7654
4234
234B
DX AX
Sahar Mosleh California State University San Marcos Page 6
• SHLD can be used to manipulate bit map images when groups of bits must be shifted left and right to reposition images on the screen.
• Another application for SHLD and SHRD is data encryption in which the encryption algorithm involves the shift of the bits
• Finally, the two instructions can be used when performing fast multiplication and division with very long integers
Sahar Mosleh California State University San Marcos Page 7
Shift and Rotate Applications
Shifting multiple double words• Programs sometimes need to shift all bits within an array, for
example, using an array of three double words, the following steps can be used to shift the array one bit to the right.
• Set ESI to offset of the array
• The high order double word at [ESI + 8] is shifted right and its lowest bit is copied into the carry flag
• The value at [ESI + 4] is shifted right, its highest bit is filled from the carry flag, and its lowest bit is copied into the new carry flag
• The low order double word at [ESI + 0] is shifted right, its highest bit is filled from the carry flag, and its lowest bit is copied into the new carry flag
Sahar Mosleh California State University San Marcos Page 8
.data arraySize = 3 array DWORD arraySize DUP (99999999h) ; 1001 1001
.codemov esi, offset arrayadd esi,8mov ebx, [esi] ; high dword shr ebx,1sub esi,4mov ebx, [esi] ; middle dword, include carry
rcr ebx,1sub esi,4mov ebx, [esi] ; low dword, include Carry
rcr ebx,1
The program output shows the number in binary before and after shift1001 1001 1001 1001 1001 1001 1001 1001 1001 1001..... ect
0100 ..... ect
h h h
[esi] [esi+4] [esi+8]
Sahar Mosleh California State University San Marcos Page 9
Binary Multiplication
• As we have already seen, SHL performs unsigned multiplication efficiently when the multiplier is a power of 2.
• You can factor any binary number into power of 2.
• For example, to multiply unsigned eax by 36, we can factor 36 into 2^5 + 2^2 and use the distributive property of multiplication to carry out the operation.
• EAX * 36 = EAX * (32 + 4) = EAX*32 + EAX *4
• The following figure shows the multiplication of 123 * 36 producing 4428.
01111011 123
00100100 36--------------------- 01111011 123 SHL 2
+ 01111011 123 SHL 5------------------------- 0001000101001100 4428
Sahar Mosleh California State University San Marcos Page 10
• Notice that bit 2 and 5 are set in the multiplier, 36. These are exactly the shift values used in the example
• The following code implements these multiplications using 32-bit registers
.codemov eax, 123mov ebc, eax shl eax, 5 ; multiply by 2^5shl ebx, 2 ; multiply by 2^2add eax, ebx ; add the products
Sahar Mosleh California State University San Marcos Page 11
Displaying Binary Bits
• A good way to apply SHL instruction is to display a byte in ASCII binary format.
• We can take advantage of the fact that the highest bit is copied into the carry flag each time the byte is shifted to the left.
• The program in the next slide displays each of the bits in EAX.
Sahar Mosleh California State University San Marcos Page 12
TITLE Displaying Binary Bits (WriteBin.asm)
INCLUDE Irvine16.inc
.databinValue DWORD 1234ABCDh ; sample binary valuebuffer BYTE 32 dup(0),0
.codemain PROC
mov eax,binValue ; number to displaymov ecx,32 ; number of bits in EAXmov esi,offset buffer
L1: shl eax,1 ; shift high bit into Carry flagmov [esi],'0' ; choose 0 as default digitjnc L2 ; if no Carry, jump to L2mov [esi],'1' ; else move 1 to buffer
L2: inc esi ; next buffer positionloop L1 ; shift another bit to left
mov edx,OFFSET buffer ; display the buffercall WriteStringcall Crlf
exitmain ENDPEND main
Sahar Mosleh California State University San Marcos Page 13
Extended Addition and Subtraction
• Extended addition and subtraction is adding and subtracting of numbers having almost unlimited size.
• Suppose you were asked to write a C++ program that adds two 128 bit integers.
• The solution would not be easy but in assembly language ADC (add with carry) and SBB (subtract with borrow) instructions are well suited to this type of problem.
Sahar Mosleh California State University San Marcos Page 14
ADC instruction
• The ADC (add with carry flag) instruction adds both a source operand and the content of the carry flag to the destination operand.
• The instruction formats are the same as mov instruction.
ADC reg, regADC mem, regADC reg, memADC mem, immADC reg, imm
• Example: the following instruction add two 8-bits integers (0FFh+0FFh), producing a 16-bit sum in DL.. AL, which is 01FEh
mov dl, 0mov al, 0FFhadd al, 0FFh ; AL = FEadc dl, 0 ; DL = 01
Sahar Mosleh California State University San Marcos Page 15
• The following instructions add two 32-bit integers (FFFFFFFFh + FFFFFFFFh) producing a 64-bit sum in
EDX:EAX: 00000001FFFFFFFEh
mov edx, 0mov eax, 0FFFFFFFFFhadd eax, 0FFFFFFFFFhadc edx, 0
Sahar Mosleh California State University San Marcos Page 16
TITLE Extended Addition Example (ExtAdd.asm)
INCLUDE Irvine32.inc
.dataop1 QWORD 0A2B2A40674981234hop2 QWORD 08010870000234502hsum DWORD 3 dup(?) ; = 0000000122C32B0674BB5736
.codemain PROC
mov esi,OFFSET op1 ; first operandmov edi,OFFSET op2 ; second operandmov ebx,OFFSET sum ; sum operandmov ecx,2 ; number of doublewordscall Extended_Addmov esi,OFFSET sum ; dump memorymov ebx,4 ; look at page 141-142 for DumpMem functionmov ecx,3call DumpMem
exitmain ENDP
Sahar Mosleh California State University San Marcos Page 17
Extended_Add PROC; Calculates the sum of two extended integers that are; stored as an array of doublewords.; Receives: ESI and EDI point to the two integers,; EBX points to a variable that will hold the sum, and; ECX indicates the number of doublewords to be added.;--------------------------------------------------------
pushadclc ; clear the Carry flag
L1: mov eax,[esi] ; get the first integeradc eax,[edi] ; add the second integerpushfd ; save the Carry flagmov [ebx],eax ; store partial sumadd esi,4 ; advance all 3 pointersadd edi,4add ebx,4popfd ; restore the Carry flagloop L1 ; repeat the loop
adc [ebx],0 ; add any leftover carrypopadret
Extended_Add ENDPEND main
Sahar Mosleh California State University San Marcos Page 18
SBB Instruction
• The SBB (Subtract with borrow) instruction subtracts both a source operand and the value of the carry flag from the destination operand.
• The possible instruction formats are the same as for ADC instruction.
• The following example code performs a 64-bit subtraction. It sets edx:eax to 0000000100000000h and subtracts 1 from this value
• The lower 32 bits are subtracted first, setting the carry flag and the upper 32-bit are subtracted including the carry flag
mov edx, 1 ; upper halfmov eax, 0 ; lower halfsub eax, 1 ; subtract 1sbb edx, 0 ; subtract upper half
• The 64-bit difference in EDX:EAX is 00000000FFFFFFFFh