Chapter 7: Subroutines Lecture notes to accompany the text book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2 nd edition,

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
Chapter 6 Data Structures
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
UEE072HM Linking HLL and ALP An example on ARM. Embedded and Real-Time Systems We will mainly look at embedded systems –Systems which have the computer.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
SPARC Architecture & Assembly Language
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
1 CSC 3210 Computer Organization and Programming Chapter 7 SUBROUTINES D.M. Rasanjalee Himali.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Natawut NupairojAssembly Language1 Memory and Stack.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 8 – Machine Instructions These are lecture notes to accompany the book.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois, Abinashi Dhungel.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Chapter 8 – Machine Instructions
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
ECE 3430 – Intro to Microcomputer Systems
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
143A: Principles of Operating Systems Lecture 4: Calling conventions
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Introduction to Compilers Tim Teitelbaum
Chapter 7 Subroutines Dr. A.P. Preethy
Chapter 9 :: Subroutines and Control Abstraction
Instructions - Type and Format
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
by Richard P. Paul, 2nd edition, 2000.
Systems Architecture I
Subroutines and the Stack
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Chapter 7: Subroutines Lecture notes to accompany the text book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2 nd edition, 2000 Abinashi Dhungel

Introduction Subroutines – makes it possible to repeat computation or repeat computation with different arguments. Open Subroutine : – handled by text editor / macro preprocessor – The code is inserted whenever required in the program – Efficient with no wasted instructions – Results in long code Closed Subroutine: – appears only once in the program, – a jump to the code is executed whenever it is needed and – return is made after the subroutine has been executed to the location after the jump instruction.

Introduction Subroutines – allows to debug code once and then to be sure that all future instantiations of the code will be correct. – provides for control of errors – basis for structured programming – specialized instruction Should not change state of machine (except condition codes) Should restore all the registers temporarily used.

Register Saving Historically, required pushing all registers to stack and popping out at the end of subroutine execution. register save mask to indicate the registers to save. SPARC provides register file with mapping registers which indicate active registers – Typically, 128 registers available – Programmer can access 32 at a time (8 global and 24 mapped registers) – save changes mapping to provide new set of registers and restore, restores mapping on subroutine return

Register Saving 32 registers are divided into four groups : in, local, out and global. global %g0 - %g7 are not mapped and are global to all subroutines in registers %i0 - %i7 are used to pass arguments local registers %l0 - %l7 are for subroutine’s local variables out registers %o0 - %o7 are used to pass arguments to subroutine that are called by current subroutine. with save instruction out registers become in registers and a new set of local and out registers is provided. Mapping pointer is changed by 16 registers

Register Saving Current register set is indicated by the current window pointer, “CWP” (bits 0-4 inside PSR). Last free register set is marked by the window invalid mask register, “WIM” Each register set has 16 registers. Number of register sets is implementation dependent

save decreases cwp by 1, restore increases it by 1

after further five subroutine calls without return an additional subroutine call (causes window_overflow) moves the 16 registers from window set 7 to the stack where the %sp of register window set 6 is pointing.

Register saving After save instruction %o6 (%sp) becomes %i6(%fp). save %sp, -96, %sp – subtracts 96 from the current stack pointer but saves the result in the new stack pointer, leaving the old stack pointer unchanged. – The old stack pointer becomes the new frame pointer restore instruction restores the register window set. – window_underflow occurs if cwp is moved to wim. – restores the registers from the stack. – restore is also an add instruction.

Subroutine Linkage To branch to a subroutine a ba instruction might be used. However, there is no way to return to the point where the subroutine was called. In SPARC,two instructions for linking to subroutine which save the address of calling instruction in %o7. return is to the address pointed by %o inside the subroutine, since o7 is mapped to i7, the return is made to %i7+8.

Subroutine Linkage Calling a subroutine – jmpl jmpl%src_register [+ constant/register] (points address), %dest_register (for saving address of jmpl). jmpl %o0, %o7 – call calllabel or %register (points address) Transfers control to that address, and stores the address of call into %o7. call %o0 is expanded as jmpl %o0, %o7 Return from subroutine – jmpl %i7 + 8, %g0 – ret is expanded as jmpl %i7+8, %g0 call subr nop … subr: save %sp, … %sp … ret restore

Arguments Placing arguments in stack, – time consuming since each argument must be stored before calling subroutine and should be moved back to register for any computation – flexible since any number of arguments can be passed, supports recursive calls In, SPARC first 6 arguments can be placed in the out registers. Only 6 out registers are available since %o6 is stack pointer and %o7 is used for storing calling address. After save instruction the arguments will be available to subroutine in %i0-%i5

