Multiplication and Division instructions Dr.Hadi AL Saadi.

Slides:



Advertisements
Similar presentations
Introduction to Computer Engineering by Richard E. Haskell Multiplication and Division Instructions Module M16.4 Section 10.4.
Advertisements

NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
1 x86’s instruction sets. 2 Instruction Set Classification  Transfer Move  Arithmetic Add / Subtract Mul / Div, etc.  Control Jump Call / Return, etc.
Assembly Language for x86 Processors 6th Edition
Department of Computer Science and Software Engineering
Assembly Language Lecture 9
1 Multiplication, Division, and Numerical Conversions Chapter 6.
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 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
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.
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:
Integer Arithmetic COE 205 Computer Organization and Assembly Language
CE302 Outline Multiplication Division Program Segment Prefix Command Line Parameters.
MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or EAX. MUL r/m8 MUL r/m16 MUL r/m32.
Assembly Language – Lab 5
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Chapter four – The 80x86 Instruction Set Principles of Microcomputers 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 1 Chapter.
Multiplication and Division Instructions & the 0Ah function.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register.
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.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
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.
CT215: Assembly Language Programming
Assembly Language for x86 Processors 6th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
Arithmetic Flags and Instructions Chapter 7 S. Dandamudi.
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
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
The Assemble, Unassemble commands of the debugger: U Command for converting machine code language source Equivalent machine code instructions Equivalent.
Introduction Arithmetic instructions are used to perform arithmetic operation such as Addition Subtraction Multiplication Division These operations can.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
Data Transfers, Addressing, and Arithmetic
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for x86 Processors 7th Edition
Basic Assembly Language
Multiplication and Division Instructions
CS-401 Computer Architecture & Assembly Language Programming
4.2 Arithmetic Instructions
X86’s instruction sets.
Chapter 4: Instructions
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
Multiplication and Division Instructions
Assembly Language for Intel-Based Computers, 4th Edition
More IA32 (AKA Pentium) Instructions
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
EECE.3170 Microprocessor Systems Design I
Chapter 5 Arithmetic and Logic Instructions
Multiplication and Division Instructions
Multiplication and Division Instructions
Chapter 8: Instruction Set 8086 CPU Architecture
Division instruction.
Computer Architecture and System Programming Laboratory
Presentation transcript:

Multiplication and Division instructions Dr.Hadi AL Saadi

Multiplication and Division Instructions Multiplication  More complicated than add / sub »Produces double-length results –E.g. Multiplying two 8 bit numbers produces a 16-bit result »Cannot use a single multiply instruction for signed and unsigned numbers –add and sub instructions work both on signed and unsigned numbers –For multiplication, we need separate instructions mul for unsigned numbers imul for signed numbers Implied operands:

Arithmetic Instructions (cont’d) Unsigned multiplication mul source »Depending on the source operand size, the location of the other source operand and destination are selected

Multiplication Instructions  Example mov AL,10 mov DL,25 mul DL produces 250D in AX register (result fits in AL) The imul instruction can use the same syntax »Also supports other formats  Example mov DL,0FFH ; DL = -1 mov AL,0BEH ; AL = -66 imul DL produces 66D in AX register (again, result fits in AL)

Division instruction  Even more complicated than multiplication »Produces two results –Quotient –Remainder »In multiplication, using a double-length register, there will not be any overflow –In division, divide overflow is possible  Pentium provides a special software interrupt when a divide overflow occurs  Two instructions as in multiplication div source for unsigned numbers idiv source for signed numbers

Dividend is twice the size of the divisor Dividend is assumed to be in  AX (8-bit divisor)  DX:AX (16-bit divisor)  EDX:EAX (32-bit divisor) Division instruction Default Operands:

Division instruction

Example mov AX,251 mov CL,12 div CL produces 20D in AL and 11D as remainder in AH Example sub DX,DX ; clear DX mov AX,141BH ; AX = 5147D mov CX,012CH ; CX = 300D div CX produces 17D in AX and 47D as remainder in DX Division instruction

Divide overflow Divide overflow happens when the quotient is too large to fit into the destination. mov ax, 1000h mov bl, 10h div bl It causes a CPU interrupt and halts the program. (divided by zero cause similar results)

Signed division requires some help »We extended an unsigned 16 bit number to 32 bits by placing zeros in the upper 16 bits »This will not work for signed numbers –To extend signed numbers, you have to copy the sign bit into those upper bit positions  Pentium provides three instructions in aiding sign extension »All three take no operands cbw converts byte to word (extends AL into AH) cwd converts word to doubleword (extends AX into DX) cdq converts doubleword to quadword (extends EAX into EDX) Division instruction

IDIV examples Example: 32-bit division of –48 by 5 mov eax,-48 cdq ; extend EAX into EDX mov ebx,5 idiv ebx ; EAX = -9, EDX = -3 Example: 16-bit division of –48 by 5 mov ax,-48 cwd; extend AX into DX mov bx,5 idiv bx; AX = -9, DX = -3

Example: 8-bit division of –48 by 5 mov al,-48 cbw ; extend AL into AH mov bl,5 idiv bl ; AL = -9, AH = -3

Example mov AL,-95 cbw ; AH = FFH mov CL,12 idiv CL produces  7D in AL and  11D as remainder in AH Example mov AX,-5147 cwd ; DX := FFFFH mov CX,300 idiv CX produces  17D in AX and  47D as remainder in DX

Example : write a code to compute the (8!) (Factorial) Fact = 1 For ( I = 8 ; I < 1 ; i--) fact *= i Mov CX,8 ; Cx is a counter Mov AX,1 ; Multiplication instruction uses DX:AX register pairs Mov DX,0 ; dx used to hold our factorial product ; initially set it to 1 Myloop: Mul CX ; DX:AX=AX*CX Dec CX Cmp CX,0 jne myloop

Example: var4 = (var1 + var2) * var3 mov eax,var1 add eax,var2 mul var3 jo TooBig; check for overflow mov var4,eax; save product Example: eax = (-var1 * var2) + var3 mov eax,var1 neg eax mul var2 jo TooBig; check for overflow add eax,var3

Example: var4 = (var1 * 5) / (var2 – 3) mov eax,var1 ; left side mov ebx,5 mul ebx ; EDX:EAX = product mov ebx,var2 ; right side sub ebx,3 div ebx ; final division mov var4,eax

Example: 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