Instruction Set Architectures (4)

Similar documents
COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

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

MIPS Assembly (Functions)

Functions in MIPS. Functions in MIPS 1

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

Today. Putting it all together

MIPS Assembly (FuncDons)

CSE Lecture In Class Example Handout

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

EE 361 University of Hawaii Fall

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

ECE331: Hardware Organization and Design

ECE232: Hardware Organization and Design

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

Compiling Code, Procedures and Stacks

COMP2611: Computer Organization MIPS function and recursion

Functions and Procedures

MIPS Assembly (FuncDons)

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

Procedures and Stacks

Lecture 5: Procedure Calls

CS153: Compilers Lecture 8: Compiling Calls

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

Course Administration

Implementing Procedure Calls

Common Problems on Homework

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

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

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

MIPS Procedure Calls - Review

Computer Systems and Networks

CS 316: Procedure Calls/Pipelining

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.

ECE260: Fundamentals of Computer Engineering

MIPS function continued

Lab 4 : MIPS Function Calls

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

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 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

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

MIPS Procedure Calls. Lecture 6 CS301

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

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

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

MIPS Functions and the Runtime Stack

EE 109 Unit 15 Subroutines and Stacks

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

CSE Lecture In Class Example Handout

CA Compiler Construction

Topic 3-a. Calling Convention 2/29/2008 1

Storage in Programs. largest. address. address

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.

Lecture 5: Procedure Calls

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

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

CS64 Week 5 Lecture 1. Kyle Dewey

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

Shift and Rotate Instructions

MIPS Functions and Instruction Formats

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

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

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

Control Instructions

Chapter 2A Instructions: Language of the Computer

CS 61c: Great Ideas in Computer Architecture

Lecture 6: Assembly Programs

SPIM Procedure Calls

Compiling Techniques

Compilers and computer architecture: A realistic compiler to MIPS

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

Anne Bracy CS 3410 Computer Science Cornell University

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

ECE 30 Introduction to Computer Engineering

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Instructions: Assembly Language

MIPS%Assembly% E155%

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

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

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Lecture 7: Procedures and Program Execution Preview

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

Computer Architecture

Procedure Call and Return Procedure call

6.004 Tutorial Problems L3 Procedures and Stacks

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

4.2: MIPS function calls

Lec 10: Assembler. Announcements

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

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

LAB C Translating Utility Classes

The Activation Record (AR)

Code Generation. Lecture 12

Anne Bracy CS 3410 Computer Science Cornell University

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

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University

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

Transcription:

Computer Architecture Week 06 Instruction Set Architectures (4) College of Information Science and Engineering Ritsumeikan University

subroutines functions, procedures remember the next instruction s address call (jump to the start of) the subroutine perform some computation return control to the remembered address in MIPS the instruction that calls a subroutine is jal, jump and link the address of the next instruction is placed in register $ra (the return address) the program counter is loaded with the address of the subroutine s first instruction the instruction that returns from a subroutine is jr, jump to register the value in a register is moved to the program counter e.g., register $ra execution continues from that address 2

doubling a number in Python def double(n): return n + n print double(21) subroutines doubling a number in MIPS machine language address of next instruction (move) saved in register $ra main: li $a0, 21 jal double move $a0, $v0 li $v0, 1 syscall li $v0, 10 syscall # double the number in a0 # return the result in v0 double: add $v0, $a0, $a0 jr $ra $ra moved to program counter, execution returns to the move instruction following the call main calls the subroutine double main is the caller, and double is the callee 3

only one level of subroutine can be called subroutines li $a0, 20 jal add1dbl # $ra = address of move, jump to add1dbl move $a0, $v0... add1dbl: jal add1 # $ra = address of next instruction jal double # $ra = address of next instruction jr $ra # infinite loop! add1: add $a0, $a0, 1 jr $ra calling add 1dbl saves a return address in $ra the call to add 1 immediately destroys the value saved in $ra the call to add1 will work correctly, but returning from add 1dbl is now impossible 4

subroutines to fix this problem, each function can allocate some memory to store its own local variables save $ra in that local memory perform some computation (including calling more functions) restore $ra from the value saved in local memory deallocate the memory return to the caller where to get some memory to store local variables? 5

subroutines every process (i.e., running program) has a stack for storing temporary values in last-in first-out order which is just what we need the stack grows downward pushing things onto the stack extends it towards lower memory addresses 6

subroutines the register $sp contains the address of the top of the stack to allocate memory on the stack, subtract the amount you need from $sp higher addresses end of stack allocated allocated allocated allocated lower addresses unallocated $sp points to the most recent data pushed on the stack 8 bytes of local memory unallocated subtract 8 $sp the program can now use 0($sp)... 7($sp) for local values to deallocate (free) the memory, add the same value back to $sp 7

at function entry and exit: subroutines func: addi $sp, $sp, -12 # space for 3 words sw $ra, 8($sp) # save return address...... # subroutine can use 0($sp) and 4($sp)... # to store local variables... lw $ra, 8($sp) # restore return address addi $sp, $sp, 12 # deallocate 3 words jr $ra # return $sp + 8 $sp + 4 $sp + 0 saved $ra local variable local variable saved $ra local variable local variable saved $ra local variable local variable function call function call $sp def factorial(n): if n < 2: return 1 return n * factorial(n-1) factorial: addi $sp, $sp, -8 # allocate two words sw $ra, 4($sp) # save return address addi $v0, $a0, -1 bgtz $v0, ok # n > 1 li $v0, 1 # return 1 j return ok: sw $a0, 0($sp) # save n addi $a0, $a0, -1 # n-1 jal factorial # v0 = factorial(n-1) lw $a0, 0($sp) # restore a0 mul $v0, $a0, $v0 # v0 = n*fact(n-1) return: lw $ra, 4($sp) # restore ret addr addi $sp, $sp, 8 # deallocate jr $ra # return 8

register conventions registers $s0... $s7 are callee-saved if you use them in a subroutine, you must save them in the stack and restore their original values before returning i.e., you (the callee) saves them during function calls the other registers ($an, $tn, $vn) are caller-saved their values can be destroyed during function calls (and your code should assume they always will be) if you need their values preserved during function call, save them before calling the function and restore them after the call i.e., you (the caller) saves them during function calls 9

practice 10

glossary allocate reserve some memory for a specific use. call a generic name for an instruction that calls a subroutine, e.g., jal in MIPS machine code. callee a generic name for an instruction that calls a subroutine, e.g., jal in MIPS machine code. callee-saved a register that must be preserved by the subroutine during its execution. callee the subroutine that is being called. caller the program or routine that calls a subroutine. caller-saved a register that can be destroyed by a subroutine, and must be preserved by the caller if necessary. deallocate release some memory that was earlier allocated, freeing it for reuse. function the high-level language idea of a subroutine that returns a value. procedure the high-level language idea of a subroutine that does not return any value. stack a last-in first-out data structure on which values can be pushed, and later popped (or pulled) to recover those same values in the opposite order. 11

subroutine a sequence of instructions that can be called out-of-line in a program, performing some task before returning to the instruction after the call. 12