Today s lecture. Translating an if-then statement

Similar documents
Quick Review. lw $t0, 4($a0) Registers x Memory. $a0 is simply another name for register 4 $t0 is another name for register (green sheet)

ECE 30 Introduction to Computer Engineering

Lecture 5: Procedure Calls

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

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

UCB CS61C : Machine Structures

ECE260: Fundamentals of Computer Engineering

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Today. Putting it all together

EE 361 University of Hawaii Fall

Lecture 7: Examples, MARS, Arithmetic

Lecture 6: Assembly Programs

COE608: Computer Organization and Architecture

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Course Administration

Chapter 2: Instructions:

Computer Architecture

MIPS Memory Access Instructions

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

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

CS61C : Machine Structures

CS3350B Computer Architecture MIPS Instruction Representation

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

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

Chapter 3. Instructions:

Topic Notes: MIPS Instruction Set Architecture

MIPS (SPIM) Assembler Syntax

CS 230 Practice Final Exam & Actual Take-home Question. Part I: Assembly and Machine Languages (22 pts)

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

Course Administration

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

We will study the MIPS assembly language as an exemplar of the concept.

ECE260: Fundamentals of Computer Engineering

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

CS61C : Machine Structures

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

The MIPS Instruction Set Architecture

UCB CS61C : Machine Structures

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Chapter 2A Instructions: Language of the Computer

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

Computer Organization MIPS ISA

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

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

MIPS Instruction Set Architecture (2)

EE108B Lecture 2 MIPS Assembly Language I. Christos Kozyrakis Stanford University

UCB CS61C : Machine Structures

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CSCE 5610: Computer Architecture

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

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

Control Instructions

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Lecture 4: MIPS Instruction Set

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

ECE232: Hardware Organization and Design

CoE - ECE 0142 Computer Organization. Instructions: Language of the Computer

CS3350B Computer Architecture MIPS Introduction

Chapter 2. Instructions:

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

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

Instructions: Language of the Computer

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

COMPUTER ORGANIZATION AND DESIGN

Run time environment of a MIPS program

Q1: /30 Q2: /25 Q3: /45. Total: /100

CS/COE1541: Introduction to Computer Architecture

MIPS function continued

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

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Instruction Set Architecture. "Speaking with the computer"

Compilers and Interpreters

CS 61c: Great Ideas in Computer Architecture

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First)

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

Machine Instructions - II. Hwansoo Han

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

CS 61c: Great Ideas in Computer Architecture

Control MIPS unconditional branch instruction (i.e., jump)

Communicating with People (2.8)

CS3350B Computer Architecture

Chapter 2. Instruction Set Architecture (ISA)

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

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

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

CS3350B Computer Architecture Winter 2015

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 22 September Your Name (please print clearly) Signed.

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

Mips Code Examples Peter Rounce

Transcription:

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 Slides adapted from Josep Torrellas, Craig Zilles, and Howard Huang 1 Translating an if-then statement! 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 to skip if v0 >= 0. This saves a few instructions in the resulting assembly code. 2

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 j L else v0 ++; E: add $v0, $v0, 1 v1 = v0; L: move $v1, $v0 Drawing the control-flow graph can help you out. 3 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++; 4

What does this code do? label: sub $a0, $a0, 1 bne $a0, $zero, label 5 Loops Loop: j Loop!! # goto Loop for (i = 0; i < 4; i++) {! // stuff Loop: add $t0, $zero, $zero # i is initialized to 0, $t0 = 0 // 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 6

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. 7 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 8

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. 32 space 48 0 64 @ 80 P 96 ` 112 p 33! 49 1 65 A 81 Q 97 a 113 q 34 50 2 66 B 82 R 98 b 114 r 35 # 51 3 67 C 83 S 99 c 115 s 36 $ 52 4 68 D 84 T 100 d 116 t 37 % 53 5 69 E 85 U 101 e 117 u 38 & 54 6 70 F 86 V 102 f 118 v 39 55 7 71 G 87 W 103 g 119 w 40 ( 56 8 72 H 88 X 104 h 120 x 41 ) 57 9 73 I 89 Y 105 I 121 y 42 * 58 : 74 J 90 Z 106 j 122 z 43 + 59 ; 75 K 91 [ 107 k 123 { 44, 60 < 76 L 92 \ 108 l 124 45-61 = 77 M 93 ] 109 m 125 46. 62 > 78 N 94 ^ 110 n 126 ~ 47 / 63? 79 O 95 _ 111 o 127 del 9 Null-terminated Strings! For example, Harry Potter can be stored as a 13-byte array. 72 97 114 114 121 32 80 111 116 116 101 114 0 H a r r y P o t t e r \0! 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. 10

What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; return L; 11 Array Indexing Implementation of strlen int strlen(char *string) { int len = 0; while (string[len]!= 0) { len ++; return len; 12

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; 13 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 * 14

What is really going on here int strlen(char *string) { int len = 0; while (*string!= 0) { string ++; len ++; return len; 15 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) 16