June 14, 2016Machine Language and Pointers1 What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; }

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Goal: Write Programs in Assembly
Branches Two branch instructions:
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
1 ECE369 ECE369 Chapter 2. 2 ECE369 Instruction Set Architecture A very important abstraction –interface between hardware and low-level software –standardizes.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
ELEN 468 Advanced Logic Design
Chapter 2 Instructions: Language of the Computer
Chapter 2.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
1 Today  All HW1 turned in on time, this is great!  HW2 will be out soon —You will work on procedure calls/stack/etc.  Lab1 will be out soon (possibly.
CS2100 Computer Organisation MIPS Part III: Instruction Formats (AY2014/2015) Semester 2.
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
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.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
CS 300 – Lecture 6 Intro to Computer Architecture / Assembly Language Instructions.
MIPS Instruction Set Advantages
1 Today  Finish-up procedures/stack  strlen example  Machine language, the binary representation for instructions. —We’ll see how it is designed for.
ISA-2 CSCE430/830 MIPS: Case Study of Instruction Set Architecture CSCE430/830 Computer Architecture Instructor: Hong Jiang Courtesy of Prof. Yifeng Zhu.
Some material taken from Assembly Language for x86 Processors by Kip Irvine © Pearson Education, 2010 Slides revised 2/2/2014 by Patrick Kelley.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
November 18, 2015Memory and Pointers in Assembly1 What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; }
Ch2b- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Thanks for all the Memory! When 32 registers just won’t do. Many times (almost.
Computer Organization and Architecture Instructions: Language of the Machine Hennessy Patterson 2/E chapter 3. Notes are available with photocopier 24.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA.
Computer Organization & Programming Chapter 6 Single Datapath CPU Architecture.
Small constants are used quite frequently (50% of operands) e.g., A = A + 5; B = B + 1; C = C - 18; Solutions? Why not? put 'typical constants' in memory.
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
1 A single-cycle MIPS processor  An instruction set architecture is an interface that defines the hardware operations which are available to software.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,
December 26, 2015©2003 Craig Zilles (derived from slides by Howard Huang) 1 A single-cycle MIPS processor  As previously discussed, an instruction set.
Computer Organization Rabie A. Ramadan Lecture 3.
CENG 311 Instruction Representation
EE472 – Spring 2007P. Chiang, with Slide Help from C. Kozyrakis (Stanford) ECE472 Computer Architecture Lecture #3—Oct. 2, 2007 Patrick Chiang TA: Kang-Min.
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
Addressing Modes. Register Addressing Immediate Addressing Base Addressing Indexed Addressing PC-Relative Addressing.
February 22, 2016©2003 Craig Zilles (derived from slides by Howard Huang) 1 A single-cycle MIPS processor  As previously discussed, an instruction set.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
Lecture 17: 10/31/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
What does this code do? label: sub $a0, $a0, 1 bne $a0, $zero, label
MIPS Instruction Set Advantages
Morgan Kaufmann Publishers
MIPS Coding Continued.
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Instructions - Type and Format
CSCI206 - Computer Organization & Programming
Computer Architecture & Operations I
MIPS Instruction Encoding
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
MIPS Instruction Encoding
Instruction encoding The ISA defines Format = Encoding
Computer Instructions
COMS 361 Computer Organization
COMS 361 Computer Organization
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
MIPS Coding Continued.
Presentation transcript:

June 14, 2016Machine Language and Pointers1 What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; }

June 14, 2016Machine Language and Pointers2  Today we’ll discuss machine language, the binary representation for instructions. —We’ll see how it is designed for the common case Fixed-sized (32-bit) instructions Only 3 instruction formats Limited-sized immediate fields  Array Indexing vs. Pointers —Pointer arithmetic, in particular

June 14, 2016Machine Language and Pointers3 Assembly vs. machine language  So far we’ve been using assembly language. —We assign names to operations (e.g., add) and operands (e.g., $t0). —Branches and jumps use labels instead of actual addresses. —Assemblers support many pseudo-instructions.  Programs must eventually be translated into machine language, a binary format that can be stored in memory and decoded by the CPU.  MIPS machine language is designed to be easy to decode. —Each MIPS instruction is the same length, 32 bits. —There are only three different instruction formats, which are very similar to each other.  Studying MIPS machine language will also reveal some restrictions in the instruction set architecture, and how they can be overcome.

June 14, 2016Machine Language and Pointers4 R-type format  Register-to-register arithmetic instructions use the R-type format.  This format includes six different fields. —op is an operation code or opcode that selects a specific operation. —rs and rt are the first and second source registers. —rd is the destination register. —shamt is only used for shift instructions. —func is used together with op to select an arithmetic instruction.  The inside cover of the textbook lists opcodes and function codes for all of the MIPS instructions. oprsrtrdshamtfunc 6 bits5 bits 6 bits

June 14, 2016Machine Language and Pointers5 About the registers  We have to encode register names as 5-bit numbers from to —For example, $t8 is register $24, which is represented as —The complete mapping is given on page A-23 in the book.  The number of registers available affects the instruction length. —Each R-type instruction references 3 registers, which requires a total of 15 bits in the instruction word. —We can’t add more registers without either making instructions longer than 32 bits, or shortening other fields like op and possibly reducing the number of available operations.

June 14, 2016Machine Language and Pointers6 I-type format  Load, store, branch and immediate instructions all use the I-type format.  For uniformity, op, rs and rt are in the same positions as in the R-format.  The meaning of the register fields depends on the exact instruction. —rs is a source register—an address for loads and stores, or an operand for branch and immediate arithmetic instructions. —rt is a source register for branches and stores, but a destination register for the other I-type instructions.  The address is a 16-bit signed two’s-complement value. —It can range from -32,768 to +32,767. —But that’s not always enough! oprsrtaddress 6 bits5 bits 16 bits

