Review 1/2 MIPS assembly language instructions mapped to numbers in 3 formats. CS61C Negative Numbers and Logical Operations R I J.

Similar documents
Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Unsigned Binary Integers

Unsigned Binary Integers

COMP MIPS instructions 2 Feb. 8, f = g + h i;

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

ECE260: Fundamentals of Computer Engineering

ECE260: Fundamentals of Computer Engineering

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Chapter 2A Instructions: Language of the Computer

CS61c MIDTERM EXAM: 3/17/99

UCB CS61C : Machine Structures

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Chapter 3 Arithmetic for Computers

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

CS61C : Machine Structures

CS61C : Machine Structures

Number Systems and Their Representations

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

CS61C : Machine Structures

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

Levels of Programming. Registers

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Lecture 4: MIPS Instruction Set

Chapter Three. Arithmetic

CSCI 402: Computer Architectures

CS61C : Machine Structures

CS61C L10 MIPS Instruction Representation II, Floating Point I (6)

Computer Architecture Set Four. Arithmetic

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

CS 61c: Great Ideas in Computer Architecture

ECE232: Hardware Organization and Design

Instructions: Language of the Computer

Arithmetic for Computers

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

CS61C : Machine Structures

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Reduced Instruction Set Computer (RISC)

Reduced Instruction Set Computer (RISC)

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Lecture 5: Procedure Calls

ECE 15B Computer Organization Spring 2010

ECE 154A Introduction to. Fall 2012

CS61C : Machine Structures

CS61C Machine Structures. Lecture 10 - MIPS Branch Instructions II. 2/8/2006 John Wawrzynek. (

CS3350B Computer Architecture MIPS Instruction Representation

CS 351 Exam 2 Mon. 11/2/2015

CS61C : Machine Structures

COMPUTER ORGANIZATION AND DESIGN

Review. Lecture #7 MIPS Decisions So Far...

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Instructions: Language of the Computer

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

CS61C Machine Structures. Lecture 13 - MIPS Instruction Representation I. 9/26/2007 John Wawrzynek. www-inst.eecs.berkeley.

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

Computer Architecture

Lecture 9: Disassembly

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

Decoding example Binary=>Decimal=>Assembly=>C? Start at program at address 4,194,304 (2 22 )

CENG3420 Lecture 03 Review

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Computer Organization and Design

CS 61c: Great Ideas in Computer Architecture

MIPS PROJECT INSTRUCTION SET and FORMAT

Lecture Topics. Announcements. Today: The MIPS ISA (P&H ) Next: continued. Milestone #1 (due 1/26) Milestone #2 (due 2/2)

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

Chapter 2. Instruction Set. RISC vs. CISC Instruction set. The University of Adelaide, School of Computer Science 18 September 2017

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: The MIPS ISA (P&H ) Consulting hours. Milestone #1 (due 1/26)

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

UCB CS61C : Machine Structures

Review: MIPS Organization

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng

MIPS Integer ALU Requirements

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

CPE 335 Computer Organization. MIPS Arithmetic Part I. Content from Chapter 3 and Appendix B

Computer Science 61C Spring Friedland and Weaver. Instruction Encoding

Chapter Two MIPS Arithmetic

Review. Steps to writing (stateless) circuits: Create a logic function (one per output)

Sheet For the following C code: a = b + c; b = a + c; d = a b; Write an equivalent assembly MIPS program. Solution:

Lecture 8: Addition, Multiplication & Division

CS 110 Computer Architecture MIPS Instruction Formats

Operations, Operands, and Instructions

CENG 3420 Lecture 06: Datapath

Chapter 3 Arithmetic for Computers. ELEC 5200/ From P-H slides

CS61C : Machine Structures

Transcription:

CS61C Negative Numbers and Logical Operations cs 61C L7 Number.1 Lecture 7 February 10, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson) www-inst.eecs.berkeley.edu/~cs61c/schedule.html Review 1/2 MIPS assembly language instructions mapped to numbers in 3 formats R I J 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits op rs rt rd shamt funct op rs rt immediate op address Op field determines format Binary => Decimal => Assembly => Symbolic Assembly => C Reverse Engineering or Disassembly Its hard to do, therefore people like shipping binary machine language more than assembly or C cs 61C L7 Number.2 Review 2/2 Programming language model of memory allocation and pointers Allocate in stack vs. heap vs. global areas Arguments passed call by value vs. call by reference Pointer in C is HLL version of machine address cs 61C L7 Number.3 Numbers: Review Number Base B => B symbols per digit: Base 10 (Decimal): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Base 2 (Binary): 0, 1 Number representation: d 31 d 30... d 2 d 1 d 0 d 31 x B 31 + d 30 x B 30 +... + d 2 x B 2 + d 1 x B 1 + d 0 x B 0 One billion (1,000,000,000 ten ) is 0011 1011 1001 1010 1100 1010 0000 0000 two 2 28 2 24 2 20 2 16 2 12 2 8 2 4 2 0 = 1x2 29 +1x2 28 +1x2 27 +1x2 25 +1x2 24 +1x2 23 +1x2 20 +1x2 19 +1x2 17 +1x2 15 +1x2 14 +1x2 11 +1x2 9 = 536,870,912 + 268,435,456 + 134,217,728 + 33,554,432 + 16,777,216 + 8,388,608 + 1,048,576 + 524,288+ 131,072+ 32,768+ 16,384+ 2,048+ 512 cs 61C L7 Number.4

