Chapter 7 Integer Arithmetic

Similar documents
Integer Arithmetic. Shift and rotate. Overview. Shift and Rotate Instructions

Integer Arithmetic. Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17. with slides by Kip Irvine

Integer Arithmetic. Pu-Jen Cheng. Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed.

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

Shift and Rotate Instructions

Integer Arithmetic Part2

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Instructions moving data

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

Defining and Using Simple Data Types

Basic Pentium Instructions. October 18

Computer Architecture and Assembly Language. Practical Session 3

Intel x86 Instruction Set Architecture

Week /8086 Microprocessor Programming I

Computer Department Chapter 7. Created By: Eng. Ahmed M. Ayash Modified and Presented by: Eng. Eihab S. El-Radie. Chapter 7

Computer Architecture and System Programming Laboratory. TA Session 3

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Lecture 9. INC and DEC. INC/DEC Examples ADD. ADD Examples SUB. INC adds one to a single operand DEC decrements one from a single operand

8086 Programming. Multiplication Instructions. Multiplication can be performed on signed and unsigned numbers.

We will first study the basic instructions for doing multiplications and divisions

3.1 DATA MOVEMENT INSTRUCTIONS 45

Introduction to 8086 Assembly

Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice

Ex : Write an ALP to evaluate x(y + z) where x = 10H, y = 20H and z = 30H and store the result in a memory location 54000H.

COMPUTER ENGINEERING DEPARTMENT

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

Step 1: Add the least significant digits together: produces with carry 1.

8088/8086 Programming Integer Instructions and Computations

Logic Instructions. Basic Logic Instructions (AND, OR, XOR, TEST, NOT, NEG) Shift and Rotate instructions (SHL, SAL, SHR, SAR) Segment 4A

Week /8086 Microprocessor Programming

Signed number Arithmetic. Negative number is represented as

CMSC 313 Lecture 07. Short vs Near Jumps Logical (bit manipulation) Instructions AND, OR, NOT, SHL, SHR, SAL, SAR, ROL, ROR, RCL, RCR

COMPUTER ENGINEERING DEPARTMENT

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

Module 3 Instruction Set Architecture (ISA)

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Lab Session 08. To understand the use of Shift and Rotate instructions. To be able to differentiate between Arithmetic shift and Logical shift.

Arithmetic Instructions

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

16.317: Microprocessor Systems Design I Spring 2014

Arithmetic and Logic Instructions And Programs

Kingdom of Saudi Arabia Ministry of Higher Education. Taif University. Faculty of Computers & Information Systems

Q1: Multiple choice / 20 Q2: Data transfers and memory addressing

Basic Assembly SYSC-3006

EC 333 Microprocessor and Interfacing Techniques (3+1)

LAB 5 Arithmetic Operations Simple Calculator

Logical and Bit Operations. Chapter 8 S. Dandamudi

Assembly Language Programming

EECE.3170: Microprocessor Systems Design I Spring 2016

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

16.317: Microprocessor Systems Design I Fall 2013

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

APPENDIX C INSTRUCTION SET DESCRIPTIONS

ELEC 242 Time Delay Procedure

Lecture Notes on Assembly Language - J. Vaughan

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

Intel 8086: Instruction Set

UNIT II. 2 Marks. 2. List out the various register of IA- 32 Register structure. 3. Write about General purpose registers

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

Lecture (08) x86 programming 7

CS-202 Microprocessor and Assembly Language

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Chapter 5. Real-Mode 80386DX Microprocessor Programming 1 Part 2. The 80386, 80486, and Prentium Processors,Triebel Prof. Yan Luo, UMass Lowell 1

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

CMSC 313 Lecture 05 [draft]

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Microprocessor & Computer Architecture / Lecture

The x86 Architecture

Data Transfers, Addressing, and Arithmetic. Part 2

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

Lecture 2 Assembly Language

Week /8086 Microprocessor Programming II

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Bit Operations. Ned Nedialkov. McMaster University Canada. SE 3F03 February 2014

