1 Multiplication, Division, and Numerical Conversions Chapter 6.

Slides:



Advertisements
Similar presentations
COMP 2003: Assembly Language and Digital Logic
Advertisements

Introduction to Computer Engineering by Richard E. Haskell Multiplication and Division Instructions Module M16.4 Section 10.4.
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
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
Assembly Language Lecture 9
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.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
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
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
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
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
ASCII and BCD Arithmetic Chapter 11 S. Dandamudi.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
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.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Arithmetic and Logic Instructions
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
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.
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
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.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Multiplication and Division instructions Dr.Hadi AL Saadi.
Data Transfers, Addressing, and Arithmetic
Assembly Language for Intel-Based Computers, 5th Edition
Basic Assembly Language
Multiplication and Division Instructions
4.2 Arithmetic Instructions
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
X86’s instruction sets.
Chapter 4: Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
Shift & Rotate Instructions)
Multiplication and Division Instructions
Assembly Language for Intel-Based Computers, 4th Edition
Shift & Rotate 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
Division instruction.
Computer Architecture and System Programming Laboratory
Presentation transcript:

1 Multiplication, Division, and Numerical Conversions Chapter 6

2 Chapter Overview  We will first study the basic instructions for doing multiplications and divisions  We then use these instructions to:  Convert a string of ASCII digits into the binary number that this string represents  Convert a binary number (stored in some register) into a string of ASCII digits that represents its numerical value  We will also use the XLAT instruction to perform character encoding

3 Integer Multiplication  Contrary to addition, the multiplication operation depends on the interpretation:  no interpretation: FFh x 2h = ??  unsigned interp.: 255 x 2 = 510  signed interpret.: -1 x 2 = -2  We thus have two different multiplication instructions: MUL source ;for unsigned multiplication IMUL source ;for signed multiplication  Where source must be either mem or reg

4 Multiplication (cont.)  Source is being multiplied by:  AL if source is of type byte  AX if source is of type word  EAX if source is of type dword  The result of MUL/IMUL is stored in:  AX if source is of type byte  DX:AX if source is of type word  EDX:EAX if source is of type dword  Hence, there is always enough storage to hold the result

5 Multiplication (cont.)  Nevertheless, CF=OF=1 iff the result cannot be contained within the least significant half (lsh) of its storage location  lsh = AL if source is of type byte  lsh = AX if source is of type word  lsh = EAX if source is of type dword  For MUL:  CF=OF=0 iff the most significant half (msh) is 0  For IMUL:  CF=OF=0 iff the msh is the sign extension of the lsh

6 Examples of MUL and IMUL  Say that AX = 1h and BX = FFFFh, then:  InstructionResult DXAXCF/OF mul bx FFFF 0 imulbx-1FFFFFFFF 0  Say that AX = FFFFh and BX = FFFFh, then:  InstructionResult DX AX CF/OF mulbx FFFE imulbx

7 Examples of MUL and IMUL (cont.)  AL = 30h and BL = 4h, then:  InstructionResultAHALCF/OF mulbl19200C0 0 imulbl19200C0 1  AL = 80h and BL = FFh, then  InstructionResultAHALCF/OF mulbl326407F80 1 imulbl

8 Two-Operand Form for IMUL  Contrary to MUL, the IMUL instruction can be used with two operands: IMUL destination,source  The source operand can be imm, mem, or reg. But the destination must be a 16-bit or 32-bit register.  The product is stored (only) into the destination operand. No other registers are changed. Ex: MOV eax,1;eax = h IMUL ax,-1;eax = 0000FFFFh, CF=OF=0 MOV eax,100h ;eax = IMUL ax,100h ;eax = h, CF=OF=1 MOV eax,100h IMUL eax,100h ;eax = h, CF=OF=0

9 Exercise 1  Give the hexadecimal content of AX and the values of CF and OF immediately after the execution of each instruction below  IMUL AH ; when AX = 0FE02h  MUL BH ; when AL = 8Eh and BH = 10h  IMUL BH ; when AL = 9Dh and BH = 10h  IMUL AX,0FFh ; when AX = 0FFh

10 Integer Division  Notation for integer division:  Ex: 7  2 = (3, 1)  dividend   divisor = (quotient, remainder)  We have 2 instructions for division:  DIV divisor;unsigned division  IDIV divisor;signed division  The divisor must be reg or mem  Convention for IDIV: the remainder has always the same sign as the dividend.  Ex: -5  2 = (-2, -1) ; not: (-3, 1)

11 Division (cont.)  The divisor determines what will hold the dividend, the quotient, and the remainder:  DivisorDividendQuotientRemainder byteAXALAH wordDX:AXAXDX dwordEDX:EAXEAXEDX  The effect on the flags is undefined  We have a divide overflow whenever the quotient cannot be contained in its destination (AL if divisor is byte...)  execution then traps into the OS which displays a message on screen and terminates the program

12 Examples of DIV and IDIV  DX = 0000h, AX = 0005h, BX = FFFEh:  InstructionQuot.Rem.AXDX divbx idivbx-21FFFE0001  DX = FFFFh, AX = FFFBh, BX = 0002h:  InstructionQuot.Rem.AXDX idivbx-2-1FFFEFFFF divbxDivide Overflow

