Princeton University
COS 217:  Introduction to Programming System

Precept 16:  SPARC Assembly Language Subroutines

Purpose

Help you learn how to create and call subroutines in SPARC assembly language programs

Reading

Paul, Chapter 7

Approach

Study many small C programs and corresponding hand-written assembly language programs

After studying each program, refer to summary sheets to reinforce and generalize the new material that the program illustrates

SPARC Subroutine Calling Conventions

Review the handout SPARC Subroutine Calling Conventions

Example:  sumsub

Illustrates the subroutine calling conventions

See sumsub.c and sumsub.S

What it does

Same as previous "sum..." programs

How it works

Uses a subroutine

The code:  sumsub.c

Defines two subroutines:  main and sumBetween

main calls sumBetween with 2 ints as actual parameters

sumBetween returns an int that is the sum of all ints between the 2 given integers

The code:  sumsub.S

(Not optimized -- for clarity and to illustrate stack manipulation)

SPARC "Leaf" Subroutine Calling Conventions

Leaf subroutine

Informally, a subroutine that does not call another

Review the handout SPARC "Leaf" Subroutine Calling Conventions

Why leaf subroutines?  

Efficiency:  avoids expensive "save" and "restore" instructions

Note:  Subroutine sumBetween could, and probably should, be a leaf subroutine...

Example:  sumsubleaf

Illustrates the leaf subroutine calling conventions

See sumsubleaf.S

What it does

How it works

Same as sumsub.S, except uses a leaf subroutine

The code

Same as sumsub.S, except sumBetween is a leaf subroutine

(Not optimized -- for clarity and to illustrate stack manipulation)

Example:  sumsub8

Artificial example

Illustrates passing more than 6 actual parameters

See sumsub8.c and sumsub8.S

Example:  fnptr

Artificial example

Illustrates how C function pointers are implemented in SPARC assembly language

Explicitly use "jmpl addr, rd" or "call ris" instruction to jump to an address that is contained in a register

See fnptr.c and fnptr.S

Copyright 2002 by Robert M. Dondero, Jr.