Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Similar documents
Computer Architecture

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Chapter 2A Instructions: Language of the Computer

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

Control Instructions

Lecture 5: Procedure Calls

CENG3420 Lecture 03 Review

ECE232: Hardware Organization and Design

ECE260: Fundamentals of Computer Engineering

Computer Organization MIPS ISA

Chapter 2: Instructions:

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

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

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

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

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

Course Administration

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

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

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Chapter 3. Instructions:

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

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

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

COMPUTER ORGANIZATION AND DESIGN

CS 61c: Great Ideas in Computer Architecture

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Lecture 4: MIPS Instruction Set

MIPS%Assembly% E155%

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

CSE Lecture In Class Example Handout

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

CS3350B Computer Architecture MIPS Introduction

Chapter 2. Instructions:

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

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

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

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Lecture 5: Procedure Calls

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

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

CSE Lecture In Class Example Handout

EE 361 University of Hawaii Fall

Chapter 2. Instructions: Language of the Computer

Instruction Set Architecture

Instructions: Assembly Language

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

CS222: MIPS Instruction Set

CENG3420 L03: Instruction Set Architecture

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

Compiling Code, Procedures and Stacks

Instructions: Language of the Computer

CS3350B Computer Architecture

CS/COE1541: Introduction to Computer Architecture

Functions in MIPS. Functions in MIPS 1

Run-time Environment

Course Administration

CA Compiler Construction

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

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

ECE 30 Introduction to Computer Engineering

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

ECE260: Fundamentals of Computer Engineering

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

Thomas Polzer Institut für Technische Informatik

Instruction Set Architectures (4)

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

UCB CS61C : Machine Structures

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

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

Lecture 7: Procedures

MIPS Functions and Instruction Formats

Computer Architecture. The Language of the Machine

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Lecture 5. Announcements: Today: Finish up functions in MIPS

Lecture 7: Procedures and Program Execution Preview

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

MIPS Functions and the Runtime Stack

MIPS Instruction Set Architecture (2)

CS61C : Machine Structures

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Procedure Call and Return Procedure call

More C functions and Big Picture [MIPSc Notes]

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

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

Reduced Instruction Set Computer (RISC)

COE608: Computer Organization and Architecture

Levels of Programming. Registers

Transcription:

Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25

Comparisons Set on less than (slt) compares its source registers and sets its destination register to 1 if src1 < src2 and to 0 otherwise (branch-if-less-than) Syntax: slt dest, src1, src2 Set on less than immediate (slti) perform the same comparison, but its second source operation is an immediate value Syntax: slti dest, src1, immed Combined with beq and bne these instructions can implement all possible relational operators 26

Relational Branches (1) Branch if less than slt $t0, src1, src2 bne $zero, $t0, label Branch if greater than or equal slt $t0, src1, src2 beq $zero, $t0, label 27

Relational Branches (2) Branch if greater than slt $t0, src2, src1 bne $zero, $t0, label Branch if less than or equal slt $t0, src2, src1 beq $zero, $t0, label 28

Case/Switch Statement * Can be implemented like a chain of if-thenelse statements Using a jump address table is faster Must be able to jump to an address loaded from memory Jump register (jr) gives us that ability Syntax: jr src Instruction: jr $t1 #go to address in $t1 29

