Computer Systems Architecture



Similar documents
More MIPS: Recursion. Computer Science 104 Lecture 9

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Typy danych. Data types: Literals:

Stack Allocation. Run-Time Data Structures. Static Structures

MIPS Assembly Code Layout

Instruction Set Architecture

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Introduction to MIPS Assembly Programming

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Chapter 7D The Java Virtual Machine

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Translating C code to MIPS

What Is Recursion? Recursion. Binary search example postponed to end of lecture

5 MIPS Assembly Language

A3 Computer Architecture

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Reduced Instruction Set Computer (RISC)

Assembly Language: Function Calls" Jennifer Rexford!

Computer Architectures

Intel 8086 architecture

Lab Work 2. MIPS assembly and introduction to PCSpim

Machine-Code Generation for Functions

Review: MIPS Addressing Modes/Instruction Formats

Assembly Language Programming

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Instruction Set Architecture (ISA) Design. Classification Categories

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Figure 1: Graphical example of a mergesort 1.

Recursive Fibonacci and the Stack Frame. the Fibonacci function. The Fibonacci function defines the Fibonacci sequence, using a recursive definition :

Fall 2006 CS/ECE 333 Lab 1 Programming in SRC Assembly

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

CSE 141 Introduction to Computer Architecture Summer Session I, Lecture 1 Introduction. Pramod V. Argade June 27, 2005

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

Instruction Set Architecture

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

An Overview of Stack Architecture and the PSC 1000 Microprocessor

Instruction Set Design

Compilers I - Chapter 4: Generating Better Code

X86-64 Architecture Guide

MIPS Assembler and Simulator

St S a t ck a ck nd Qu Q eue 1

A Tiny Guide to Programming in 32-bit x86 Assembly Language

DATA STRUCTURE - STACK

2) Write in detail the issues in the design of code generator.

CSC 2405: Computer Systems II

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

MICROPROCESSOR AND MICROCOMPUTER BASICS

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Where we are CS 4120 Introduction to Compilers Abstract Assembly Instruction selection mov e1 , e2 jmp e cmp e1 , e2 [jne je jgt ] l push e1 call e

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Lecture 27 C and Assembly

The stack and the stack pointer

HC12 Assembly Language Programming

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Chapter 4 Lecture 5 The Microarchitecture Level Integer JAVA Virtual Machine

EC 362 Problem Set #2

1 Classical Universal Computer 3

Chapter 5 Instructor's Manual

Virtual Machines as an Aid in Teaching Computer Concepts

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

The Operating System and the Kernel

THUMB Instruction Set

Semester Review. CSC 301, Fall 2015

Introduction. What is an Operating System?

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

1 The Java Virtual Machine

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

PROBLEMS (Cap. 4 - Istruzioni macchina)

Computer Organization and Components

Module 2 Stacks and Queues: Abstract Data Types

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

CS2210: Compiler Construction. Runtime Environment

Pseudo code Tutorial and Exercises Teacher s Version

What to do when I have a load/store instruction?

Extending the swsusp Hibernation Framework to ARM. Russell Dill

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

C Compiler Targeting the Java Virtual Machine

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

COS 318: Operating Systems

64-Bit NASM Notes. Invoking 64-Bit NASM

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

Chapter 5. Recursion. Data Structures and Algorithms in Java

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

An Assembly Language Programming Style Guide. by Ken Lambert. Naming Conventions. Use of Whitespace

x64 Cheat Sheet Fall 2015

Code Generation & Parameter Passing

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

Instruction Set Architecture (ISA)

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Code Refactoring and Defects

Common Data Structures

Z80 Instruction Set. Z80 Assembly Language

Language Specification & Compiler Construction

Transcription:

Computer Systems Architecture http://cs.nott.ac.uk/ txa/g51csa/ Thorsten Altenkirch and Liyang Hu School of Computer Science University of Nottingham Lecture 10: MIPS Procedure Calling Convention and Recursion

A procedure by any other name... A portion of code within larger program, typically called: procedures or subroutines in imperative languages like C methods in OO languages like Java and functions in functional languages such as Haskell Functions usually return a value; procedures don t Procedures are necessary to reduce duplication of code and enable re-use decompose complex programs into manageable parts Procedures can call other procedures; even themselves What happens when we call a procedure? Control hands over to the callee; the caller is suspended Callee performs requested task Callee returns control to the caller

