Logical & shift ops (1) Fall 2007 Lecture 05: Logical Operations.

Slides:



Advertisements
Similar presentations
Lecture 5: MIPS Instruction Set
Advertisements

Systems Architecture Lecture 5: MIPS Instruction Set
Chapter 2.
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
Binary Logic (review) Basic logical operators: (Chapter 7 expanded)
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
Modified from the notes by
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 12 – Introduction to MIPS Procedures II & Logical Ops IBM has created.
1 CS61C L10 Fl. Pt. © UC Regents CS61C - Machine Structures Lecture 10 - Floating Point, Part II and Miscellaneous September 29, 2000 David Patterson
Lecturer PSOE Dan Garcia
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 12 – Introduction to MIPS Procedures II & Logical Ops As of mid-2007,
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
1 Texas A&M University Computer Science Department CPSC 321 Computer Organization ISA Part II Logical and Shift Operations Instruction Representation Instructor:
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
EEM 486 EEM 486: Computer Architecture Lecture 2 MIPS Instruction Set Architecture.
CS61C L9 MIPS Logical & Shift Ops, and Instruction Representation I (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c.
CS61C L9 MIPS Proc II, Logical & Shift Ops, and Instruction Representation I (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c.
Shift Instructions (1/4)
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
CS61C L12 Introduction to MIPS : Procedures II & Logical Ops (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
1 Lecture 4: Arithmetic for Computers (Part 4) CS 447 Jason Bakos.
CS 61C L08 MIPS Procedures (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #8: MIPS Procedures
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
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.
BITWISE OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /08/2013 Lecture 10: MIPS Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL STATE.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
What is a program? A sequence of steps
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic6: Logic, Multiply and Divide Operations José Nelson Amaral.
Csci136 Computer Architecture II Lab#5 Arithmetic Review ALU Design Ripple Carry Adder & Carry lookahead HW #4: Due on Feb 22, before class Feb.16, 2005.
Microprocessor & Assembly Language
CS Computer Organization Numbers and Instructions Dr. Stephen P. Carl.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
Binary Logic (review) Basic logical operators:(Chapter 7 expanded) NOT AND – outputs 1 only if both inputs are 1 OR – outputs 1 if at lest one input is.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Procedures II, Logical & Shift Ops. Review Functions called with jal, return with jr $ra. The stack is your friend: Use it to save anything you need.
COMPUTER ARCHITECTURE & OPERATIONS I
Morgan Kaufmann Publishers
Lecture 4: MIPS Instruction Set
March 2006 Saeid Nooshabadi
Lecture 4: MIPS Instruction Set
MISP Assembly.
Systems Architecture Lecture 5: MIPS Instruction Set
Computer Architecture & Operations I
ECE232: Hardware Organization and Design
There is one handout today at the front and back of the room!
The University of Adelaide, School of Computer Science
Topic 6: Bitwise Instructions
The University of Adelaide, School of Computer Science
MIPS Assembly.
Computer Architecture
CS61C : Machine Structures Lecture 2. 2
3.
Fixed Point Arithmetic Operations
MIPS Assembly.
MIPS assembly.
MIPS Arithmetic and Logic Instructions
MIPS Arithmetic and Logic Instructions
MIPS instructions.
Presentation transcript:

Logical & shift ops (1) Fall 2007 Lecture 05: Logical Operations

Logical & shift ops (2) Fall 2007 Bitwise Operations Up until now, we’ve done arithmetic ( add, sub,addi ), memory access ( lw and sw ), and branches and jumps. All of these instructions view contents of register as a single value (such as a signed or unsigned integer) New Perspective: View register as 32 raw bits rather than as a single 32-bit number Since registers are composed of 32 bits, we may want to access individual bits (or groups of bits) rather than the total value. Introduce two new classes of instructions: Logical & Shift Ops

Logical & shift ops (3) Fall 2007 Logical Operators (1/3) Two basic logical operators: AND: outputs 1 only if both inputs are 1 OR: outputs 1 if at least one input is 1 Truth Table: A B A AND B A OR B

Logical & shift ops (4) Fall 2007 Logical Operators (2/3) Logical Instruction Syntax: 1 2,3,4 where 1) operation name 2) register that will receive value 3) first operand (register) 4) second operand (register) or immediate (numerical constant) In general, can define them to accept more than 2 inputs, but in the case of MIPS assembly, these accept exactly 2 inputs and produce 1 output Again, rigid syntax, simpler hardware