Compiling a Case (Switch) Statement Memory switch (k) { case 0: h=i+j; break; /*k=0*/ case 1: h=i+h; break; /*k=1*/ case 2: h=i-j; break; /*k=2*/ Assuming three sequential words in memory starting at the address in $t4 have the addresses of the labels L0, L1, and L2 and k is in $s2 $t4 add $t1, $s2, $s2 #$t1 = 2*k add $t1, $t1, $t1 #$t1 = 4*k add $t1, $t1, $t4 #$t1 = addr of JumpT[k] lw $t0, 0($t1) #$t0 = JumpT[k] jr $t0 #jump based on $t0 L0: add $s3, $s0, $s1 #k=0 so h=i+j j Exit L1: add $s3, $s0, $s3 #k=1 so h=i+h j Exit L2: sub $s3, $s0, $s1 #k=2 so h=i-j Exit:... L2 L1 L0 30

MIPS Organization src1 addr 5 src2 addr 5 dst addr 5 write data 32 Processor Register File 32 registers ($zero - $ra) 32 bits 32 32 src1 data src2 data read/write addr 32 Memory 1 1100 2 30 words Exec br offset 32 32 PC 32 Add 32 4 Fetch 32 PC = PC+4 Decode 32 32 Add 32 ALU 32 read data write data 32 32 4 5 6 7 0 1 2 3 byte address (big Endian) 32 bits 0 1100 0 1000 0 0100 0 0000 word address (binary) 31

Variable Index into Array Previous method for accessing array elements will only work if index is constant If index is a variable we must compute the address in code Formula: address + (index width) Since width is usually a power of two, we can use a left shift to perform the multiplication 32

Structures Like an array, a structure (struct) is made up of several smaller data elements stored contiguously in memory However the elements do not have to all be the same type The compiler will construct a table mapping each structure member to an offset 33

Bit Fields Structure members can be bit fields In this case multiple members are packed into a single byte or word To retrieve the value of a bit field, AND the word with a mask and then shift right To set the value of a bit field use a left shift and an OR 34

Procedures * A function (or procedure) is a sequence of instructions that can be used to perform a task When a function is called control transfers to the function; once the function completes its task it returns Functions may accept parameters (arguments) and/or return a value A function can also define local variables for its use which exist only until the function returns Functions can call other functions 35

Call and Return Call and return are nothing more than unconditional jumps The calling function (the caller) jumps to the beginning of the function The called function (the callee) jumps back to the point from which it was called (return address) Since a function can be called from any number of places the return address changes with each call 36

Jump and Link The jump and link (jal) instruction performs an unconditional jump just like j, but first saves the address of the next instruction in the return address register ($ra) jal label A function returns by jumping to the address stored in that register: jr $ra 37

Example (1) Calling a function: jal function1 Function: function1: add $t0, $t1, $t2 sub $t3, $t4, $t5 jr $ra 38

Calling Conventions We need a way to pass parameters to a function and get a return value from it A calling convention is a set of rules that define how parameters and return values are passed to/from functions as well as which registers a function can use Allows us to call functions compiled by different compilers and even languages 39

Parameters/Return Value Most MIPS software uses the same calling convention The first four parameters are placed in registers ($a0-$a3) additional parameters are placed on the stack Return values go in registers $v0-$v1 40

MIPS Registers Name Number Usage Saved on Call? $zero 0 Zero (hardware) N/A $at 1 reserved Assembler Temp No $v0-$v1 2-3 Return Value/Temporaries No $a0-$a3 4-7 Arguments No $t0-$t9 8-15, 24-25 Temporaries No $s0-$s7 16-23 Saved Values Yes $k0-$k1 26-27 Reserved for OS Kernel No $gp 28 Global Pointer Yes $sp 29 Stack Pointer Yes $fp 30 Frame Pointer Yes $ra 31 Return Address (hardware) Yes 41

Example (2) Function call or $a0, $s0, $zero jal function2 or $s0, $v0, $zero Function definition function2: add $v0, $a0, $a0 jr $ra 42

Register Use Callee-saved registers are owned by caller; if a function uses a callee-saved register it must save the old value and restore that value before it returns Caller-saved registers are owned by the callee; if a caller is using a caller-saved register, it must save the old value before calling the function and restore it after the function returns In MIPS, $s0-$s7 are callee-saved and $t0-$t9 are caller-saved 43

The Stack Data that a function needs to save in memory is pushed onto the stack Before returning, the function pops the data off of the stack In MIPS, the stack pointer ($sp) contains the address of the last word pushed onto the stack The stack grows downward from higher addresses to lower ones 44

Spilling Registers What if the callee needs more registers? What if the procedure is recursive? high addr uses a stack a last-in-first-out queue in memory for passing additional values or saving (recursive) return address(es) One of the general registers, $sp, is used to address the stack (which grows from high address to low address) add data onto the stack push top of stack $sp $sp = $sp 4 data on stack at new $sp remove data from the stack pop low addr data from stack at $sp $sp = $sp + 4 45

Accessing the Stack Push a word onto the stack addi $sp, $sp, -4 sw src, 0($sp) Pop a word off of the stack lw dest, 0($sp) addi $sp, $sp, 4 Typically we batch multiple pushes and pops together 46

Accessing the Stack (cont'd) Push three words onto the stack addi $sp, $sp, -12 sw $t0, 8($sp) sw $t1, 4($sp) sw $s0, 0($sp) Pop three words off of the stack lw $s0, 0($sp) lw $t1, 4($sp) lw $t0, 8($sp) addi $sp, $sp, 12 47

Stack Illustration High address $sp $sp Contents of register $t1 Contents of register $t0 $sp Contents of register $s0 Low address a. b. c. 48

Data on the Stack Data a function puts on the stack makes up its stack frame or activation record If there are more than four parameters, the extra parameters must be pushed onto the stack Non-leaf functions must save the value of the return address register ($ra) 49

Data on the Stack (cont'd) Any registers that must be saved are stored on the stack Also local variables that won't fit into registers (either because there are no registers left or the data is too big) Commonly a frame pointer ($fp) is used in addition to the stack pointer, since the stack pointer could change during the function 50

Stack Frame High address $fp $fp $sp $sp $fp Saved argument registers (if any) Saved return address Saved saved registers (if any) Local arrays and structures (if any) $sp Low address a. b. c. 51

Where Data is Stored The static data area holds global variables Fixed size In MIPS, the global pointer ($gp) contains the address of this area The stack holds local variables and functionrelated information Dynamic and managed automatically by the compiler The heap holds data allocated through new or malloc Dynamic and managed by the programmer 52

1000 0000 hex Text Program Memory $sp 7fff ffff hex Stack Dynamic data $gp 1000 8000 hex Static data pc 0040 0000 hex 0 Reserved 53

MIPS Registers Name Number Usage Saved on Call? $zero 0 Zero N/A $at 1 Assembler Temporary No $v0-$v1 2-3 Return Value/Temporaries No $a0-$a3 4-7 Arguments No $t0-$t9 8-15, 24-25 Temporaries No $s0-$s7 16-23 Saved Yes $k0-$k1 26-27 Reserved for OS Kernel No $gp 28 Global Pointer Yes $sp 29 Stack Pointer Yes $fp 30 Frame Pointer Yes $ra 31 Return Address Yes 54