This is a Assembly project, details are in the pdf, and it need to be run with qtspim.
11/28/2019 Project 3: Rollin with Stack pointers https://canvas.csun.edu/courses/63658/assignments/478893 1/6 Project 3: Rollin with Stack pointers Due Saturday by 11:59pm Points 10 Submitting a file upload File Types asm, s, and a Submit Assignment Task For your third project, you will be tasked with writing a procedure that solves this formula given a user input for the missing paramater (n). Unlike the previous project, you are required to use procedures and recursion here. Notice that the equation has the same format as factorial. So let us consider the recursive implementation of factorial in C. When solving a problem recursively you are usually given two approaches. The bad way which is what you probably have learned in your introductory class and tail call recursion. Derpy recursion: int factorial(int n) { return n<0 -1="" :="" n="=" 0="" 1="" :="" n="" *="" factorial(n="" -="" 1);="" }="" tail="" call="" recursion:="" int="" fac_aux(int="" n,="" int="" acc)="" {="" return="" n="">0>< 1="" acc="" :="" fac_aux(n="" -="" 1,="" acc="" *="" n);="" }="" int="" factorial(int="" n)="" {="" return="" fac_aux(n,="" 1);="" }="" as="" we="" have="" discussed="" before,="" the="" tail="" call="" recursive="" function="" can="" be="" optimized="" by="" the="" compiler="" to="" run="" in="" a="" loop="" instead="" of="" being="" recursive.="" however,="" even="" if="" we="" are="" not="" optimizing="" our="" code,="" it="" is="" still="" much="" easier="" to="" visualize="" tail="" call="" recursion="" instead="" of="" regular="" recursion,="" since="" it="" has="" the="" structure="" of="" a="" loop.="" approach:="" 11/28/2019="" project="" 3:="" rollin="" with="" stack="" pointers="" https://canvas.csun.edu/courses/63658/assignments/478893="" 2/6="" first,="" you="" want="" to="" plan="" ahead.="" convert="" the="" equation="" into="" a="" c="" function.="" the="" one="" thing="" to="" keep="" an="" eye="" on="" is="" that="" division="" requires="" precision,="" therefore="" you="" will="" have="" to="" either="" use="" double="" or="" float="" for="" the="" answer="" double="" myfunction(int="" n)="" {="" return="" n="=" 1="" 5.0="" :="" ((4.0/n)="" +="" n*n*n)="" *="" myfunction(n="" -="" 1);="" }="" or="" alternatively="" double="" myfunc_aux(int="" n,="" double="" acc)="" {="" return="" n="">< 1="" acc="" :="" myfunc_aux(n="" -="" 1,="" acc="" *="" ((4.0/n)="" +="" n*n*n));="" }="" double="" myfunction(int="" n)="" {="" return="" myfunc_aux(n,="" 5.0);="" }="" like="" project="" 2,="" your="" first="" step="" should="" be="" to="" break="" up="" the="" c="" function="" into="" modular="" components,="" translate="" them="" into="" assembly="" instructions="" and="" then="" reconstruct="" them="" in="" either="" mips="" or="" arm="" mips="" unlike="" before="" you="" will="" have="" to="" actually="" follow="" the="" register="" convention="" function="" calls="" in="" mips="" $0="" $zero="" hard-wired="" to="" 0="" $1="" $at="" reserved="" for="" pseudo-instructions="" $2="" -="" $3="" $v0,="" $v1="" return="" values="" from="" functions="" $4="" -="" $7="" $a0="" -="" $a3="" arguments="" to="" functions="" -="" not="" preserved="" by="" subprograms="" $8="" -="" $15="" $t0="" -="" $t7="" temporary="" data,="" not="" preserved="" by="" subprograms="" $16="" -="" $23="" $s0="" -="" $s7="" saved="" registers,="" preserved="" by="" subprograms="" $24="" -="" $25="" $t8="" -="" $t9="" more="" temporary="" registers,="" not="" preserved="" by="" subprograms="" $26="" -="" $27="" $k0="" -="" $k1="" reserved="" for="" kernel="" for="" the="" interrupt/trap="" handler.="" do="" not="" use!="" $28="" $gp="" global="" area="" pointer="" (base="" of="" global="" data="" segment)="" 11/28/2019="" project="" 3:="" rollin="" with="" stack="" pointers="" https://canvas.csun.edu/courses/63658/assignments/478893="" 3/6="" $29="" $sp="" stack="" pointer="" $30="" $fp="" frame="" pointer="" $31="" $ra="" return="" address="" $f0="" -="" $f3="" -="" floating="" point="" return="" values="" $f4="" -="" $f10="" -="" temporary="" registers,="" not="" preserved="" by="" subprograms="" $f12="" -="" $f14="" -="" first="" two="" arguments="" to="" subprograms,="" not="" preserved="" by="" subprograms="" $f16="" -="" $f18="" -="" more="" temporary="" registers,="" not="" preserved="" by="" subprograms="" $f20="" -="" $f31="" -="" saved="" registers,="" preserved="" by="" subprograms="" you="" will="" also="" have="" to="" use="" the="" fpu.="" in="" some="" cases,="" you="" will="" need="" to="" send="" numbers="" from="" and="" to="" the="" cpu="" and="" the="" fpu.="" fp="" instructions="" available="" in="" mips:="" add.s="" fd,="" fs,="" ft="" #fp="" add="" single="" cvt.s.w="" fd,="" fs="" #convert="" to="" single="" precision="" fp="" from="" integer="" cvt.w.s="" fd,="" fs="" #convert="" to="" integer="" from="" single="" precision="" fp="" div.s="" fd,="" fs,="" ft="" #fp="" divide="" single="" mfc1="" rd,="" fs="" #move="" from="" coprocessor="" 1="" (fp)="" mov.s="" fd,="" fs="" #move="" fp="" single="" precision="" fp="" mtc1="" rs,="" fd="" #move="" to="" coprocessor="" 1="" (fp)="" mul.s="" fd,="" fs,="" ft="" #fp="" multiply="" single="" sub.s="" fd,="" fs,="" ft="" #fp="" subtract="" single="" the="" structure="" of="" your="" function="" should="" have="" the="" following="" structure="" #="" comment="" information="" about="" the="" name="" of="" program="" and="" description="" of="" the="" function="" #="" double="" myfunction(int="" n)="" {="" #="" return="" n="=" 0="" 5.0="" :="" (((double)1/n)="" +="" 4*nn)="" *="" myfunction(n="" -="" 1);="" #="" }="" myrecfunc:="" #="" procedure="" entry="" addi="" $sp,="" $sp,="" -8="" #allocate="" space="" on="" the="" stack="" for:="" sw="" $a0,="" 0($sp)="" #argument="" sw="" $ra,="" 4($sp)="" #return="" address="" 11/28/2019="" project="" 3:="" rollin="" with="" stack="" pointers="" https://canvas.csun.edu/courses/63658/assignments/478893="" 4/6="" #="">< -8 if you have local variables you need to sstore # branch to either a recursivecall or the base condition #restore the registers, "recursive anchor" lw $ra, 4($sp) lw $a0, 0($sp) ## whichever you didn't branch to jr $ra #return to caller recursivecall: addi $a0, $a0, -1 # decrement n by 1 jal myrecfunc # procedure exit lw $ra, 4($sp) # restore the registers lw $a0, 0($sp) # restore the registers addi $sp, $sp, 8 # restore the stack pointer # calculate (((double)1/n) + 4*n*n) * myfunction(n - 1); jr $ra #return to caller # end of program, leave a blank line afterward if you wish, you can wrap a main function around your assembly instruction for testing purposes; # comment information about the name of program and description of the function # main() # { # int n; # printf("enter a value for n \n") # scanf("%d",&n); # printf("f(%d) = %lf",n,myfunction(n)); # return 0; # } # double myfunction(int n) { # return n == 0 ? 5.0 : (((double)1/n) + 4*n*n) * myfunction(n - 1); # } 11/28/2019 project 3: rollin with stack pointers https://canvas.csun.edu/courses/63658/assignments/478893 5/6 .data # variable declarations follow this line msg: .asciiz "enter a value for n \n" result1: .asciiz "f(" result2: .asciiz ") = " newline: .asciiz "\n" .text # instructions follow this line main: # indicates start of code (first instruction to execute) # prompt the user for the value of n li $v0, 4 la $a0, msg syscall # get user input li $v0, 5 syscall # store user input in a register add $t0, $0, $v0 li $v0, 4 la $a0, result1 syscall add $a0, $0, $t0 li $v0, 1 li $t0, 5 # $integer to print syscall # pass n as an argument to the recusive function jal myrecfunc #print result li $v0, 4 la $a0, result2 syscall #result is currently stored in #$v0 if integer, or $f0-$f3 if flaot/double mov.d $f12, $f0 # move contents of register $f4 to register $f12 to print li $v0, 3 syscall #end of main li $v0, 10 syscall arm: arm should be the same as the mips instruction except you will be using the vfp coprocessor (http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0133c/index.html) for floating point http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0133c/index.html 11/28/2019 project 3: rollin with stack pointers https://canvas.csun.edu/courses/63658/assignments/478893 6/6 arithmetics. -8="" if="" you="" have="" local="" variables="" you="" need="" to="" sstore="" #="" branch="" to="" either="" a="" recursivecall="" or="" the="" base="" condition="" #restore="" the="" registers,="" "recursive="" anchor"="" lw="" $ra,="" 4($sp)="" lw="" $a0,="" 0($sp)="" ##="" whichever="" you="" didn't="" branch="" to="" jr="" $ra="" #return="" to="" caller="" recursivecall:="" addi="" $a0,="" $a0,="" -1="" #="" decrement="" n="" by="" 1="" jal="" myrecfunc="" #="" procedure="" exit="" lw="" $ra,="" 4($sp)="" #="" restore="" the="" registers="" lw="" $a0,="" 0($sp)="" #="" restore="" the="" registers="" addi="" $sp,="" $sp,="" 8="" #="" restore="" the="" stack="" pointer="" #="" calculate="" (((double)1/n)="" +="" 4*n*n)="" *="" myfunction(n="" -="" 1);="" jr="" $ra="" #return="" to="" caller="" #="" end="" of="" program,="" leave="" a="" blank="" line="" afterward="" if="" you="" wish,="" you="" can="" wrap="" a="" main="" function="" around="" your="" assembly="" instruction="" for="" testing="" purposes;="" #="" comment="" information="" about="" the="" name="" of="" program="" and="" description="" of="" the="" function="" #="" main()="" #="" {="" #="" int="" n;="" #="" printf("enter="" a="" value="" for="" n="" \n")="" #="" scanf("%d",&n);="" #="" printf("f(%d)="%lf",n,myFunction(n));" #="" return="" 0;="" #="" }="" #="" double="" myfunction(int="" n)="" {="" #="" return="" n="=" 0="" 5.0="" :="" (((double)1/n)="" +="" 4*n*n)="" *="" myfunction(n="" -="" 1);="" #="" }="" 11/28/2019="" project="" 3:="" rollin="" with="" stack="" pointers="" https://canvas.csun.edu/courses/63658/assignments/478893="" 5/6="" .data="" #="" variable="" declarations="" follow="" this="" line="" msg:="" .asciiz="" "enter="" a="" value="" for="" n="" \n"="" result1:="" .asciiz="" "f("="" result2:="" .asciiz="" ")=" newline: .asciiz " \n"="" .text="" #="" instructions="" follow="" this="" line="" main:="" #="" indicates="" start="" of="" code="" (first="" instruction="" to="" execute)="" #="" prompt="" the="" user="" for="" the="" value="" of="" n="" li="" $v0,="" 4="" la="" $a0,="" msg="" syscall="" #="" get="" user="" input="" li="" $v0,="" 5="" syscall="" #="" store="" user="" input="" in="" a="" register="" add="" $t0,="" $0,="" $v0="" li="" $v0,="" 4="" la="" $a0,="" result1="" syscall="" add="" $a0,="" $0,="" $t0="" li="" $v0,="" 1="" li="" $t0,="" 5="" #="" $integer="" to="" print="" syscall="" #="" pass="" n="" as="" an="" argument="" to="" the="" recusive="" function="" jal="" myrecfunc="" #print="" result="" li="" $v0,="" 4="" la="" $a0,="" result2="" syscall="" #result="" is="" currently="" stored="" in="" #$v0="" if="" integer,="" or="" $f0-$f3="" if="" flaot/double="" mov.d="" $f12,="" $f0="" #="" move="" contents="" of="" register="" $f4="" to="" register="" $f12="" to="" print="" li="" $v0,="" 3="" syscall="" #end="" of="" main="" li="" $v0,="" 10="" syscall="" arm:="" arm="" should="" be="" the="" same="" as="" the="" mips="" instruction="" except="" you="" will="" be="" using="" the="" vfp="" coprocessor="" (http://infocenter.arm.com/help/index.jsp?topic="/com.arm.doc.dai0133c/index.html)" for="" floating="" point="" http://infocenter.arm.com/help/index.jsp?topic="/com.arm.doc.dai0133c/index.html" 11/28/2019="" project="" 3:="" rollin="" with="" stack="" pointers="" https://canvas.csun.edu/courses/63658/assignments/478893="" 6/6=""> -8 if you have local variables you need to sstore # branch to either a recursivecall or the base condition #restore the registers, "recursive anchor" lw $ra, 4($sp) lw $a0, 0($sp) ## whichever you didn't branch to jr $ra #return to caller recursivecall: addi $a0, $a0, -1 # decrement n by 1 jal myrecfunc # procedure exit lw $ra, 4($sp) # restore the registers lw $a0, 0($sp) # restore the registers addi $sp, $sp, 8 # restore the stack pointer # calculate (((double)1/n) + 4*n*n) * myfunction(n - 1); jr $ra #return to caller # end of program, leave a blank line afterward if you wish, you can wrap a main function around your assembly instruction for testing purposes; # comment information about the name of program and description of the function # main() # { # int n; # printf("enter a value for n \n") # scanf("%d",&n); # printf("f(%d) = %lf",n,myfunction(n)); # return 0; # } # double myfunction(int n) { # return n == 0 ? 5.0 : (((double)1/n) + 4*n*n) * myfunction(n - 1); # } 11/28/2019 project 3: rollin with stack pointers https://canvas.csun.edu/courses/63658/assignments/478893 5/6 .data # variable declarations follow this line msg: .asciiz "enter a value for n \n" result1: .asciiz "f(" result2: .asciiz ") = " newline: .asciiz "\n" .text # instructions follow this line main: # indicates start of code (first instruction to execute) # prompt the user for the value of n li $v0, 4 la $a0, msg syscall # get user input li $v0, 5 syscall # store user input in a register add $t0, $0, $v0 li $v0, 4 la $a0, result1 syscall add $a0, $0, $t0 li $v0, 1 li $t0, 5 # $integer to print syscall # pass n as an argument to the recusive function jal myrecfunc #print result li $v0, 4 la $a0, result2 syscall #result is currently stored in #$v0 if integer, or $f0-$f3 if flaot/double mov.d $f12, $f0 # move contents of register $f4 to register $f12 to print li $v0, 3 syscall #end of main li $v0, 10 syscall arm: arm should be the same as the mips instruction except you will be using the vfp coprocessor (http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0133c/index.html) for floating point http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0133c/index.html 11/28/2019 project 3: rollin with stack pointers https://canvas.csun.edu/courses/63658/assignments/478893 6/6 arithmetics.>