Overview What if Numbers too Big? How Represent Negative Numbers? What if Result doesn t fit in register? More Compact Notation than Binary? Administrivia, What s this stuff good for Shift Instructions And/Or Instructions Conclusion cs 61C L7 Number.5 What if too big? Binary bit patterns above are simply representatives of numbers Numbers really have an infinite number of digits cs 61C L7 Number.6 with almost all being zero except for a few of the rightmost digits Just don t normally show leading zeros If result of add (or any other arithmetic operation), cannot be represented by these rightmost hardware bits, overflow is said to have occurred Up to Compiler and OS what to do How avoid overflow, allow it sometimes? Some languages detect overflow (Ada), some don t (C) MIPS solution is 2 kinds of arithmetic instructions to recognize 2 choices: cs 61C L7 Number.7 add (add), add immediate (addi), and subtract (sub) cause exceptions on overflow add unsigned (addu), add immediate unsigned (addiu), and subtract unsigned (subu) do not cause exceptions on overflow Compiler selects appropriate arithmetic MIPS C compilers produce addu, addiu, subu What if Overflow Detected? An exception (or interrupt ) occurs Address of the instruction that overflowed is saved in a register Computer jumps to predefined address to invoke appropriate routine for that exception Like an unplanned hardware function call Operating system decides what to do In some situations program continues after corrective code is executed MIPS support: exception program counter (EPC) contains address of that instruction move from system control (mfc0) to copy EPC cs 61C L7 Number.8

How Represent Negative Numbers? Obvious solution: add a separate sign! sign represented in a single bit! representation called sign and magnitude Shortcomings of sign and magnitude Where to put the sign bit: right? left? Separate sign bit means it has both a positive and negative zero, lead to problems for inattentive programmers: +0 = -0? Adder may need extra step size don t know sign in advance Thus sign and magnitude was abandoned Search for Negative Number Representation Obvious solution didn t work, find another What is result for unsigned numbers if tried to subtract large number from a small one? Would try to borrow from string of leading 0s, so result would have a string of leading 1s With no obvious better alternative, pick representation that made the hardware simple: leading 0s => positive, leading 1s => negative 000000...xxx is >=0, 111111...xxx is < 0 This representation called two s complement cs 61C L7 Number.9 cs 61C L7 Number.10 Two s Complement 0000... 0000 0000 0000 0000 two = 0 ten 0000... 0000 0000 0000 0001 two = 1 ten 0000... 0000 0000 0000 0010 two = 2 ten... 0111... 1111 1111 1111 1101 two = 2,147,483,645 ten 0111... 1111 1111 1111 1110 two = 2,147,483,646 ten 0111... 1111 1111 1111 1111 two = 2,147,483,647 ten 1000... 0000 0000 0000 0000 two = 2,147,483,648 ten 1000... 0000 0000 0000 0001 two = 2,147,483,647 ten 1000... 0000 0000 0000 0010 two = 2,147,483,646 ten... 1111... 1111 1111 1111 1101 two = 3 ten 1111... 1111 1111 1111 1110 two = 2 ten 1111... 1111 1111 1111 1111 two = 1 ten One zero, 1st bit => >=0 or <0, called sign bit Two s Complement Formula, Example Recognizing role of sign bit, can represent positive and negative numbers in terms of the bit value times a power of 2: d 31 x -2 31 + d 30 x 2 30 +... + d 2 x 2 2 + d 1 x 2 1 + d 0 x 2 0 Example 1111 1111 1111 1111 1111 1111 1111 1100 two = 1x-2 31 +1x2 30 +1x2 29 +... +1x2 2 +0x2 1 +0x2 0 = -2 31 + 2 30 + 2 29 +... + 2 2 + 0 + 0 = -2,147,483,648 ten + 2,147,483,644 ten = -4 ten but one negative with no positive 2,147,483,648 cs 61C L7 Number.11 ten cs 61C L7 Number.12

