Proj2 Here are the format specifications you must implement You might add some others if you have time on your hands We suggest you not do 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 result Don 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 sprintf turns the characters that would be printed by a corresponding printf into a string Your function must accept any number of arguments all passed on the stack as described below The first argument is the address of a character array into which your procedure will put its results The second argument is a format string in which each occurrence of a percent sign indicates where one of the subsequent arguments is to be substituted and how it is to be formatted The remaining arguments are values that are to be converted to printable character form according to the format instructions sprintf returns the number of characters in its output string not including the null at the end int sprintf char outbuf char format Write a MAL implementation of a string formatting routine inspired by the C function sprintf Project description The project can be done with a partner Put names logins section and TA information at the top of sprintf s You should only submit sprintf s The project is due on Sept 22 nd 11 59 PM Administrative details CS61c Patterson Procedure foo has two integer arguments The space for those arguments is allocated on the stack as part of the caller s stack frame In other words main not foo must allocate the space The arguments go at the bottom of main s stack frame That is main will use 0 sp to hold the argument x and 4 sp to hold the argument y The first argument is always at the top of the stack you have to be consistent about this so that foo 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 foo int foo int x int y int a b a y int main int c d foo 3 4 The procedure calling convention we ve been using up to now uses 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 all arguments are passed on the stack not in registers at all Each argument gets one word of stack space Suppose we are trying to write in MIPS assembler a program like this Background Fall 2000 addi sw lw sw addi jr sp sp 12 ra t0 16 sp get argument y see below t0 4 sp store as a sp sp 12 three wds ra a b ra 8 sp save ra Proj6 P S The real MIPS argument passing convention is a combination of the two we ve used Stack space is allocated for all the arguments but the first four arguments are passed in registers anyway their stack space is unused For this project although the arguments are passed on the stack the return value should still be in v0 Your sprintf procedure should work with the main function in spf main s Register convention of t registers and s registers still hold The a registers can be considered temporary registers in this project If you don t follow the convention then points will be deducted Miscellaneous requirements This instruction is the key to understanding the stack method of argument passing Procedure foo is referring to a word of stack memory that s beyond the boundary of its own stack frame Its own frame includes only the three words 0 sp 4 sp and 8 sp It thereby refers to the stack frame of its caller This is legal only to the extent that the caller s stack frame contains foo s arguments foo doesn t know what s where on the rest of its caller s stack frame it doesn t even know which procedure called it foo addi sp sp 20 jr ra CS61c Patterson Spring 99
View Full Document
Unlocking...