EEM336 Microprocessors I. Arithmetic and Logic Instructions

CS241 Computer Organization Spring 2015 IA

ADDRESSING MODES. Operands specify the data to be used by an instruction

UNIT III MICROPROCESSORS AND MICROCONTROLLERS MATERIAL OVERVIEW: Addressing Modes of Assembler Directives. Procedures and Macros

Logical and bit operations

Conditional Processing

8086 INSTRUCTION SET

6.1 The 80x86 Flags Register Flags. 6.1 Which flags are the condition codes?

Basic Assembly Instructions

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

UMBC. 1 (Feb. 9, 2002) seg_base + base + index. Systems Design & Programming 80x86 Assembly II CMPE 310. Base-Plus-Index addressing:

UNIT II 16 BIT MICROPROCESSOR INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING. The Intel 8086 Instruction Set

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

Lab 3: Defining Data and Symbolic Constants

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

SOEN228, Winter Revision 1.2 Date: October 25,

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

Assembly Language Lab # 9

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

Introduction to 8086 Assembly

/ 30 Q3: Arithmetic instructions / 30 Q4: Logical instructions / 20 TOTAL SCORE / 100 Q5: EXTRA CREDIT / 10

8086 ASSEMBLY LANGUAGE PROGRAMMING

Transcription:

Chapter 7 Integer Arithmetic 7.1 Introduction 193 7.2 Shift and Rotate Instructions 194 7.2.1 Logical Shifts and Arithmetic Shifts 194 7.2.2 SHL Instruction 195 7.2.3 SHR Instruction 196 7.2.4 SAL and SAR Instructions 196 7.2.5 ROL Instruction 197 7.2.6 ROR Instruction 198 7.2.7 RCL and RCR Instructions 198 7.2.8 Signed Overflow 199 7.2.9 SHLD/SHRD Instructions 199 7.2.10 Section Review 200 7.3 Shift and Rotate Applications 201 7.3.1 Shifting Multiple Doublewords 201 7.3.2 Binary Multiplication 202 7.3.3 Displaying Binary Bits 202 7.3.4 Isolating MS-DOS File Date Fields 203 7.3.5 Section Review 203 7.4 Multiplication and Division Operations 204 7.4.1 MUL Instruction 204 7.4.2 IMUL Instruction 205 7.4.3 Benchmarking Multiplication Operations 207 7.4.4 DIV Instruction 208 7.4.5 Signed Integer Division 209 7.4.6 Implementing Arithmetic Expressions 211 7.4.7 Section Review 212 7.5 Extended Addition and Subtraction 213 7.5.1 ADC Instruction 213 7.5.2 Extended Addition Example 213 7.5.3 SBB Instruction 214 7.5.4 Section Review 215 Page 1 / 14

Chapter 7 Integer Arithmetic 7.1 Introduction 193 Integer Arithmetic o Shift and Rotate Instructions o Multiplication and Division Operations o Extended Addition and Subtraction 7.2 Shift and Rotate Instructions 194 7.2.1 Logical Shifts and Arithmetic Shifts 194 A logical shift fills the newly created bit position with zero: 0 An arithmetic shift fills the newly created bit position with a copy of the number s sign bit: Page 2 / 14

7.2.2 SHL Instruction 195 The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with 0 0 Operand types for SHL: SHL reg,imm8 SHL mem,imm8 SHL reg,cl SHL mem,cl Fast Multiplication o Shifting left 1 bit multiplies a number by 2 mov dl,5 shl dl,1 Before: After: 0 0 0 0 0 1 0 1 = 5 0 0 0 0 1 0 1 0 = 10 o Shifting left n bits multiplies the operand by 2 n, for example, 5 * 2 2 = 20 mov dl,5 shl dl,2 ; DL = 20 7.2.3 SHR Instruction 196 The SHR (shift right) instruction performs a logical right shift on the destination operand. The highest bit position is filled with a zero. 0 Shifting right n bits divides the operand by 2 n mov dl,80 shr dl,1 ; DL = 40 shr dl,2 ; DL = 10 Page 3 / 14