An Example of Procedures in C int f(int x, int y) { return sqrt(x * x + y * y); } int main() { printf("f(5,12)=%d\n",f(5, 12)); }

Calling Procedures in MIPS Assembly jal label jump and link $ra := PC + 4; PC := [label] Calls procedure at address label jr src jump register PC := src Issuing j $ra is the assembly equivalent of return Register $ra contains the return address of the caller Arguments are passed in registers $a0 to $a3 Results are left in registers $v0 and $v1

First Attempt f: mult $a0, $a0, $a0 mult $a1, $a1, $a1 add $a0, $a0, $a1 jal sqrt jr $ra main: li $a0, 5 li $a1, 12 jal f move $a0, $v0 li $v0, 1 syscall jr $ra What s wrong with this? $ra modified by jal, so... j $ra jumps to wrong address Must save required registers Previous value of $ra f overwrites $a0 and $a1 What if we need > 4 arguments?

The Stack Not enough registers? Save the contents of some registers to memory The stack provides last-in, first out (LIFO) storage Register $sp points to the topmost word on the stack By convention, the stack grows downwards Placing words onto the stack is termed pushing Taking words off the stack is called popping

Calling Convention Caller Push any of $a0-3, $v0-1 and $t0-9 needed later Place arguments in $a0 to $a3, and stack if necessary Make the call using jal callee; result in $v0 and $v1 Pop saved registers and/or extra arguments off stack Callee Push any of $ra, $s0-$s9 that may be overwritten Perform desired task; place result in $v0 and $v1 Pop above registers off the stack Return to caller with jr $ra

Procedure Example f: addi $sp, $sp, -4 sw $ra, 0($sp) mult $a0, $a0, $a0 mult $a1, $a1, $a1 add $a0, $a0, $a1 jal sqrt lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra # allocate space on stack # push $ra onto stack # call sqrt # pop $ra off stack # deallocate space on stack

Calling Convention Summary Preserved by Callee Not Preserved Saved registers $s0-$s7 Temporary registers $t0-$t9 Stack pointer $sp Argument registers $a0-$a3 Return address $ra Return values $v0 and $v1 Stack at/above $sp Stack below $sp Items not preserved but needed later, caller must preserve Stack contents preserved by not writing at/above $sp Stack pointer saved by always popping what we pushed Leaf procedures are those which do not make further calls In such instances, we needn t explicitly save $ra The main label is just another procedure Ought to follow the same conventions

Local Variables We needn t preserve the stack below the initial $sp A convenient location for local storage Creating locals: subtract number of bytes from $sp Complex functions may do this many times Each time this changes the $sp-offset of previous locals! Assembly harder for humans and debuggers to read! Solution: save initial value of $sp in $fp Hence local variables always have the same $fp-offset Note callee must preserve previous value of $fp!

Example: Recursive Factorial # int fact(int n): return n <= 0? 1 : n * fact(n-1); fact: addi $sp, $sp, -8 # space for two words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # temporary variable to hold n li $v0, 1 ble $a0, $zero, fact_return addi $a0, $a0, -1 jal fact lw $a0, 0($sp) # retrieve original n mul $v0, $v0, $a0 # n * fact(n - 1) fact_return: lw $ra 4($sp) # restore $ra addi $sp, $sp, 8 # restore $sp jr $ra # back to caller

Example: Recursive Fibonacci # int fib(int n): return n < 2? n : fib(n-1) + fib(n-2) fib: addi $sp, $sp, -8 # room for $ra and one temporary sw $ra, 4($sp) # save $ra move $v0, $a0 # pre-load return value as n blt $a0, 2, fib_rt # if(n < 2) return n sw $a0, 0($sp) # save a copy of n addi $a0, $a0, -1 # n - 1 jal fib # fib(n - 1) lw $a0, 0($sp) # retrieve n sw $v0, 0($sp) # save result of fib(n - 1) addi $a0, $a0, -2 # n - 2 jal fib # fib(n - 2) lw $v1, 0($sp) # retrieve fib(n - 1) add $v0, $v0, $v1 # fib(n - 1) + fib(n - 2) fib_rt: lw $ra, 4($sp) # restore $ra addi $sp, $sp, 8 # restore $sp jr $ra # back to caller

Reading Read up on calling conventions in H&P: 2.7 (pp 79 86) Appendix A 6 (pp 22 33)