350 likes | 1.3k Views
Applications of Shift and Rotate Instructions. 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.
E N D
Applications of Shift and Rotate Instructions
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
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/imm8 • SHLD mem16, reg16, cl/imm8 • SHLD reg32, reg32, cl/imm8 • SHLD mem32, reg32, cl/imm8 • The count operand can be either the CL register or a 8-bits immediate operand
wval AX 9BA6 AC36 BA6A AC36 • 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 • .code • mov ax, 0AC36h • shld wval, ax, 4 ; wval = BA6Ah • The data movement is shown in the following figure
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 234Bh • mov dx, 7654h • shrd ax, dx, 4 ; AX = 4234h DX AX 7654 234B 7654 4234
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
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
[esi] [esi+4] [esi+8] 99999999h 99999999h 99999999h • .data • arraySize = 3 • array DWORD arraySize DUP (99999999h) ; 1001 1001 • .code • mov 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 shift • 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001..... ect • 0100 1100 1100 1100 1100 1100 1100 1100 1100 1100 ..... ect
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
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 • .code • mov eax, 123 • mov ebc, eax • shl eax, 5 ; multiply by 2^5 • shl ebx, 2 ; multiply by 2^2 • add eax, ebx ; add the products
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.
TITLE Displaying Binary Bits (WriteBin.asm) INCLUDE Irvine16.inc .data binValue DWORD 1234ABCDh ; sample binary value buffer BYTE 32 dup(0),0 .code main PROC mov eax,binValue ; number to display mov ecx,32 ; number of bits in EAX mov esi,offset buffer L1: shl eax,1 ; shift high bit into Carry flag mov [esi],'0' ; choose 0 as default digit jnc L2 ; if no Carry, jump to L2 mov [esi],'1' ; else move 1 to buffer L2: inc esi ; next buffer position loop L1 ; shift another bit to left mov edx,OFFSET buffer ; display the buffer call WriteString call Crlf exit main ENDP END main
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.
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, reg • ADC mem, reg • ADC reg, mem • ADC mem, imm • ADC 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, 0 • mov al, 0FFh • add al, 0FFh ; AL = FE • adc dl, 0 ; DL = 01
The following instructions add two 32-bit integers (FFFFFFFFh + FFFFFFFFh) producing a 64-bit sum in • EDX:EAX: 00000001FFFFFFFEh • mov edx, 0 • mov eax, 0FFFFFFFFFh • add eax, 0FFFFFFFFFh • adc edx, 0
TITLE Extended Addition Example (ExtAdd.asm) INCLUDE Irvine32.inc .data op1 QWORD 0A2B2A40674981234h op2 QWORD 08010870000234502h sum DWORD 3 dup(?) ; = 0000000122C32B0674BB5736 .code main PROC mov esi,OFFSET op1 ; first operand mov edi,OFFSET op2 ; second operand mov ebx,OFFSET sum ; sum operand mov ecx,2 ; number of doublewords call Extended_Add mov esi,OFFSET sum ; dump memory mov ebx,4 ; look at page 141-142 for DumpMem function mov ecx,3 call DumpMem exit main ENDP
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. ;-------------------------------------------------------- pushad clc ; clear the Carry flag L1: mov eax,[esi] ; get the first integer adc eax,[edi] ; add the second integer pushfd ; save the Carry flag mov [ebx],eax ; store partial sum add esi,4 ; advance all 3 pointers add edi,4 add ebx,4 popfd ; restore the Carry flag loop L1 ; repeat the loop adc [ebx],0 ; add any leftover carry popad ret Extended_Add ENDP END main
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 half • mov eax, 0 ; lower half • sub eax, 1 ; subtract 1 • sbb edx, 0 ; subtract upper half • The 64-bit difference in EDX:EAX is 00000000FFFFFFFFh