Integer Multiplication, Division Arithmetic shift

Slides:



Advertisements
Similar presentations
B261 Systems Architecture
Advertisements

Integer Arithmetic: Multiply, Divide, and Bitwise Operations
Chapter 3 Arithmetic for Computers. Exam 1 CSCE
Statement Format MIPS assembly language statements have the following format label: opcode operand,operand,operand # comment Label identifies the statement.
Lecture 15: Computer Arithmetic Today’s topic –Division 1.
CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, ©
Lecture Objectives: 1)Perform binary division of two numbers. 2)Define dividend, divisor, quotient, and remainder. 3)Explain how division is accomplished.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
Lecture 9 Sept 28 Chapter 3 Arithmetic for Computers.
Integer Multiplication and Division
1 Lecture 8: Binary Multiplication & Division Today’s topics:  Addition/Subtraction  Multiplication  Division Reminder: get started early on assignment.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
Lecture Objectives: 1)Explain the relationship between addition and subtraction with twos complement numbering systems 2)Explain the concept of numeric.
Arithmetic for Computers
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Lec 13Systems Architecture1 Systems Architecture Lecture 13: Integer Multiplication and Division Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan.
July 2005Computer Architecture, The Arithmetic/Logic UnitSlide 1 Part III The Arithmetic/Logic Unit.
CSE378 Instr. encoding.1 Instruction encoding The ISA defines –The format of an instruction (syntax) –The meaning of the instruction (semantics) Format.
Chapter 3 Arithmetic for Computers (Integers). Florida A & M University - Department of Computer and Information Sciences Arithmetic for Computers Operations.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number.
Integer Multiplication and Division
Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming.
True Assembly Language Part I. The Assembly Process The process of translating a MAL code (an assembly language program) into machine code (a sequence.
Integer Multiplication and Division ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering.
Csci 136 Computer Architecture II – Multiplication and Division
EI 209 Chapter 3.1CSE, 2015 EI 209 Computer Organization Fall 2015 Chapter 3: Arithmetic for Computers Haojin Zhu ( )
Division Check for 0 divisor Long division approach – If divisor ≤ dividend bits 1 bit in quotient, subtract – Otherwise 0 bit in quotient, bring down.
Integers’ Representation. Binary Addition. Two's Complement. Unsigned number representation Binary Addition, Subtraction. Overflow of unsigned numbers.
CDA 3101 Spring 2016 Introduction to Computer Organization
Integer Multiplication, Division Arithmetic shift Twice the number of places MIPS multiply unit. mult, multu Significant bits Mfhi, mflo, div, divu Arithmetic.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic6: Logic, Multiply and Divide Operations José Nelson Amaral.
Integer Multiplication and Division COE 301 Computer Organization Dr. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University.
Integer Operations Computer Organization and Assembly Language: Module 5.
Integer Multiplication and Division ICS 233 Computer Architecture & Assembly Language Prof. Muhamed Mudawar College of Computer Sciences and Engineering.
COMPUTER ORGANIZATION ARITHMETIC YASSER MOHAMMAD.
CSE 340 Computer Architecture Spring 2016 MIPS Arithmetic Review.
Computer System Design Lecture 3
More Binary Arithmetic - Multiplication
Computer Architecture & Operations I
Integers’ Representation. Binary Addition. Two's Complement.
Computer Architecture & Operations I
Integer Multiplication and Division
Single Bit ALU 3 R e s u l t O p r a i o n 1 C y I B v b 2 L S f w d O
Morgan Kaufmann Publishers Arithmetic for Computers
Computer Organization and Design Instruction Sets - 2
Computer Organization and Design Instruction Sets - 2
Morgan Kaufmann Publishers
Morgan Kaufmann Publishers
Lecture 8: Binary Multiplication & Division
CDA 3101 Summer 2007 Introduction to Computer Organization
CDA 3101 Spring 2016 Introduction to Computer Organization
Computer Organization and Design Instruction Sets
CS352H: Computer Systems Architecture
Lecture 8: Addition, Multiplication & Division
Lecture 8: Addition, Multiplication & Division
Systems Architecture I
CDA 3101 Summer 2007 Introduction to Computer Organization
Part III The Arithmetic/Logic Unit
ECE232: Hardware Organization and Design
Computer Arithmetic Multiplication, Floating Point
Arithmetic Topics Basic operations Programming Implications
Flow of Control -- Conditional branch instructions
Morgan Kaufmann Publishers Arithmetic for Computers
Flow of Control -- Conditional branch instructions
CS352H Computer Systems Architecture
MIPS Arithmetic and Logic Instructions
Pointless Poll Clap if you like pizza!.
Number Representation
MIPS Arithmetic and Logic Instructions
Presentation transcript:

Integer Multiplication, Division Arithmetic shift Twice the number of places MIPS multiply unit. mult, multu Significant bits mfhi, mflo, div, divu Arithmetic shift. sra madd, maddu, msub, msub mul, mulo, mulou Textbook: Appendix A Central Connecticut State University, MIPS Tutorial. Chapter 14.

Review: MIPS programming model MIPS contains: Integer arithmetic processor Floating point arithmetic processor Whole system’s Control Processor In this model from the textbook we can see several additional groups of MIPS instructions : Integer Multiplication Division instructions. Floating point arithmetic instructions. System Control instructions

Twice the Number of Places The product of two N-place decimal integers may need 2N places. This is also true for numbers expressed in any base. In particular, the product of two integers expressed with N-bit binary may need 2N bits.

Significant bits The significant bits in a positive or unsigned number represented in binary are the most significant 1 bit (the leftmost 1 bit) and all bits to the right of it. For example, the following has 23 significant bits: 0000 0000 0100 0011 0101 0110 1101 1110 The significant bits in a negative number represented in two's complement are the most significant 0 bit (the leftmost 0 bit) and all bits to the right of it. For example, the following has 23 significant bits: 1111 1111 1011 1100 1010 1001 0010 0010 To ensure that a product has no more than 32 significant bits, ensure that the sum of the number of significant bits in each operand is 32 or less. We will mostly write programs that keep the result under 32 bits in length. When this is true, the result of a multiplication will be in lo How to keep the result of multiplication in 32 bits ? How large will be the operands in that case ?

MIPS Multiply Unit The multiply unit of MIPS contains two 32-bit registers called hi and lo. These are not general purpose registers. When two 32-bit operands are multiplied, hi and lo hold the 64 bits of the result. Bits 32 through 63 are in hi and bits 0 through 31 are in lo. 31 0 31 0 63 32 31 0

mult , multu There is a multiply instruction for unsigned operands - multu and a multiply instruction for signed operands (two's complement operands) - mult. Integer multiplication never causes a trap. Note: with add and addu, the operation carried out is the same with both instructions. The "u" means "don't trap overflow". With mult and multu, different operations are carried out. Neither instruction ever causes a trap. Will be different result for mult and multu if I use the same $s and $t for both?

mult , multu If we treat the binary patterns as two’s complement then 2x-3 should be equal to -6 – 11010. So we should use mult to get the correct two’s complement result. If we treat the binary patterns as unsigned numbers then 2x5 should be equal to unsigned 10. So we should use the unsigned multiplication. In this case multu will do the task.

The mfhi and mflo Instructions There are two instructions that move the result of a multiplication into a general purpose register: ## Program to calculate 5 × x – 74 #### Register Use: ## $8 x ## $9 result .text .globl main main: ori $8,$0,12 # put x into $8 ori $9,$0,5 # put 5 into $9 mult $9,$8 # lo <-- 5x mflo $9 # $9 = 5x addiu $9,$9,-74 # $9 = 5x - 74 mfhi $d # d <-- hi. Move From Hi mflo $d # d <-- lo. Move From Lo

div “/” and mod “%” in C 9 div 4 = 2 remainder 1: int 9 / 4 = 2 Example from K&R:   printf ("\n9 div 4 = 2 remainder 1:\n"); printf ("int 9 / 4 = %d\n", 9 / 4); //Quotient -> (lo) printf ("int 9 % 4 = %d\n", 9 % 4); //Remainder -> (hi) 9 div 4 = 2 remainder 1: int 9 / 4 = 2 int 9 % 4 = 1

The div and the divu Instructions With N-place integer division there are two results an N-place quotient and an N-place remainder. With 32-bit operands there will be (in general) two 32-bit results. MIPS uses the hi and lo registers for the results: div $s,$t # lo <-- s div t # hi <-- s mod t # two's complement divu $s,$t # lo <-- s div t # unsigned 9 4 1 2

Example ## Program to calculate (y + x) / (y - x) #### Register Use: ## $8 x, $9 y ## $10 x/y quotient, $11 x%y remainder .text .globl main main: ori $8,$0,8 # put x into $8 ori $9,$0,36 # put y into $9 addu $10,$9,$8 # $10 <-- (y+x) subu $11,$9,$8 # $11 <-- (y-x) div $10,$11 # hilo<--(y+x)/(y-x) mflo $10 # $10 <-- quotient mfhi $11 # $11 <-- remainder

Shift Right Arithmetic The problem is that a shift right logical moves zeros into the high order bit. This is correct in some situations, but not for dividing two's complement negative integers. An arithmetic right shift replicates the sign bit as needed to fill bit positions.

The madd, maddu, msub, msubu Instructions Multiply registers rs and rt and add the resulting 64-bit product to the 64-bit value in the concatenated registers lo and hi. madd $8, $9 # Multiply add maddu $8, $9 # Unsigned multiply add Multiply registers rs and rt and subtract the resulting 64-bit product from the 64-bit value in the concatenated registers lo and hi. msub $8, $9 # Multiply subtract msubu $8, $9 # Unsigned multiply subtract

Different multiplication examples ori $8, $0, 0x7FFF # Big numbers multiplication sll $8, $8, 16 ori $8, $8, 0xFFFF ori $9, $0, 0x7FFF sll $9, $9, 16 ori $9, $9, 0xFFFF mult $9, $8 madd $8, $9 # No yet overflow madd $8, $9 # Sign is changed 2's c.overflow but not trap maddu $8, $9 # No yet overflow maddu $8, $9 # Unsigned overflow but not trap # msub $8, $9 # msubu $8, $9 mul $10, $8, $9 # 32 bit multiplications. Without overflow mulo $10, $8, $9 # With 2's complement overflow detection and trap mulou $10, $8, $9 # With unsigned overflow detection and trap