DOC PREVIEW
Berkeley COMPSCI 61C - CS 61C Project

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61c (Patterson) Proj2 Fall 2000Administrative detailsThe project can be done with a partner. Put names, logins, sectionand TA information at the top of sprintf.s. You should only submitsprintf.s. The project is due on Sept. 22nd, 11:59 PM. Project descriptionWrite a MAL implementation of a string-formatting routineinspired by the C function sprintf:int sprintf (char *outbuf, char *format, ...)sprintf turns the characters that would be printed by acorresponding printf into a string. Your function must accept any numberof arguments, all passed on the stack as described below. The firstargument is the address of a character array into which your procedure willput its results. The second argument is a format string in which eachoccurrence of a percent sign ("%") indicates where one of the subsequentarguments is to be substituted and how it is to be formatted. The remainingarguments are values that are to be converted to printable character formaccording to the format instructions. sprintf returns the number ofcharacters in its output string (not including the null at the end).Here are the format specifications you must implement. Youmight add some others if you have time on your hands. We suggest you notdo any floating-point, because it is tedious:%d convert integer to decimal%x convert integer to hexadecimal%c include one character argument in result%s include string of characters in result%% include a percent sign in resultDon't implement width or precision modifiers (e.g., %6d).Copy all the files from ~cs61c/proj2. Only modify sprintf.s.To run this project, you need to load two files in the correct order. Run[x]spim and load spf-main.s. Then load sprintf.s and now you can run it.BackgroundThe procedure-calling convention we've been using up to nowuses four registers ($a0-$a3) for passing arguments down to procedures.What if there are more than four arguments? That approach won't work.For the sprintf project you will use an alternative convention, in which allarguments are passed on the stack, not in registers at all. Each argumentgets one word of stack space. Suppose we are trying to write in MIPSassembler a program like this:int foo (int x, int y) { int a, b; ... a = y; ...}int main () { int c, d; ... foo (3, 4); ...}Procedure foo has two integer arguments. The space for thosearguments is allocated on the stack as part of the caller's stack frame. Inother words, main, not foo, must allocate the space. The arguments go atthe bottom of main's stack frame. That is, main will use 0($sp) to hold theargument x, and 4($sp) to hold the argument y. (The first argument isalways at the top of the stack-you have to be consistent about this so thatfoo knows which argument is which.)main: addi $sp, $sp, -20 # five wds: $ra, c, d, arg x, arg y sw $ra, 20($sp) # save $ra ... addi $t0, $0, 3 # first argument value is 3 sw $t0, 0($sp) # save on stack addi $t0, $0, 4 # second argument value is 4 sw $t0, 4($sp) # save on stack jal fooCS61c (Patterson) Proj6 Spring 99 ... addi $sp, $sp, 20 jr $rafoo: addi $sp, $sp, -12 # three wds: $ra, a, b sw $ra, 8($sp) # save $ra ... lw $t0, 16($sp) # get argument y *** (see below) sw $t0, 4($sp) # store as a ... addi $sp, $sp, 12 jr $ra*** This instruction is the key to understanding the stack method ofargument passing. Procedure foo is referring to a word of stack memorythat's beyond the boundary of its own stack frame. (Its own frame includesonly the three words 0($sp), 4($sp), and 8($sp).) It thereby refers to thestack frame of its caller. This is legal only to the extent that the caller'sstack frame contains foo's arguments! (foo doesn't know what's where onthe rest of its caller's stack frame; it doesn't even know which procedurecalled it.)Miscellaneous requirementsFor this project, although the arguments are passed on the stack,the return value should still be in $v0. Your sprintf procedure shouldwork with the main function in spf-main.s. Register convention of ‘t’registers and ‘s’ registers still hold The ‘a’ registers can be consideredtemporary registers in this project. . If you don’t follow the conventionthen points will be deducted.P.S. The real MIPS argument passing convention is a combination of thetwo we've used. Stack space is allocated for all the arguments, but the firstfour arguments are passed in registers anyway; their stack space is


View Full Document

Berkeley COMPSCI 61C - CS 61C Project

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download CS 61C Project
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 61C Project and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 61C Project 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?