Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.

Slides:



Advertisements
Similar presentations
Integer Arithmetic: Multiply, Divide, and Bitwise Operations
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
COMP 2003: Assembly Language and Digital Logic
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Department of Computer Science and Software Engineering
80x86 Instruction Set Dr. Qiang Lin.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
1 Lecture 7 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Shift and Rotate Instructions
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
CS2422 Assembly Language & System Programming October 24, 2006.
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
Integer Arithmetic Computer Organization & Assembly Language Programming Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa [Adapted from slides of Dr. Kip Irvine:
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Integer Arithmetic COE 205 Computer Organization and Assembly Language
Assembly Language for x86 Processors 6th Edition
Sahar Mosleh California State University San MarcosPage 1 CPU Flags and Boolean Instructions.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Sahar Mosleh California State University San MarcosPage 1 Review.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Integer Arithmetic Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
CT215: Assembly Language Programming
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Assembly Language for x86 Processors 6th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Logical and Bit Operations Chapter 9 S. Dandamudi.
Conditional Loop Instructions, Conditional Structures
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers,
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Assembly Language for x86 Processors 7th Edition
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for x86 Processors 7th Edition
Chapter 4 Data Movement Instructions
Assembly Language Lab (4).
Chapter 4: Instructions
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17
Data Transfers, Addressing, and Arithmetic
Shift & Rotate Instructions)
Multiplication and Division Instructions
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Shift, Multiply, and Divide
Chapter 5 Arithmetic and Logic Instructions
Computer Organization and Assembly Language
Shift and Rotate Instructions.
Presentation transcript:

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