Overflow for Two s Complement Numbers? Adding (or subtracting) 2 32-bit numbers can yield a result that needs 33 bits sign bit set with value of result instead of proper sign of result since need just 1 extra bit, only sign bit can be wrong Op A B Result A + B >=0 >=0 <0 A + B <0 <0 >=0 A - B >=0 <0 <0 A - B <0 >=0 >=0 Adding operands with different signs, (subtracting with same signs) overflow cannot occur cs 61C L7 Number.13 Signed v. Unsigned Comparisons Note: memory addresses naturally start at 0 and continue to the largest address That is, negative addresses make no sense C makes distinction in declaration integer (int) can be positive or negative unsigned integers (unsigned int) only positive Thus MIPS needs two styles of compare Set on less than (slt) and set on less than immediate (slti) work with signed integers Set on less than unsigned (sltu) and set on less than immediate unsigned (sltiu) cs 61C L7 Number.14 Example: Signed v. Unsigned Comparisons $s0 has 1111 1111 1111 1111 1111 1111 1111 1100 two $s1 has 0011 1011 1001 1010 1000 1010 0000 0000 two What are $t0, $t1 after slt $t0,$s0,$s1 # signed compare sltu $t1,$s0,$s1 # unsigned compare $t0: -4 ten < 1,000,000,000 ten? $t1: 4,294,967,292 ten < 1,000,000,000 ten? cs 61C L7 Number.15 Administrivia Readings: (4.1,4.2,4.3) 3.7, 4.8 (skip HW) 3rd homework: Due Tonight 7PM 4th homework: Due 2/17 7PM Exercises 3.21, 4.3, 4.7, 4.14, 4.15, 4.31 2nd project: MIPS Disassembler Due Wed. 2/17 7PM Book is a valuable reference! Appendix A (as know more, easier to refer) Back inside cover has useful MIPS summary: instructions, descriptions, definitions, formats, opcodes, examples cs 61C L7 Number.16

What s This Stuff Good For? cs 61C L7 Number.17 Remote Diagnosis: NeoRest ExII, a high-tech toilet features microprocessor-controlled seat warmers, automatic lid openers, air deodorizers, water sprays and blow-dryers that do away with the need for toilet tissue. About 25 percent of new homes in Japan have a washlet, as these toilets are called. Toto's engineers are now working on a model that analyzes urine to determine blood-sugar levels in diabetics and then automatically sends a daily report, by modem, to the user's physician. One Digital Day, 1998 www.intel.com/onedigitalday Two s complement shortcut: Negation Invert every 0 to 1 and every 1 to 0, then add 1 to the result Sum of number and its inverted representation must be 111...111 two 111...111 two = -1 ten Let x mean the inverted representation of x Then x + x = -1 => x + x + 1 = 0 => x + 1 = -x Example: -4 to +4 to -4 x : 1111 1111 1111 1111 1111 1111 1111 1100 two x : 0000 0000 0000 0000 0000 0000 0000 0011 two +1: 0000 0000 0000 0000 0000 0000 0000 0100 two () : 1111 1111 1111 1111 1111 1111 1111 1011 two +1: 1111 1111 1111 1111 1111 1111 1111 1100 two cs 61C L7 Number.18 Two s complement shortcut: Sign extension Convert two s complement number represented in n bits to more than n bits e.g., 16-bit immediate field converted to 32 bits before adding to 32-bit register in addi Simply replicate the most significant bit (sign bit) of smaller quantity to fill new bits 2 s comp. positive number has infinite 0s to left 2 s comp. negative number has infinite 1s to left Bit representation hides most leading bits; sign extension restores some of them More Compact Representation v. Binary? Shorten numbers by using higher base than binary that converts easily into binary almost all computer data sizes are multiples of 4, so use hexadecimal (base 16) numbers base 16 a power of 2, convert by replacing group of 4 binary digits by 1 hex digit; and vice versa 0 0000 4 0100 8 1000 c 1100 1 0001 5 0101 9 1001 d 1101 2 0010 6 0110 a 1010 e 1110 3 0011 7 0111 b 1011 f 1111 Example: from before, 1 000 000 000 ten is 16-bit -4 ten to 32-bit: 1111 1111 1111 1100 two 0011 1011 1001 1010 1100 1010 0000 0000 two 1111 1111 1111 1111 1111 1111 1111 1100 two 3 b 9 a c a 0 0 hex cs 61C L7 Number.20 C uses notation 0x3b9aca00 cs 61C L7 Number.19