June 14, 2016Machine Language and Pointers7  Larger constants can be loaded into a register 16 bits at a time. —The load upper immediate instruction lui loads the highest 16 bits of a register with a constant, and clears the lowest 16 bits to 0s. —An immediate logical OR, ori, then sets the lower 16 bits.  To load the 32-bit value : lui $s0, 0x003D# $s0 = 003D 0000 (in hex) ori $s0, $s0, 0x0900# $s0 = 003D 0900  This illustrates the principle of making the common case fast. —Most of the time, 16-bit constants are enough. —It’s still possible to load 32-bit constants, but at the cost of two instructions and one temporary register.  Pseudo-instructions may contain large constants. Assemblers including SPIM will translate such instructions correctly. —Yay, SPIM!! Larger constants

June 14, 2016Machine Language and Pointers8  The limited 16-bit constant can present problems for accesses to global data. —As we saw in our memory example, the assembler put our result variable at address 0x —0x is bigger than 32,767  In these situations, the assembler breaks the immediate into two pieces. lui$at, 0x1001# 0x lw $t1, 0x0004($at)# Read from Mem[0x ] Loads and stores

June 14, 2016Machine Language and Pointers9  For branch instructions, the constant field is not an address, but an offset from the current program counter (PC) to the target address. beq$at, $0, L add$v1, $v0, $0 add$v1, $v1, $v1 jSomewhere L:add$v1, $v0, $v0  Since the branch target L is three instructions past the beq, the address field would contain 3. The whole beq instruction would be stored as:  SPIM’s encoding of branches offsets is off by one, so the code it produces would contain an address of 4. (But it has a compensating error when it executes branches.) Branches oprsrtaddress

June 14, 2016Machine Language and Pointers10  Empirical studies of real programs show that most branches go to targets less than 32,767 instructions away—branches are mostly used in loops and conditionals, and programmers are taught to make code bodies short.  If you do need to branch further, you can use a jump with a branch. For example, if “Far” is very far away, then the effect of: beq$s0, $s1, Far... can be simulated with the following actual code. bne$s0, $s1, Next jFar Next:...  Again, the MIPS designers have taken care of the common case first. Larger branch constants

June 14, 2016Machine Language and Pointers11 J-type format  Finally, the jump instruction uses the J-type instruction format.  The jump instruction contains a word address, not an offset —Remember that each MIPS instruction is one word long, and word addresses must be divisible by four. —So instead of saying “jump to address 4000,” it’s enough to just say “jump to instruction 1000.” —A 26-bit address field lets you jump to any address from 0 to your MP solutions had better be smaller than 256MB  For even longer jumps, the jump register, or jr, instruction can be used. jr$ra# Jump to 32-bit address in register $ra opaddress 6 bits26 bits

June 14, 2016Machine Language and Pointers12 Representing strings  A C-style string is represented by an array of bytes. —Elements are one-byte ASCII codes for each character. —A 0 value marks the end of the array. 33!49165A81Q97a113q 34”50266B82R98b114r 35#51367C83S99c115s 36$52468D84T100d116t 37%53569E85U101e117u 38&54670F86V102f118v 39’55771G87W103g119w 40(56872H88X104h120x 41)57973I89Y105I121y 42*58:74J90Z106j122z 43+59;75K91[107k123{ 44,60<76L92\108l124| 45-61=77M93]109m125} 46.62>78N94^110n126~ 47/63?79O95_111o127del

June 14, 2016Machine Language and Pointers13 Null-terminated Strings  For example, “Harry Potter” can be stored as a 13-byte array.  Since strings can vary in length, we put a 0, or null, at the end of the string. —This is called a null-terminated string  Computing string length —We’ll look at two ways HarryPotter\0

June 14, 2016Machine Language and Pointers14 Array Indexing Implementation of strlen int strlen(char *string) { int len = 0; while (string[len] != 0) { len ++; } return len; }

June 14, 2016Machine Language and Pointers15 Pointers & Pointer Arithmetic  Many programmers have a vague understanding of pointers —Looking at assembly code is useful for their comprehension. int strlen(char *string) { int len = 0; while (string[len] != 0) { len ++; } return len; } int strlen(char *string) { int len = 0; while (*string != 0) { string ++; len ++; } return len; }

June 14, 2016Machine Language and Pointers16 What is a Pointer?  A pointer is an address.  Two pointers that point to the same thing hold the same address  Dereferencing a pointer means loading from the pointer’s address  A pointer has a type; the type tells us what kind of load to do —Use load byte (lb) for char * —Use load half (lh) for short * —Use load word (lw) for int * —Use load single precision floating point (l.s) for float *  Pointer arithmetic is often used with pointers to arrays —Incrementing a pointer (i.e., ++) makes it point to the next element —The amount added to the point depends on the type of pointer pointer = pointer + sizeof(pointer’s type)  1 for char *, 4 for int *, 4 for float *, 8 for double *

June 14, 2016Machine Language and Pointers17 What is really going on here… int strlen(char *string) { int len = 0; while (*string != 0) { string ++; len ++; } return len; }

June 14, 2016Machine Language and Pointers18 Summary  Machine language is the binary representation of instructions: —The format in which the machine actually executes them  MIPS machine language is designed to simplify processor implementation —Fixed length instructions —3 instruction encodings: R-type, I-type, and J-type —Common operations fit in 1 instruction Uncommon (e.g., long immediates) require more than one  Pointers are just addresses!! —“Pointees” are locations in memory  Pointer arithmetic updates the address held by the pointer —“string ++” points to the next element in an array —Pointers are typed so address is incremented by sizeof(pointee)