Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions
Sahar Mosleh California State University San MarcosPage 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 MarcosPage 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/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
Sahar Mosleh California State University San MarcosPage 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.code mov ax, 0AC36h shld wval, ax, 4; wval = BA6Ah The data movement is shown in the following figure AC36 wvalAX
Sahar Mosleh California State University San MarcosPage 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 234Bh mov dx, 7654h shrd ax, dx, 4; AX = 4234h 7654 B DXAX
Sahar Mosleh California State University San MarcosPage 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 MarcosPage 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 MarcosPage 8.data arraySize = 3 array DWORD arraySize DUP ( h); code mov esi, offset array addesi,8 movebx, [esi]; high dword shrebx,1 subesi,4 movebx, [esi]; middle dword, include carry rcrebx,1 subesi,4 movebx, [esi]; low dword, include Carry rcrebx,1 The program output shows the number in binary before and after shift ..... ect 0100 ..... ect h [esi][esi+4][esi+8]
Sahar Mosleh California State University San MarcosPage 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 SHL SHL
Sahar Mosleh California State University San MarcosPage 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.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
Sahar Mosleh California State University San MarcosPage 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 MarcosPage 12 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
Sahar Mosleh California State University San MarcosPage 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 MarcosPage 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, 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
Sahar Mosleh California State University San MarcosPage 15 The following instructions add two 32-bit integers (FFFFFFFFh + FFFFFFFFh) producing a 64-bit sum in EDX:EAX: FFFFFFFEh mov edx, 0 mov eax, 0FFFFFFFFFh add eax, 0FFFFFFFFFh adc edx, 0
Sahar Mosleh California State University San MarcosPage 16 TITLE Extended Addition Example (ExtAdd.asm) INCLUDE Irvine32.inc.data op1 QWORD 0A2B2A h op2 QWORD h sum DWORD 3 dup(?) ; = C32B0674BB5736.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 for DumpMem function mov ecx,3 call DumpMem exit main ENDP
Sahar Mosleh California State University San MarcosPage 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. ; 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
Sahar Mosleh California State University San MarcosPage 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 h 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 FFFFFFFFh