Computer Systems and Networks

Similar documents
MIPS Assembly (FuncDons)

MIPS Assembly (Functions)

MIPS Assembly (FuncDons)

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Course Administration

Instruction Set Architectures (4)

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

MIPS function continued

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

ECE 30 Introduction to Computer Engineering

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

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

CSE Lecture In Class Example Handout

COMP2611: Computer Organization MIPS function and recursion

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

Common Problems on Homework

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

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

Compiling Techniques

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

Code Genera*on for Control Flow Constructs

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

Computer Systems and Networks

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Lecture 5: Procedure Calls

CSE Lecture In Class Example Handout

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

Control Instructions

Lab 4 : MIPS Function Calls

Compiling Code, Procedures and Stacks

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

MIPS Procedure Calls. Lecture 6 CS301

Computer Systems and Networks

Functions in MIPS. Functions in MIPS 1

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

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

Course Administration

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

Lecture 6: Assembly Programs

CENG3420 Lecture 03 Review

ECE331: Hardware Organization and Design

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

MIPS%Assembly% E155%

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

CS64 Week 5 Lecture 1. Kyle Dewey

Assembler. Lecture 8 CS301

ECE232: Hardware Organization and Design

EE 361 University of Hawaii Fall

Chapter 2A Instructions: Language of the Computer

Today. Putting it all together

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

Computer Organization MIPS ISA

COE608: Computer Organization and Architecture

4.2: MIPS function calls

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

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

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

CS 316: Procedure Calls/Pipelining

MIPS Functions and the Runtime Stack

MIPS Procedure Calls - Review

Architecture II. Computer Systems Laboratory Sungkyunkwan University

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

MIPS Assembly Language Guide

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

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

Lectures 3-4: MIPS instructions

We can emit stack-machine-style code for expressions via recursion

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Review of Activation Frames. FP of caller Y X Return value A B C

QtSPIM and MARS : MIPS Simulators

Lecture 5: Procedure Calls

EE 109 Unit 15 Subroutines and Stacks

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

SPIM Instruction Set

Lecture 7: Examples, MARS, Arithmetic

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

CS 61c: Great Ideas in Computer Architecture

Computer Hardware Engineering

ECE260: Fundamentals of Computer Engineering

MIPS Memory Access Instructions

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

Shift and Rotate Instructions

Instruction Set Architecture

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

Procedures and Stacks

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

ECE 30 Introduction to Computer Engineering

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

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.

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)

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Introduction to Assembler Language Programming

Chapter 3. Instructions:

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Procedure Call and Return Procedure call

6.004 Tutorial Problems L3 Procedures and Stacks

Transcription:

LECTURE 16: MIPS (11 AND 12) Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) University of the Pacific

Deadline Lab 11 is open: DUE 1 st NOV 5 AM Lab 12 is open: DUE 8 th DEC 5 AM

You can do almost everything just using these Arithmetic Instructions add <destination register>, <register 1>, <register 2> sub <destination register>, <register 1>, <register 2> mul <destination register>, <register 1>, <register 2> addi <destination register>, <register 1>, value Branching Instructions beq <register 1>, <register 2>, label bgt <register 1>, <register 2>, label blt <register 1>, <register 2>, label ble <register 1>, <register 2>, label bge <register 1>, <register 2>, label Memory Instructions la <register>, memory lw/sw <register>, offset(base)

Problem 1: Everything unml now - - Convert this to assembly int array[]={5, 4, 3, 2, 1}; int main() { sum=0; for(int i=0;i<5;i++) { sum+=array[i]; } printf( \n The sum is: %d, sum); } Register Map: $s0: array addr. $s1: i $s2: sum

The Program Counter Instructions are stored in the memory one by one, following the code structure Each MIPS32 instruction occupies 4 bytes How does the processor know from where to fetch the next instruction? A special 32-bit register called Program Counter (PC) holds the address of the next instruction

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on PC Reverse engineer: Write a C code for this assembly 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 PC 8 addi $t1,$zero, 2 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 PC 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 PC 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> PC 24 li $v0, 10 28 syscall

Program ExecuMon Program Counter (PC) Instructions are stored in memory and each occupy 4 bytes. Instruc,on 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 12 bge $t0, $t1, <label to addr. 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 PC 28 syscall

FuncMons in MIPS

Problem 2: Write down what MIPS should provide you for the following How caller can pass arguments to the callee? How to call a function? How to return values? How to return from a function and resume operation?

What happens when funcmons are called 1. Program saves the context (registers) of calling function (caller) 2. Program saves the arguments in registers ($a0 - $a3) 3. Program calls the callee via jump-and-link instruction jal <function label> jal saves the address of the next instruction in $ra Program Counter (PC) jumps to the callee s location. Callee saves return values in regs. $v0-$v1 4. Callee returns via jump register instruction, jr <register name> #usually $ra jr sets PC to $ra. PC continues there onwards

FuncMon ExecuMon Instructions are stored in memory and each occupy 4 bytes. Instruc,on PC 4 addi $a0, $zero, 5 #argument 5 8 jal <function at 20> 12 li $v0, 10 16 syscall 20 func?on: 24 jr $ra 28 add $v0, $a0, $a0 #return value v0