7.2.4 SAL and SAR Instructions 196 SAL (shift arithmetic left) is identical to SHL. SAR (shift arithmetic right) performs a right arithmetic shift on the destination operand An arithmetic shift preserves the number's sign mov dl,-80 sar dl,1 ; DL = -40 sar dl,2 ; DL = -10 7.2.5 ROL Instruction 197 ROL (rotate left) shifts each bit to the left The highest bit is copied into both the Carry flag and into the lowest bit No bits are lost mov al,11110000b rol al,1 ; AL = 11100001b mov dl,3fh rol dl,4 ; DL = F3h 7.2.6 ROR Instruction 198 ROR (rotate right) shifts each bit to the right The lowest bit is copied into both the Carry flag and into the highest bit No bits are lost mov al,11110000b ror al,1 ; AL = 01111000b mov dl,3fh ror dl,4 ; DL = F3h Page 4 / 14

7.2.7 RCL and RCR Instructions 198 RCL Instruction o RCL (rotate carry left) shifts each bit to the left o Copies the Carry flag to the least significant bit o Copies the most significant bit to the Carry flag clc ; = 0 mov bl,88h rcl bl,1 ;,BL = 0 10001000b ;,BL = 1 00010000b rcl bl,1 ;,BL = 0 00100001b RCR Instruction o RCR (rotate carry right) shifts each bit to the right o Copies the Carry flag to the most significant bit o Copies the least significant bit to the Carry flag stc ; = 1 mov ah,10h ;,AH = 1 00010000b rcr ah,1 ;,AH = 0 10001000b Page 5 / 14

7.2.9 SHLD/SHRD Instructions 199 SHLD (shift left double) Instruction o Shifts a destination operand a given number of bits to the left o The bit positions opened up by the shift are filled by the most significant bits of the source operand o The source operand is not affected o Syntax: SHLD destination, source, count o Operand types: SHLD reg16/32, reg16/32, imm8/cl SHLD mem16/32, reg16/32, imm8/cl o Example: Shift wval 4 bits to the left and replace its lowest 4 bits with the high 4 bits of AX:.data wval WORD 9BA6h.code mov ax,0ac36h shld wval,ax,4 Before: After: wval 9BA6 BA6A AX AC36 AC36 SHRD (shift right double) Instruction o Shifts a destination operand a given number of bits to the right o The bit positions opened up by the shift are filled by the least significant bits of the source operand o The source operand is not affected o Syntax: SHRD destination, source, count o Operand types: SHLD reg16/32, reg16/32, imm8/cl SHLD mem16/32, reg16/32, imm8/cl o SHRD Example: Shift AX 4 bits to the right and replace its highest 4 bits with the low 4 bits of DX:.code mov ax,234bh mov dx,7654h shrd ax,dx,4 Before: After: DX AX 7654 234B 7654 4234 Page 6 / 14

7.3 Shift and Rotate Applications 201 7.3.1 Shifting Multiple Doublewords 201 Programs sometimes need to shift all bits within an array, as one might when moving a bitmapped graphic image from one screen location to another. The following shifts an array of 3 doublewords 1 bit to the right.data ArraySize = 3 array DWORD ArraySize DUP(99999999h) ; 1001 1001....code mov esi,0 shr array[esi + 8],1 ; high dword rcr array[esi + 4],1 ; middle dword, include Carry rcr array[esi],1 ; low dword, include Carry 7.3.2 Binary Multiplication 202 We already know that SHL performs unsigned multiplication efficiently when the multiplier is a power of 2. You can factor any binary number into powers of 2. For example, to multiply EAX * 36, factor 36 into 32 + 4 and use the distributive property of multiplication to carry out the operation: EAX * 36 = EAX * (32 + 4) = (EAX * 32) + (EAX * 4) mov eax,123 mov ebx,eax shl eax,5 ; mult by 32 (2 5 ) shl ebx,2 ; mult by 4 (2 2 ) add eax,ebx Page 7 / 14