Arguments – 64 bytes for saving 16 registers starting at %sp – 4 bytes for structure return pointer at %sp + 64, – 24 bytes for 6 arguments starting at %sp (saving convention when window_overflow) additional arguments may be placed on stack starting at %sp + 92 and can be accessed by the called subroutine at %fp + 92 subroutine_name: save %sp, -( args + local ) & -8, %sp – where local variables are accessed at %fp – local_var_offset

Return Values Subroutines with return values - functions value returned in register %o0 structure, the pointer can be passed on %sp+64

/** C program to add two complex numbers **/ struct complex { int re, im; }; struct complex complex_set(int re, int im) { struct complex c; c.re = re; c.im = im; return c; } struct complex complex_add(struct complex* c1, struct complex* c2) { struct complex c; c.re = c1->re + c2->re; c.im = c1->im + c2->im; return c; } main() { struct complex c, c1, c2; c1 = complex_set(1,2); c2 = complex_set(2,3); c = complex_add(&c1, &c2); }

/** struct complex { int re, im; }; **/ !structure fields re_offset = 0 im_offset = 4 size_str = 8 ! space for 3 structures used in main str1_offset = -size_str str2_offset = str1_offset - size_str str3_offset = str2_offset - size_str

/** struct complex complex_set(int re, int im) { struct complex c; c.re = re; c.im = im; return c; } **/.global complex_set complex_set: save %sp, -96, %sp ld [%fp + 64], %l0 st %i0, [%l0 + re_offset] st %i1, [%l0 + im_offset] ret restore

/** struct complex complex_add(struct complex* c1, struct complex* c2) { struct complex c; c.re = c1->re + c2->re; c.im = c1->im + c2->im; return c; } **/.global complex_add complex_add: save %sp, -96, %sp ld [%fp + 64], %l0 !add and store real part ld [%i0 + re_offset], %o0 ld [%i1 + re_offset], %o1 add %o0, %o1, %o0 st %o0, [%l0 + re_offset] !add and store im part ld [%i0 + im_offset], %o0 ld [%i1 + im_offset], %o1 add %o0, %o1, %o0 st %o0, [%l0 + im_offset] ret restore

.global main main: save %sp, (-92+str3_offset)&-8, %sp !initialize the first structure add %fp, str1_offset, %l0 st %l0, [%sp+64] mov 1, %o0 mov 2, %o1 call complex_set nop !initialize the second structure add %fp, str2_offset, %l1 st %l1, [%sp+64] mov 2, %o0 mov 3, %o1 call complex_set nop !add the complex numbers stored in the two structures add %fp, str3_offset, %l2 st %l2, [%sp+64] mov %l0, %o0 mov %l1, %o1 call complex_add nop mov 1, %g1 ta 0 /** main() { struct complex c, c1, c2; c1 = complex_set(1,2); c2 = complex_set(2,3); c = complex_add(&c1, &c2); } **/

Subroutines with more than 6 arguments arg7_offset = 92 arg8_offset = arg7_offset + 4 stack_size = arg8_offset + 4.global main main: save %sp, (-stack_size)&-8, %sp … mov 70, %l0 st %l0, [%sp + arg7_offset] mov 20, %l0 call foo st %l0, [%sp + arg8_offset] /** int foo(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { return a1+a2+a3+a4+a5+a6+a7+a8; } **/.global foo foo: save %sp, -96, %sp … !load 7th and 8th arguments ld [%fp+arg7_offset], %l0 add %l0, %i0, %i0 ld [%fp+arg8_offset], %l0 add %l0, %i0, %i0 ret restore May be stored in stack The calling subroutine must make enough space for the extra arguments

Leaf Subroutines.global foo foo: !does not need save !save %sp, -96, %sp … !load 7th and 8th arguments ld [%sp+arg7_offset], %o1 add %o1, %o0, %o0 ld [%fp+arg8_offset], %o1 !add %o1, %o0, %o0 retl add %o1, %o0, %o0 Subroutines that do not call any other subroutines Can be made efficient by working on same register sets of the parent subroutine. register use should be restricted to %o0 - %o5, %g0, %g1. Does not require restore or save Return to parent is to %o7 + 8 instead of %i7 + 8 retl is expanded as jmpl %o7 + 8, %g0

Pointers as arguments a_offset = -4; b_offset = -8;.global main main: save %sp, (-92+b_offset)&-8, %sp mov 5, %o0 st %o0, [%fp + a_offset]; mov 7, %o1 st %o1, [%fp + b_offset]; add %fp, a_offset, %o0 call swap add %fp, b_offset, %o1 mov 1, %g1 ta 0.global swap swap: ld [%o0], %o2 ld [%o1], %o3 st %o3, [%o0] retl st %o2, [%o1] swap(int *a, int *b) { int temp ; temp = *a; *a = *b; *b = temp; }