CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic6: Logic, Multiply and Divide Operations José Nelson Amaral.

Slides:



Advertisements
Similar presentations
B261 Systems Architecture
Advertisements

Integer Arithmetic: Multiply, Divide, and Bitwise Operations
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
1  2004 Morgan Kaufmann Publishers Chapter Three.
Integer Multiplication and Division
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
1 Lecture 8: Binary Multiplication & Division Today’s topics:  Addition/Subtraction  Multiplication  Division Reminder: get started early on assignment.
Arithmetic I CPSC 321 Andreas Klappenecker. Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
1  1998 Morgan Kaufmann Publishers Chapter Four Arithmetic for Computers.
Chapter 3 Arithmetic for Computers. Arithmetic Where we've been: Abstractions: Instruction Set Architecture Assembly Language and Machine Language What's.
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 Bits are just bits (no inherent meaning) — conventions define relationship between bits and numbers Binary numbers (base 2)
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
Computer Organization and Design Instruction Sets Montek Singh Wed, Sep 12, 2012 Lecture 5.
Lecture 6: Multiply, Shift, and Divide
CSE378 Instr. encoding.1 Instruction encoding The ISA defines –The format of an instruction (syntax) –The meaning of the instruction (semantics) Format.
Registers and MAL Lecture 12. The MAL Architecture MAL is a load/store architecture. MAL supports only those addressing modes supported by the MIPS RISC.
CSCI 136 Lab 1: 135 Review.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
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.
EI 209 Chapter 3.1CSE, 2015 EI 209 Computer Organization Fall 2015 Chapter 3: Arithmetic for Computers Haojin Zhu ( )
MIPS Assembly Language Chapter 13 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Addition, Subtraction, Logic Operations and ALU Design
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.
1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.
MIPS Assembly Language Chapter 15 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Computer Arthmetic Chapter Four P&H. Data Representation Why do we not encode numbers as strings of ASCII digits inside computers? What is overflow when.
Integer Operations Computer Organization and Assembly Language: Module 5.
9/23/2004Comp 120 Fall September Chapter 4 – Arithmetic and its implementation Assignments 5,6 and 7 posted to the class web page.
Pirouz Bazargan SabetDecember 2003 Effective Implementation of a 32-bit RISC Processor Pirouz Bazargan Sabet University of Paris 6 - LIP6 - ASIM
COMPUTER ORGANIZATION ARITHMETIC YASSER MOHAMMAD.
MIPS Arithmetic and Logic Instructions
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
Integer Multiplication, Division Arithmetic shift
Integer Multiplication and Division
Computer Organization and Design Instruction Sets - 2
Chapter Three : Arithmetic for Computers
Computer Organization and Design Instruction Sets - 2
CDA 3101 Summer 2007 Introduction to Computer Organization
CDA 3101 Spring 2016 Introduction to Computer Organization
Computer Organization and Design Instruction Sets
Lecture 8: Addition, Multiplication & Division
Lecture 8: Addition, Multiplication & Division
MPIS Instructions Functionalities of instructions Instruction format
MISP Assembly.
CDA 3101 Summer 2007 Introduction to Computer Organization
Computer Architecture & Operations I
ECE232: Hardware Organization and Design
MIPS Assembly.
Flow of Control -- Conditional branch instructions
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
Computer Architecture EECS 361 Lecture 6: ALU Design
MIPS Assembly.
Flow of Control -- Conditional branch instructions
MIPS assembly.
CS352H Computer Systems Architecture
Reduced Instruction Set Computer (RISC)
COMS 361 Computer Organization
MIPS Arithmetic and Logic Instructions
Pointless Poll Clap if you like pizza!.
MIPS Arithmetic and Logic Instructions
Presentation transcript:

CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic6: Logic, Multiply and Divide Operations José Nelson Amaral

CMPUT Computer Organization and Architecture I2 Reading Assignment Appendix A: Section A.9 Chapter 4: Section 4.2, 4.3, 4.4 parts of sections of

CMPUT Computer Organization and Architecture I3 Operating with Unsigned Numbers If a byte is loaded into a register using the lb instruction, the byte will be sign-extended to 32 bits before it is written into the destination register. For instance, if the byte 0x84 is stored in position 0x and the following instruction is executed: lb $t0, 0($gp) # $gp = 0x What is the value of $t0 after this instruction is executed? $t0 = 0xFFFFFF84 How could we get 0x loaded into $t0 from position 0x ? We need to use the load byte unsigned instruction for that effect: lbu $t0, 0($gp)

CMPUT Computer Organization and Architecture I4 sltu and sltiu Sometimes when comparing two values, we want to treat them as unsigned numbers. For this situation MIPS offers two instructions: set less than unsigned (sltu) and set less than immediate unsigned (sltiu) Example: Assume that the following values are stored in $s0 and $s1 $s0 = 0xFFFF FFFF $s1 = 0x What are the values of $t0 and $t1 after the following instructions are executed? slt$t0, $s0, $s1 sltu$t1, $s0, $s1 $t0 = 1 $t1 = 0

CMPUT Computer Organization and Architecture I5 addu, addiu, and subu MIPS also offers unsigned arithmetic operations that do not cause exceptions on overflow: addu: add unsigned addiu: add immediate unsigned subu: subtract unsigned The only difference between the signed instructions add, addi and sub, and the unsigned ones addu, addiu, and subu, is that the unsigned ones do not generate overflow exceptions. The immediate fields of addiu and sltiu are sign-extended! Pat-Hen. pp. 222 and pp. 230

