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.

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
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
The University of Adelaide, School of Computer Science
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
SPIM and MIPS programming
Chapter 2 Instructions: Language of the Computer
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
The University of Adelaide, School of Computer Science
CS61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
©UCB CPSC 161 Lecture 5 Prof. L.N. Bhuyan
MIPS Instruction Set Advantages
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
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; }
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 +
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization Rabie A. Ramadan Lecture 3.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Computer Organization Instructions Language of The Computer (MIPS) 2.
March 21, 2016© Craig ZIlles (adapted from slides by Howard Huang) 1 What does this code do? label:sub$a0, $a0, 1 bne$a0, $zero, label Arf.
June 14, 2016Machine Language and Pointers1 What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; }
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Instruction Set Architectures Continued. Expanding Opcodes & Instructions.
What does this code do? label: sub $a0, $a0, 1 bne $a0, $zero, label
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
MIPS Instruction Set Advantages
CS2100 Computer Organisation
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Pick up the handout on your way in!!
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MPIS Instructions Functionalities of instructions Instruction format
MIPS Instruction Encoding
The University of Adelaide, School of Computer Science
MIPS Instruction Encoding
Instruction Set Architectures Continued
The University of Adelaide, School of Computer Science
Computer Instructions
3.
COMS 361 Computer Organization
COMS 361 Computer Organization
Lecture 6: Assembly Programs
9/27: Lecture Topics Memory Data transfer instructions
Conditional Branching (beq)
Presentation transcript:

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  Array Indexing vs. Pointers —In particular pointer arithmetic —String representation  (tentative) 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 Slides adapted from Josep Torrellas, Craig Zilles, and Howard Huang

2  We can use branch instructions to translate if-then statements into MIPS assembly code. v0 = a0;move $v0 $a0 if (v0 < 0)bge $v0, $0, Label v0 = -v0;sub $v0, 0, $v0 v1 = v0 + v0; Label: add $v1, $v0, $v0  Sometimes it’s easier to invert the original condition. — In this case, we changed “continue if v0 = 0 ”. — This saves a few instructions in the resulting assembly code. Translating an if-then statement

3 Translating an if-then-else statements  If there is an else clause, it is the target of the conditional branch — And the then clause needs a jump over the else clause // increase the magnitude of v0 by one if (v0 < 0)bge$v0, $0, E v0 --;sub$v0, $v0, 1 jL else v0 ++;E:add$v0, $v0, 1 v1 = v0;L:move$v1, $v0 — Drawing the control-flow graph can help you out.

4 Control-flow graphs  It can be useful to draw control-flow graphs when writing loops and conditionals in assembly: // Find the absolute value of *a0 v0 = *a0; if (v0 < 0) v0 = -v0; v1 = v0 + v0; // Sum the elements of a0 v0 = 0; t0 = 0; while (t0 < 5) { v0 = v0 + a0[t0]; t0++; }

5  It can be useful to draw control-flow graphs when writing loops and conditionals in assembly: // Find the absolute value of *a0 v0 = *a0; if (v0 < 0) v0 = -v0; v1 = v0 + v0; // Sum the elements of a0 v0 = 0; t0 = 0; while (t0 < 5) { v0 = v0 + a0[t0]; t0++; } Control-flow graphs

6 What does this code do? label:sub$a0, $a0, 1 bne$a0, $zero, label

7 Loops Loop: j Loop # goto Loop for (i = 0; i < 4; i++) { // stuff } add $t0, $zero, $zero # i is initialized to 0, $t0 = 0 Loop: // stuff addi $t0, $t0, 1 # i ++ slti $t1, $t0, 4 # $t1 = 1 if i < 4 bne $t1, $zero, Loop # go to Loop if i < 4

8 Case/Switch Statement  Many high-level languages support multi-way branches, e.g. switch (two_bits) { case 0: break; case 1: /* fall through */ case 2:count ++; break; case 3: count += 2; break; }  We could just translate the code to if, thens, and elses: if ((two_bits == 1) || (two_bits == 2)) { count ++; } else if (two_bits == 3) { count += 2; }  This isn’t very efficient if there are many, many cases.

9 Case/Switch Statement switch (two_bits) { case 0: break; case 1: /* fall through */ case 2:count ++; break; case 3: count += 2; break; }  Alternatively, we can: 1. Create an array of jump targets 2. Load the entry indexed by the variable two_bits 3. Jump to that address using the jump register, or jr, instruction

10 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

11 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

12 int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; } What does this C code do?

13 Array Indexing Implementation of strlen int strlen(char *string) { int len = 0; while (string[len] != 0) { len ++; } return len; }

14 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; }

15 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 *

16 What is really going on here… int strlen(char *string) { int len = 0; while (*string != 0) { string ++; len ++; } return len; }

17 Pointers Summary  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) 17