7.4 Multiplication and Division Operations 204 7.4.1 MUL Instruction 204 The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or 32-bit operand by either AL, AX, or EAX. The instruction formats are: MUL r/m8 MUL r/m16 MUL r/m32 Implied operands: Examples: o 100h * 2000h, using 16-bit operands:.data val1 WORD 2000h val2 WORD 100h.code mov ax,val1 mul val2 ; DX:AX = 00200000h, =1 o 12345h * 1000h, using 32-bit operands: mov eax,12345h mov ebx,1000h mul ebx ; EDX:EAX = 0000000012345000h, =0 7.4.2 IMUL Instruction 205 IMUL (signed integer multiply ) multiplies an 8-, 16-, or 32-bit signed operand by either AL, AX, or EAX Preserves the sign of the product by sign-extending it into the upper half of the destination register Examples: o Multiply 48 * 4, using 8-bit operands mov al,48 ; AL = 30h mov bl,4 imul bl ; AX = 00C0h, OF=1 Note: OF=1 because AH is not a sign extension of AL o Multiply 4,823,424 * -423 mov eax,4823424 The Carry flag indicates whether or not the upper half of the product contains significant digits Page 8 / 14

mov ebx,-423 imul ebx ; EDX:EAX = FFFFFFFF86635D80h, OF=0 Note: OF=0 because EDX is a sign extension of EAX 7.4.4 DIV Instruction 208 The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit division on unsigned integers A single operand is supplied (register or memory operand), which is assumed to be the divisor Instruction formats: DIV r/m8 DIV r/m16 DIV r/m32 Default Operands: Examples o Divide 8003h by 100h, using 16-bit operands: mov dx,0 ; clear dividend, high mov ax,8003h ; dividend, low mov cx,100h div cx ; divisor ; AX = 0080h, DX = 3 o Same division, using 32-bit operands: mov edx,0 ; clear dividend, high mov eax,8003h ; dividend, low mov ecx,100h ; divisor div ecx ; EAX = 00000080h, DX = 3 Page 9 / 14

7.4.5 Signed Integer Division 209 Signed integers must be sign-extended before division takes place Fill high byte/word/doubleword with a copy of the low byte/word/doubleword's sign bit For example, the high byte contains a copy of the sign bit from the low byte: 1 0 0 0 1 1 1 1 Source 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 Destination CBW, CWD, CDQ Instructions o The CBW, CWD, and CDQ instructions provide important sign-extension operations: o CBW (convert byte to word) extends AL into AH o CWD (convert word to doubleword) extends AX into DX o CDQ (convert doubleword to quadword) extends EAX into EDX o Example: mov eax,0ffffff9bh ; (-101) cdq ; EDX:EAX = FFFFFFFFFFFFFF9Bh IDIV Instruction o IDIV (signed divide) performs signed integer division o Same syntax and operands as DIV instruction o Example 1: 8-bit division of 48 by 5 mov al,-48 cbw ; extend AL into AH mov bl,5 idiv bl ; AL = -9, AH = -3 o Example 2: 16-bit division of 48 by 5 mov ax,-48 cwd ; extend AX into DX mov bx,5 idiv bx ; AX = -9, DX = -3 o Example 3: 32-bit division of 48 by 5 mov eax,-48 cdq ; extend EAX into EDX mov ebx,5 idiv ebx ; EAX = -9, EDX = -3 Page 10 / 14