FuncMon ExecuMon Instructions are stored in memory and each occupy 4 bytes. $ra=12 Instruc,on 4 addi $a0, $zero, 5 #argument 5 PC 8 jal <function at 20> 12 li $v0, 10 16 syscall 20 func?on: 24 jr $ra 28 add $v0, $a0, $a0 #return value v0

FuncMon ExecuMon Instructions are stored in memory and each occupy 4 bytes. $ra=12 Instruc,on 4 addi $a0, $zero, 5 #argument 5 8 jal <function at 20> 12 li $v0, 10 16 syscall PC 20 func?on: 24 jr $ra add $v0, $a0, $a0 #return value v0 28

FuncMon ExecuMon Instructions are stored in memory and each occupy 4 bytes. $ra=12 Instruc,on 4 addi $a0, $zero, 5 #argument 5 8 jal <function at 20> 12 li $v0, 10 16 syscall PC 20 func?on: 24 jr $ra add $v0, $a0, $a0 #return value v0 28

FuncMon ExecuMon Instructions are stored in memory and each occupy 4 bytes. $ra=12 Instruc,on 4 addi $a0, $zero, 5 #argument 5 8 jal <function at 20> PC 12 li $v0, 10 16 syscall 20 func?on: 24 jr $ra 28 add $v0, $a0, $a0 #return value v0

FuncMon ExecuMon Instructions are stored in memory and each occupy 4 bytes. $ra=12 Instruc,on 4 addi $a0, $zero, 5 #argument 5 8 jal <function at 20> 12 li $v0, 10 PC 16 syscall 20 func?on: 24 jr $ra 28 add $v0, $a0, $a0 #return value v0

More Jumps Jump and Link (side effect: $ra stores address of next instruc?on) jal <destination> Jump Register (des?na?on address is stored in <reg1> Use this to call a function! jr <reg1> Use this to return from a function!

Problem 3: Write Code #include <stdio.h> int function(int a); int main() { int x=5; int y; } y = function(x); printf( y:%d,y); return 0; Place arguments in $a0-$a3 Place return values in $v0-$v1 Return address saved automa?cally in $ra Ignore the stack for this example. (Thus, the func?on will destroy registers used by calling func?on) int function(int a) { return 3*a+5; }

What if Callee needs some of the registers ($s0 - $s9) to compute and these were already in use by the caller? Callee calls another function, overwriting the return address, $ra? Stack to the rescue!

How Stack Operates Stack is a Last In, First Out (LIFO) data structure $sp 20 Value 2 3 4 5 16 12 8 4

How Stack Operates Stack is a Last In, First Out (LIFO) data structure push 5 $sp 20 5 Value 2 3 4 16 12 8 4

How Stack Operates Stack is a Last In, First Out (LIFO) data structure push 4 20 5 Value 2 3 $sp 16 4 12 8 4

How Stack Operates Stack is a Last In, First Out (LIFO) data structure push 3 20 5 Value 2 16 4 $sp 12 3 8 4

How Stack Operates Stack is a Last In, First Out (LIFO) data structure push 2 20 5 Value 16 4 12 3 $sp 8 2 4

How Stack Operates Stack is a Last In, First Out (LIFO) data structure pop 2 20 5 Value 2 16 4 $sp 12 3 8 2 4

How Stack Operates Stack is a Last In, First Out (LIFO) data structure pop 3 20 5 Value 2 3 $sp 16 4 12 3 8 2 4

How Stack Operates Stack is a Last In, First Out (LIFO) data structure Value 2 3 4 pop 4 $sp 20 5 16 4 12 3 8 2 4

How Stack Operates Stack is a Last In, First Out (LIFO) data structure pop 5 $sp 20 5 Value 2 3 4 5 16 4 12 3 8 2 4

Problem 4: Using $sp, write the set of commands for push and pop

What a caller must do with the stack prior to funcmon call? Typically not much, unless: it wants to store temporary registers ($t0-$t9) or argument registers ($a0-$a3) onto the stack it wants to pass arguments via stack After return, it should pop the stack

What a callee must do with the stack prior to funcmon call? Push $s registers onto the stack Push $ra onto the stack Pop $ra from the stack Pop $s from the stack In Lab 11 folder, create a file called aggressive_context_saving.txt and write commands for aggressive push and pop

Caller and Callee MIPS pormon Caller <some code> : push $t0 jal callee pop $t0 : <some code> Callee addi $sp, $sp, -4 lw $s0, 0($sp) addi $sp, $sp, -4 lw $ra, 0($sp) <some code> sw $ra, 0($sp) addi $sp, $sp, 4 sw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

Caller and Callee MIPS pormon Caller <some code> : push $t0 jal callee pop $t0 : <some code> Callee <aggressively push regs.> <some code> : : <aggressively pop regs.> jr $ra

Problem 5: Convert this to MIPS int array[] = {2, 3, 4, 5, 6}; int main() { int num, position; scanf( %d,&num); position = search(array, num, 5); printf( \n The position is: %d,position); } int search(int *array, int num, int size) { int position =-1; for(int i=0;i<size;i++) if(array[i]==num) { position=i; break;} return position; } Register map: $s0:num $s1: position $a0: array addr. $a1: num $a2: size $v0: return val.

Group Discussion Read Lab 12 instructions carefully Draw flowchart of function calls, their arguments, and return values Discuss how would you implement in MIPS Thereafter, you should only consult the professor, TA, and tutoring for coding. Use help from peers for debugging, but do not look into each other s code

Happy Thanksgiving!