CMPUT Computer Organization and Architecture I6 Logical Shifts MIPS provides bit shifting operations such as shift left logical (sll), and shift right logical (srl). When a logical shift occurs, the new bits introduced in the word are always zeros. Example: Assume that $s0 = What would $t2 contain after the following instruction is executed? sll$t2, $s0, 8# $t2  $s0 << 8 $t2 = Pat-Hen. pp. 226

CMPUT Computer Organization and Architecture I7 Example of logical shifts Propose a sequence of two MIPS instructions that performs the following binary transformation in $s0 (without affecting any other register): Before: $s0 = bbbb bbbb bbbb bbbb bbbb bbcc cccc ccbb After: $s0 = cccc cccc Solution: sll$s0, $s0, 22# $s0  $s0 << 22 # $s0 = cccc cccc bb srl$s0, $s0, 24# $s0  $s0 >> 24 # $s0 = cccc cccc Pat-Hen. pp. 229

CMPUT Computer Organization and Architecture I8 Integer Multiplication and Division using Shifts Shift operations can be used to perform integer multiplication and division by powers of 2. For example to multiply the integer stored in $t1 by 4, we could use the following instruction: sll $t1, $t1, 2# $t1  $t1 << 2 = 4  $t1 If $t1 contained, for instance 0x : Before: $t1 = = 2 3 = 8 10 After: $t1 = = 2 5 = Similarly, a shift right by 2 is equivalent to an integer division by 4.

CMPUT Computer Organization and Architecture I9 Arithmetic Shifts What happens if we shift the number -16 to the right by 2? = After a shift right logical of 2 we get: What is the decimal value represented by this binary number? +16 = = +1,073,741,820

CMPUT Computer Organization and Architecture I10 Something is amiss! -16 = Thus our logical right shift of -16: by 2 resulted in: +1,073,741,820 = If we want to use shift right as a fast alternative to division by powers of two, we need a different shift operation!

CMPUT Computer Organization and Architecture I11 Shift Right Arithmetic MIPS provides a shift right arithmetic (sra) operation that preserves the sign of the number shifted. If $t0 = 0xFFFF FFF0 = The following instruction: sra$t0, $t0, 2# $t0  $t0 >> a 2 = $t0/4 will produce: $t0 = 0xFFFF FFFC = -4 10

CMPUT Computer Organization and Architecture I12 A Note in Binary-to- Decimal Conversion When converting a positive binary number that has a long sequence of 1’s into decimal, is it useful to use the following observations: We wanted to convert the following binary number to decimal: X =

CMPUT Computer Organization and Architecture I13 A Note in Binary-to- Decimal Conversion When converting a positive binary number that has a long sequence of 1’s into decimal, is it useful to use the following observations: We want to convert the following binary number to decimal: X = = Y = = X = = = Y X = Y - 3 = = 2 10  2 10  X = 1024  1024  = +1,073,741,820

CMPUT Computer Organization and Architecture I14 Variable Shifts Sometimes we want to shift a value by a variable number of bits (computed by an algorithm, for instance). For this cases, MIPS provides the shift left logical variable (sllv), shift right logical variable (srlv), and shift right arithmetic variable (srav) instructions Example: What is the value stored in $s1 after the following instructions are executed? addi$t0, $zero, 4 addi$s0, $zero, -128 srav$s1, $s0, $t0 $s1 = -128/16 = -8

CMPUT Computer Organization and Architecture I15 Bitwise Logic Operations Bitwise logic instructions cause the values stored in two registers to be combined, bit by bit, by a logic operator. MIPS implements or, and, not, nor, xor. It also has immediate versions: andi, ori, xori Example: or $t0, $t1, $t2# $t0  $t1 | $t2 Pat-Hen. pp. 227

CMPUT Computer Organization and Architecture I16 Logic Functions If X=0 then X’=1 If X=1 then X’=0 NOT If A=1 AND B=1 then C=1 otherwise C=0 AND If A=1 OR B=1 then C=1 otherwise C=0 OR Pat-Hen. pp. 231

CMPUT Computer Organization and Architecture I17 Logic Functions If X=1 OR Y=1, but not both of them, then C=1 XOR If X=0 AND Y=0, then C=1 NOR

CMPUT Computer Organization and Architecture I18 Bitwise logic example Example: The following is the code of a leaf procedure. How would you describe what this procedure does? add$v0, $zero, $zero andi$t1, $a0, 3 beq$t1, $zero, end add$v0, $zero, 1 end:jr$ra

CMPUT Computer Organization and Architecture I19 Multiply in MIPS Multiplication in Decimal: 88 x two digits operands four digit product Similarly, the binary multiplication of two 32-bit operands produces a 64 bit product. The MIPS has two special register, Hi and Lo, to store the high and low parts of the 64 bit product generated by a multiply operation. Pat-Hen. pp. 264 The multiplication of two 2-digit operands resulted in a 4-digit product.

CMPUT Computer Organization and Architecture I20 Multiply in MIPS MIPS has a multiply instruction, mult, and a multiply unsigned, multu, instruction. MIPS also provides two special instructions to move data from Hi and Lo to the general purpose registers: move from Lo, mflo, and move from Hi, mfhi. Neither the mult nor the multu instruction detects overflow. Example: mult$s0, $s1# Hi  high[$s0  $s1]; Lo  low[$s0  $s1]; MIPS assembler implements a pseudo assembly instruction that moves the values from Hi and Lo into a register: mul$s0, $s1, $s2# $s0  $s1  $s2

CMPUT Computer Organization and Architecture I21 Integer Division Pat-Hen. pp. 273 Quotient Remainder The divide, div, instruction produces the quotient of the division in Lo and the remainder in Hi. The MIPS divide instruction ignores overflow. The MIPS assembler also provides a three register pseudo divide instruction to place the quotient in an specified register.