13 Examples of DIV and IDIV (cont.)  AX = 0007, BX = FFFEh:  InstructionQuot.Rem.ALAH divbl idivbl-31FD01  AX = 00FBh, BX = 0CFFh:  InstructionQuot.Rem.ALAH divbl025100FB idivblDivide Overflow

14 Exercise 2  Give the hexadecimal content of AX immediately after the execution of each instruction below or indicate if there is a divide overflow  IDIV BL ; when AX = 0FFFBh and BL = 0FEh  IDIV BL ; when AX = 0080h and BL = 0FFh  DIV BL ; when AX = 7FFFh and BL = 08h

15 Preparing for a division  Recall that:  For a byte divisor: the dividend is in AX  For a word divisor:the dividend is in DX:AX  For a dword divisor:the dividend is in EDX:EAX  If the dividend occupies only its least significant half (lsh) we must prepare its most significant half (msh) for a division  For DIV: the msh must be zero  For IDIV: the msh must be the sign extension of the lsh

16 Preparing for IDIV  To fill the msh of the dividend with the sign extension of its lsh, we use:  CBW (convert byte to word): fills AH with the sign extension of AL  CWD (convert word to double word): fills DX with the sign extension of AX  CDQ (convert double to quad): fills EDX with the sign extension of EAX  Sign extension (recall):  if AX = 8AC0h, then CWD will set DX to FFFFh  if AX = 7F12h, then CWD will set DX to 0000h

17 Preparing for DIV or IDIV  To divide the unsigned number in AX by the unsigned number in BX, you must do xor dx,dx ;to fill DX with 0 div bx  To divide the signed number in AX by the signed number in BX, you must do cwd ;to fill DX with sign extension of AX idiv bx  Never assign the msh of the dividend to zero before performing IDIV

18 The XLAT instruction  The XLAT instruction (without any operands) is the basic tool for character translation.  Upon execution of: XLAT  The byte pointed by EBX + AL is moved to AL.data table db ‘ ABCDEF’.code mov ebx,offset table mov al,0Ah xlat;AL = ‘A’ = 41h ;converts from binary to ASCII code of hex digit

19 Character Encoding  This is a table to encode numerical and alphabetical characters:.data codetable label byte db 48 dup(0) ; no translation db ' ' ; ASCII codes db 7 dup (0) ; no translation db 'GVHZUSOBMIKPJCADLFTYEQNWXR' db 6 dup (0) ; no translation db 'gvhzusobmikpjcadlftyeqnwxr' db 133 dup(0) ; no translation

20 mov ebx,offset codetable nextchar: getch ;char in AL mov edx,eax ;save original in DL xlat ;translate char in AL cmp al,0 ;not translatable? je putchar ;then write original mov edx,eax ;else write translation putchar: putch edx ;write DL to output jmp nextchar Character Encoding (cont.)  This is a code snippet to encode (only) numerical and alphabetical characters:

21 Binary to ASCII Conversion  We want to convert a binary number into the string of ASCII digits that represents its unsigned value (for display).  Ex: if AX = 4096, to generate the string “4096” we divide by 10 until the quotient is 0: Dividend / 10 = Quotient Remainder 4096 / 10 = / 10 = / 10 = 40 4 / 10 = 04 ASCII String:

22 Binary to ASCII Conversion (cont.)  The same method can be used to obtain the ASCII string of digits with respect to any base  Ex: if AX = 10C4h = 4292, to generate the string “10C4” we divide by 16 until the quotient is 0: Dividend / 16 = Quotient Remainder 4292 / 16 = / 16 = / 16 = 10 1 / 16 = 01 ASCII String: 1 0 C 4

23 Binary to ASCII Conversion (cont.)  The Wuint procedure displays the ASCII string of the unsigned value in EAXWuint  EBX contains a radix value (2 to 16) that determines the base of the displayed number  The Wsint procedure displays the ASCII string of the signed value in EAX:Wsint  Check the sign bit. If the value is negative, perform two’s complement (with the NEG instruction) and display “-”  Then use the same algorithm to display the digits of the (now) positive number

24 ASCII to Binary Conversion  To convert a sequence of ASCII digits into its numerical value:  for each new digit, we multiply by the base and add the new digit.  Ex: to convert “4096” into its base-10 value: Value Before New Digit Value After 0 x = 4 4 x = x = x = 4096 Final value

25 ASCII to Binary Conversion (cont.)  The Rint procedure reads a string of ASCII decimal digits and stores the base 10 numerical value into EAXRint  For signed numbers: the sequence of digits can be preceded by a sign.  Checks for overflows at each multiplication and addition  The next program uses both Rint and Wsint.386.model flat include csi2121.inc.data msg1 db “Enter an int: “,0 msg2 db “EAX = “,0.code main: putstr msg1 call Rint putstr msg2 mov ebx,10 call Wsint ret ;from main include Wsint.asm include Rint.asm end