7.4.6 Implementing Arithmetic Expressions 211 Unsigned Arithmetic Expressions o Some good reasons to learn how to implement integer expressions: Learn how do compilers do it Test your understanding of MUL, IMUL, DIV, IDIV Check for overflow (Carry and Overflow flags) o Example: var4 = (var1 + var2) * var3 mov eax,var1 add eax,var2 ; EAX = var1 + var2 mul var3 ; EAX = EAX * var3 jc TooBig ; check for carry mov var4,eax ; save product Signed Arithmetic Expressions o Example 1: eax = (-var1 * var2) + var3 mov eax,var1 neg eax imul var2 jo TooBig ; check for overflow add eax,var3 jo TooBig ; check for overflow o Example 2: var4 = (var1 * 5) / (var2 3) mov eax,var1 ; left side mov ebx,5 imul ebx ; EDX:EAX = product mov ebx,var2 ; right side sub ebx,3 idiv ebx ; EAX = quotient mov var4,eax o Example 3: var4 = (var1 * -5) / (-var2 % var3) mov eax,var2 ; begin right side neg eax cdq ; sign-extend dividend idiv var3 ; EDX = remainder mov ebx,edx ; EBX = right side mov eax,-5 ; begin left side imul var1 ; EDX:EAX = left side idiv ebx ; final division mov var4,eax ; quotient Page 11 / 14

7.5 Extended Addition and Subtraction 213 7.5.1 ADC Instruction 213 ADC (add with carry) instruction adds both a source operand and the contents of the Carry flag to a destination operand. Operands are binary values Same syntax as ADD, SUB, etc. Example: Add two 32-bit integers (FFFFFFFFh + FFFFFFFFh), producing a 64-bit sum in EDX:EAX: mov edx,0 mov eax,0ffffffffh add eax,0ffffffffh adc edx,0 ;EDX:EAX = 00000001FFFFFFFEh Example: add 1 to EDX:EAX o Starting value of EDX:EAX: 00000000FFFFFFFFh o Add the lower 32 bits first, setting the Carry flag. o Add the upper 32 bits, and include the Carry flag mov edx,0 ; set upper half mov eax,0ffffffffh ; set lower half add eax,1 ; add lower half adc edx,0 ; add upper half EDX:EAX=00000001 00000000 7.5.2 Extended Addition Example 213 Extended Precision Addition o Adding two operands that are longer than the computer's word size (32 bits). o Virtually no limit to the size of the operands o The arithmetic must be performed in steps o The Carry value from each step is passed on to the next step TITLE Extended Addition Example (ExtAdd.asm) ; This program calculates the sum of two 64-bit integers. ; Chapter 7 example. ; Last update: 06/01/2006 INCLUDE Irvine32.inc.data op1 QWORD 0A2B2A40674981234h op2 QWORD 08010870000234502h sum DWORD 3 dup(0ffffffffh) ; = 0000000122C32B0674BB5736.code main PROC mov esi,offset op1 mov edi,offset op2 mov ebx,offset sum mov ecx,2 ; first operand ; second operand ; sum operand ; number of doublewords Page 12 / 14

call Extended_Add ; Display the sum. mov eax,sum + 8 call WriteHex mov eax,sum + 4 call WriteHex mov eax,sum call WriteHex call Crlf ; display high-order dword ; display middle dword ; display low-order dword exit main ENDP ;-------------------------------------------------------- Extended_Add PROC ; ; Calculates the sum of two extended integers 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. ; The sum must be one doubleword longer than the ; input operands. ;-------------------------------------------------------- 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 mov dword ptr [ebx],0 adc dword ptr [ebx],0 popad ret Extended_Add ENDP END main ; clear high dword of sum ; add any leftover carry Page 13 / 14

7.5.3 SBB Instruction 214 The SBB (subtract with borrow) instruction subtracts both a source operand and the value of the Carry flag from a destination operand. Operand syntax: same as for the ADC instruction Example: Subtract 1 from EDX:EAX o Starting value of EDX:EAX: 0000000100000000h o Subtract the lower 32 bits first, setting the Carry flag. o Subtract the upper 32 bits, and include the Carry flag. mov edx,1 ; set upper half mov eax,0 ; set lower half sub eax,1 ; subtract lower half sbb edx,0 ; subtract upper half EDX:EAX = 00000000 FFFFFFFF Page 14 / 14