Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part Two covers non-leaf functions - those that call another function. Once again, a MIPS stack frame should conform to the format discussed in-class and

Part Two covers non-leaf functions - those that call another function. Once again, a MIPS stack frame should conform to the format discussed in-class and in the online notes. Here is the full information on stack frame layout for non-leaf functions and for leaf functions that need to allocate a stack frame for local variables: on entry to any function, the stack pointer points to the home location of your first argument. The first four arguments are passed in the a-registers (and are not on the stack). Remaining arguments are on the stack at the corresponding offsets. on entry to the callee, the callee's stack frame is allocated. A stack frame is required for non-leaf functions, and for leaf functions that need room for temporary storage. A stack frame is created by subtracting a constant amount from $sp. It consists of (in order of decreasing address (from 'bottom' to 'top') ) of ? saved registers (including $ra). These must be stored on the stack in register number order. Thus looking 'up' the stack (towards higher addresses) the register numbers should increase, culminating in saving $ra (for non-leaf functions), which should be closest to the home location for your arguments (the end of the caller's stack frame). ? local variables take home assignment #3 Due on May 23, 2018 1 ? If your function is not a leaf, you must allocate room for the arguments to functions you call. The minimum amount of room allocated for arguments is 16 bytes, which is sufficient to 'home' the arguments passed in registers. More space must be allocated if you call a function with more than four arguments. Again, looking 'up' the stack a function you call should see room for its arguments in increasing argument order. After the creation of the callee's stack frame, 0($sp) accesses the end of the callee's stack frame. In a non-leaf, this points to the home location of the first argument you will pass to a function. The home location of your first argument is now at 0+N($sp), where N is the size of the stack frame you allocated. Items in the callee's stack frame are then accessed indirectly using the $sp register with a nonnegative offset. You should use the support functions contained in the file util.s in the public work area. See the handout entitled Support Functions on canvas note for week 7. Each of the files provided for this exercise set relies on the functions in util.s Thought exercise : itoax.s This function, which is part of util.s, translates an integer into its equivalent hexadecimal string. (This is called encoding the integer as a string.) Your job is to think about how to write this function, then to examine the code, which has been written. This function has the prototype int itoax(unsigned num, char *string); It translates the integer num to hexadecimal ASCII, storing the result in string. It assumes that string has at least 9 bytes of space (max 8 hex digits + null byte). The return value of itoax is 1 (true) unless string is NULL, as the conversion must succeed. For this exercise, think about the problem and map out an algorithm in pseudo code (or flow chart) you would use to do it. Remember, the problem is actually very simple. The integer you will receive is already a hexadecimal 32-bit number. You must simply convert it to the string counterpart, one digit at a time. Your function should ignore leading zeroes in the number. Thus the hexadecimal number 0x00000007 should be output as 7. (A program that uses this function would be responsible for prefixing 0x to the string result for clarity.) When you have thought about the problem, examine the code for itoax.s. Besides being in util.s, a copy of the .s file, the C source file, a driver program for it (in both C and assembler) are in the answers directory. You can run the driver program under Mars. It will use the copy of itoax() from util.s Please submit your algorithm on canvas. This program is very important to practice the procedure calling convention. This program will simply get a string from the user (using a buffer allocated on the stack) and make a copy of the string, placing the result in dynamic (heap) memory. It then displays the copy. I have written a stub for you with C code for you to translate that gets the original string. It then calls a function strdup() that you must provide, passing it the [ address of the ] original string. strdup() returns a pointer to the new string. take home assignment #3 Due on May 23, 2018 2 The prototype is char *strdup(const char *) In a nutshell, here is what strdup() will do: get the length of the string (using strlen()). The length returned by strlen does not include the nul-terminator. call malloc() (see the Support Functions) to get a pointer to a piece of memory large enough to hold the copy (you need enough room for a null-terminator too!) copy the original string to the newly allocated memory using strcpy() from util.s. To practice the procedure calling convention, your strdup() function must , in turn, call two other functions from util.s: strlen() and strcpy() I have written a stub for you to start with that has the C code that repeatedly asks the user to type in a string, calls strdup (which is an empty function in the stub) and outputs the result. It continues until the user clicks Cancel or types q as the only character in the string. It is strdup.s in the procedures directory. You will have to translate the C code to the MIPS and add your strdup() function. NOTE: strlen() and strcpy() are leaf functions and do not use many registers. You may not use this knowledge when you write strdup. Obey the calling convention strictly. Pretend you do not know what registers strlen() and strcpy() will use.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

More Books

Students also viewed these Databases questions

Question

How do we organise for international logistics?

Answered: 1 week ago

Question

What are the logistics implications of internationalisation?

Answered: 1 week ago