Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You need to write a main function that calls the recursive A function and passes arguments m in X0 and n in X1. You may

image text in transcribedimage text in transcribed

You need to write a main function that calls the recursive A function and passes arguments m in X0 and n in X1. You may have the function return its result in X0 or X2. This function calls itself recursively (except in the base case) and will be overwriting any registers which are not saved. You will have a couple of new areas to work on. First, how to implement and call functions. Next, how to properly use the stack. You will have no idea how deep the recursion will go, so there is no way for you to save values in main memory, as they would be overwritten. In these situations, we store values on the stack to save them. We refer to these as "stack frames", and each contains the local variables for a function. This will include the parameters passed in ( m and n in this case). Each time a function is called, it will need to save anything on the stack it wishes to be preserved across function calls. For this program, the only value that you really need to save is m (or m1 ). No values are assigned in the function, so it might be tempting to think that no values need to be saved at all, just pass them to next stage and return what they return. But notice that the third case has a nested recursive call. The call to "A(m,n-1)" occurs first, and its result will be passed as the second parameter for the outer call. Thus, the value "m-1" needs to be passed as the first parameter. In addition to this parameter, you will also need to save the return address (passed in X30 ). Recall that the BL (branch and link) opcode transfers control to an address and saves a copy of the program counter (the address of the NEXT instruction after the current one) in X30. When the function completes it should branch to the address in X30 (the RET instruction does this by default). Some of you have noted that X30 will be overwritten if a function calls another function (such as a recursive call). This will keep us from properly returning unless we can save/restore this value. Any value that must be preserved across a function call (like the two mentioned above) should be placed on the stack. The SP register contains the address of the first free byte on the stack. As values are placed on the stack, the stack pointer is decremented to make room for this value. The stack pointer needs to be incremented or decremented in increments of 16 (to maintain proper alignment of the stack). If you are saving a single value on the stack, you will decrement by 16 . If you are saving three values, you will decrement by 32. When you call a function it should save values on the stack before changing them or calling any other functions. These values should be restored from the stack to their original locations before the function returns. Here is some pseudo-code for your function: // X0 contains m //X1 contains n Function A: Check if m is zero. If yes, set the return register (X0 or X2) to n+1 and return. Decrement the stack pointer // saving values we need to preserve Save X0 and X30 on the stack. Check if n is zero. If yes, set X0 to m1 and set X1 to 1 . Call A. Else Set X1 to n1. Call A. // inner call A(m,n1) Set X1 to the return value ( X0 or X2 ). Restore X0 from the stack // original m Set X0 to m1 Call A. // outer call A(m1, returned-value) Restore X30 from the stack Return Input: A(2,2)=7 (Test on smaller values like A(1,2) first, but submit A(2,2) output) Submit (Required): a) A file containing your assembly code (.S file) b) A read-me file which indicates your use of registers c) A screenshot of the stack when main calls A d) A screenshot of the stack whenever A(1,0) is called e) A screenshot showing the final register values in main when finished

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

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

Explain all drawbacks of application procedure.

Answered: 1 week ago

Question

Explain the testing process of accounting 2?

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago