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

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

Lecture 5: Procedure Calls

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

ECE260: Fundamentals of Computer Engineering

MIPS function continued

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

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

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

Instruction Set Architectures (4)

Functions in MIPS. Functions in MIPS 1

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

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

Control Instructions

ECE232: Hardware Organization and Design

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

Lecture 7: MIPS Functions Part 2. Nested Function Calls. Lecture 7: Character and String Operations. SPIM Syscalls. Recursive Functions

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

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

COMP2611: Computer Organization MIPS function and recursion

Lecture 5: Procedure Calls

CSE Lecture In Class Example Handout

Today. Putting it all together

Sorting Algorithms. + Analysis of the Sorting Algorithms

Common Problems on Homework

Lab 4 : MIPS Function Calls

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

EE 361 University of Hawaii Fall

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

Lecture 6: Assembly Programs

Compiling Techniques

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

MIPS Assembly (Functions)

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

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

MIPS Functions and Instruction Formats

Course Administration

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

CS 316: Procedure Calls/Pipelining

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

ECE331: Hardware Organization and Design

CS64 Week 5 Lecture 1. Kyle Dewey

Implementing Procedure Calls

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

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

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

Chapter 2A Instructions: Language of the Computer

CENG3420 Lecture 03 Review

Thomas Polzer Institut für Technische Informatik

Architecture II. Computer Systems Laboratory Sungkyunkwan University

MIPS Functions and the Runtime Stack

Functions and Procedures

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

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

Compiling Code, Procedures and Stacks

MIPS Procedure Calls. Lecture 6 CS301

COE608: Computer Organization and Architecture

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

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

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

Machine Instructions - II. Hwansoo Han

More C functions and Big Picture [MIPSc Notes]

ECE 30 Introduction to Computer Engineering

ECE 30 Introduction to Computer Engineering

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

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.

CS3350B Computer Architecture MIPS Procedures and Compilation

COMPUTER ORGANIZATION AND DESIGN

Lecture 7: Procedures and Program Execution Preview

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

Procedure Call and Return Procedure call

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

CS61C : Machine Structures

Implement a C language function that computes the factorial of a number, recursively, according to the following prototype:

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

MIPS Instruction Set Architecture (2)

CS 110 Computer Architecture MIPS Instruction Formats

Instruction Set Architecture

CSE Lecture In Class Example Handout

MIPS Assembly (FuncDons)

CS 61c: Great Ideas in Computer Architecture

Lecture 7: Procedures

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

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

6.004 Tutorial Problems L3 Procedures and Stacks

Procedures and Stacks

Lab 4 Prelab: MIPS Function Calls

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

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

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

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

MIPS Assembly Language Programming

MIPS Assembly Language Programming

CS61C : Machine Structures

DATA STRUCTURES AND ALGORITHMS

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

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

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

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Storage in Programs. largest. address. address

Transcription:

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions Wen Zong Department of Computer Science and Engineering The Chinese University of Hong Kong wzong@cse.cuhk.edu.hk

Overview Review of function calls Save register to stack Recursive function call Appendix

Save register to stack I A subroutine fun a can also call another subroutine fun b. When fun a calls fun b using jal fun b, $ra is overwritten to the address returning to fun a. $ra overwritten example fun_a: ins 0 ins 1 jal fun_b # $ra will store addess of ins 2 ins 2 jr $ra # hope to return to fun_a s caller fun_b: ins 3 jr $ra # return to fun_a $ra needs to be saved to stack, and becomes like this:

Save register to stack II save $ra fun_a: addi $sp, -4 # allocate space in stack sw 0($sp), $ra # save $ra value ins 0 ins 1 jal fun_b # $ra will store addess of ins 2 ins 2 lw $ra, 0($sp) # restore $ra value add $sp, 4 # free stack space jr $ra # return to fun_a s caller fun_b: ins 3 jr $ra # return to fun_a

Save register to stack III Similarly we can save other registers to stack by allocating more spaces in stack. Register are divided to saved register group $s0 - $s7 and temporary register group $t0 - $t7. By convention, caller expects callee to save the saved register group but does not expect temporary register to be saved.

Recursion Recursion is a function call to itself which is a special case of mentioned nested function call. It usually works in a divide and conquer manner. The same with nested function call, in recursive functions, register contents that is useful after a subroutine call should be saved to stack since they may be modified by subroutines.

Recurion example I Factorial can be computed recursively fact(n) = n fact(n 1). When the sub-problem f (n 1) is solved, we can multiply it by n to get the result of current problem f (n). When writting the assembly code we should save the register that contians this n which is $a0 in this example. Factorials

Recurion example II fact: addi $sp, $sp, -8 #adjust stack pointer sw $ra, 4($sp) #save return address sw $a0, 0($sp) #save argument n slti $t0, $a0, 1 #test for n < 1 beq $t0, $zero, L1 #if n >=1, go to L1 addi $v0, $zero, 1 #else return 1 in $v0 addi $sp, $sp, 8 #adjust stack pointer jr $ra #return to caller L1: addi $a0, $a0, -1 #n >= 1, so decrement n jal fact #call fact with (n-1) #this is where fact returns bk_f: lw $a0, 0($sp) #restore argument n lw $ra, 4($sp) #restore return address addi $sp, $sp, 8 #adjust stack pointer mul $v0, $a0, $v0 #$v0 = n * fact(n-1) jr $ra

Stack behavior during fact() call I Suppose we call fact(3) in main function, the stack will grow like this after recursive function calls. ret add in main $a0 (=3) in fact(3) bk f $a0 (=2) in fact(2) bk f $a0 (=1) in fact(1) bk f $a0 (=0) in fact(0)

Stack behavior during fact() call II In fact(0), it won t invoke fact() anymore and sets $v0 = 1 and returns. The stack will shrink backwards to merge the solutions. When fact(0) returns, the stack looks like this, ret add in main $a0 (=3) bk f $a0 (=2) bk f $a0 (=1) at bk f, $v0 = 1 The program continues to execute code at label bk f, which mulitplies $a0 with $v0 and then return to caller.

Stack behavior during fact() call III After fact(1) returns to fact(2), the stack content is: ret add in main $a0 (=3) bk f $a0 (=2) at bk f, $v0 = 1 After fact(2) returns to fact(3), the stack content is: ret add in main $a0 (=3) at bk f, $v0 = 2 fact(3) will return to the its caller the main function and the result is stored in $v0 register.

Exercises Write a Quicksort function using MIPS assembly language. We provide a c++ version of the Quicksort function, and you need to translate it to MIPS assembly. The array is declared in the data segment with name array. The starting code looks like this:.data array:.word -1 22 8 35 5 4 11 2 1 78.text main: la $a0, array li $a1, 0 li $a2, 9 jal qsort li $v0, 10 syscall qsort:...

Overview Review of function calls Save register to stack Recursive function call Appendix

The ideal of Quicksort Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. The steps are: 1. Pick an element, called a pivot, from the array. 2. Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. 3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values. The base case of the recursion is arrays of size zero or one, which never need to be sorted.

C++ implementation of Quicksort I void quicksort(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; /* partition */ while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;

C++ implementation of Quicksort II }; } i++; j--; } /* recursion */ if (left < j) quicksort(arr, left, j); if (i < right) quicksort(arr, i, right);