Logical Operations Operations on less than full words Fields of bits or individual bits Think of word as 32 bits vs. 2 s comp. integers or unsigned integers Need to extract bits from a word, insert bits into a word Extracting via Shift instructions C operators: << (shift left), >> (shift right) Inserting via And/Or instructions C operators: & (bitwise AND), (bitwise OR) cs 61C L7 Number.21 Shift Instructions Move all the bits in a word to the left or right, filling the emptied bits with 0s Before and after shift left 8 of $s0 ($16): 0000 0000 0000 0000 0000 0000 0000 1101 two 0000 0000 0000 0000 0000 1101 0000 0000 two MIPS instructions shift left logical (sll) and shift right logical (srl) sll $s0,$s0,8 # $s0 = $s0 << 8 bits Register Format, using shamt (shift amount)! 0 0 16 16 8 0 op rs rt rd shamt funct cs 61C L7 Number.22 31 Extracting a field of bits Extract bit field from bit 9 (left bit no.) to bit 2 (size=8 bits) of register $s1, place in rightmost part of register $s0 000000000000000000000000 Shift field as far left as possible (31-bit no.) and then as far right as possible (32-size) cs 61C L7 Number.23 9876543210 0000000000000000000000 000000000000000000000000 sll $s0,$s1,22 #8bits to left end (31-9) srl $s0,$s0,24 #8bits to right end(32-8) And instruction AND: bit-by-bit operation leaves a 1 in the result only if both bits of the operands are 1. For example, if registers $t1 and $t2 0000 0000 0000 0000 0000 1101 0000 0000 two 0000 0000 0000 0000 0011 1100 0000 0000 two After executing MIPS instruction and $t0,$t1,$t2 # $t0 = $t1 & $t2 Value of register $t0 0000 0000 0000 0000 0000 1100 0000 0000 two AND can force 0s where 0 in the bit pattern Called a mask since mask hides bits cs 61C L7 Number.24

Or instruction OR: bit-by-bit operation leaves a 1 in the result if either bit of the operands is 1. For example, if registers $t1 and $t2 0000 0000 0000 0000 0000 1101 0000 0000 two 0000 0000 0000 0000 0011 1100 0000 0000 two After executing MIPS instruction or $t0,$t1,$t2 # $t0 = $t1 & $t2 Value of register $t0 Inserting a field of bits (almost OK) Insert bit field into bit 9 (left bit no.) to bit 2 (size=8 bits) of register $s1 from rightmost part of register $s0 (rest is 0) 31 9876543210 000000000000000000000000 Mask out field, shift left field 2, OR in field 00000000 0000000000000000000000 00 0000 0000 0000 0000 0011 1101 0000 0000 two OR can force 1s where 1 in the bit pattern andi $s1,$s1,0xfc03 # mask out 9-2 sll $t0,$s0,2 # field left 2 or $s1,$s1,$t0 # OR in field If 0s in field of 1 operand, can insert new value cs 61C L7 Number.25 cs 61C L7 Number.26 Sign Extension of Immediates addi and slti: deal with signed numbers, so immediates sign extended Branch and data transfer address fields are sign extended too addiu and sltiu also sign extend! addiu really to avoid overflow; sltiu why? andi and ori work with unsigned integers, so immediates padded with leading 0s andi won t work as mask in upper 16 bits addiu $t1,$zero,0xfc03 #32b mask in $t1 and $s1,$s1,$t1 # mask out 9-2 sll $t0,$s0,2 # field left 2 Summary: 12 new instructions (with formats) Unsigned (no overflow) arithmetic: addu (R), subu (R), addiu (I) Unsigned compare: sltu (R), sltiu (I) Logical operations: and (R), or (R), andi (I), ori (I), sll (R), srl (R) Handle overflow exception: EPC register and mfc0 (R) or $s1,$s1,$t0 # OR in field cs 61C L7 Number.27 cs 61C L7 Number.28

Example: show C, assembly, machine Convert C code: Bit Fields in C struct { unsigned int ready: 1; /* bit 31 */ unsigned int enable: 1; /* bit 30 */ } rec; /* $S0 */ rec.enable = 1; rec.ready = 0; printf(ò%d %dò, rec.enable, rec.ready);... And in Conclusion... 1/1 Handling case when number is too big for representation (overflow) Representing negative numbers (2 s complement) Comparing signed and unsigned integers Manipulating bits within registers: shift and logical instructions Next time: characters, floating point numbers cs 61C L7 Number.29 cs 61C L7 Number.31