Logical & shift ops (5) Fall 2007 Logical Operators (3/3) Instruction Names: and, or : Both of these expect the third argument to be a register andi, ori : Both of these expect the third argument to be an immediate MIPS Logical Operators are all bitwise, meaning that bit 0 of the output is produced by the respective bit 0’s of the inputs, bit 1 by the bit 1’s, etc. In C: Bitwise AND is & (e.g., z = x & y; ) In C: Bitwise OR is | (e.g., z = x | y; )

Logical & shift ops (6) Fall 2007 Uses for Logical Operators (1/3) Notes and a bit with 0 produces a 0 at the output and a bit with 1 produces the original bit. This can be used to create a mask. Example: The result of and ing these: mask: mask last 12 bits

Logical & shift ops (7) Fall 2007 Uses for Logical Operators (2/3) The second bitstring in the example is called a mask. it is used to isolate the rightmost 12 bits of the first bitstring by masking out the rest of the string (e.g. setting it to all 0s). Thus, the and operator can be used to set certain portions of a bitstring to 0s, without changing the rest. if the first bitstring in the above example were in $t0, then the following instruction would mask it: andi $t0,$t0,0xFFF

Logical & shift ops (8) Fall 2007 Uses for Logical Operators (3/3) Note or a bit with 1 produces a 1 at the output or a bit with 0 produces the original bit. This can be used to change certain bits of a string to 1s. For example, if $t0 contains 0x , then after this instruction: ori$t0, $t0, 0xFFFF … $t0 contains 0x1234FFFF (e.g. the high-order 16 bits are untouched, while the low-order 16 bits are forced to 1s).

Logical & shift ops (9) Fall 2007 Shift Instructions (1/4) Move (shift) all the bits in a word to the left or right by a number of bits. Example: shift right by 8 bits Example: shift left by 8 bits

Logical & shift ops (10) Fall 2007 Shift Instructions (2/4) Shift Instruction Syntax: 1 2,3,4 where 1) operation name 2) register that will receive value 3) first operand (register) 4) shift amount (constant < 32) MIPS shift instructions: 1. sll (shift left logical): shifts left and fills emptied bits with 0s 2. srl (shift right logical): shifts right and fills emptied bits with 0s 3. sra (shift right arithmetic): shifts right and fills emptied bits by sign extending

Logical & shift ops (11) Fall 2007 Shift Instructions (3/4) Example: shift right arith by 8 bits Example: shift right arith by 8 bits

Logical & shift ops (12) Fall 2007 Shift Instructions (4/4) Since shifting may be faster than multiplication, a good compiler usually compiles C code multiplies by a power of 2 to a shift instruction: a *= 8; (in C) would be compiled to: sll $s0,$s0,3 (in MIPS) Likewise, shift right to divide by powers of 2 remember to use sra

Logical & shift ops (13) Fall 2007 Example: Uses for Shift Instructions (1/4) Extracting byte 0 (rightmost 8 bits) of a word in $t0. Simply use: andi $t0,$t0,0xFF Isolating byte 1 (bit 15 to bit 8) of a word in $t0. We can use: andi $t0,$t0,0xFF00 but then we still need to shift to the right by 8 bits...

Logical & shift ops (14) Fall 2007 Example: Uses for Shift Instructions (2/4) Could use instead: sll $t0,$t0,16 srl $t0,$t0,

Logical & shift ops (15) Fall 2007 Example: Uses for Shift Instructions (3/4) In decimal: Multiplying by 10 is same as shifting left by 1: x = x = Multiplying by 100 is same as shifting left by 2: x = x = Multiplying by 10 n is same as shifting left by n

Logical & shift ops (16) Fall 2007 Example: Uses for Shift Instructions (4/4) In binary: Multiplying by 2 is same as shifting left by 1: x 10 2 = x 10 2 = Multiplying by 4 is same as shifting left by 2: x = x = Multiplying by 2 n is same as shifting left by n

Logical & shift ops (17) Fall 2007 “And in Conclusion…” Logical and Shift Instructions Operate on bits individually, unlike arithmetic, which operate on entire word. Use to isolate fields, either by masking or by shifting back and forth. Use shift left logical, sll, for multiplication by powers of 2 Use shift right arithmetic, sra, for division by powers of 2. New Instructions: and,andi, or,ori, sll,srl,sra