Question: myFunc: addi sp , sp , - 1 2 8 # assume 1 2 8 bytes is enough sw ra , 0 ( sp )

myFunc:
addi sp, sp,-128 # assume 128 bytes is enough
sw ra,0(sp)
sw fp,4(sp)
mv fp, sp
sw a0,8(fp)
sw a1,12(fp)
sw a2,16(fp)
la t0,.SC0
mv a0, t0
jal printStr
la t0,.SC1
mv a1, t0
jal printStr
#--assignment--
li t0,42
sw t0, x
mv sp, fp # for safety, restore sp from fp
lw ra,0(sp)
lw fp,4(sp)
addi sp, sp,128
ret
The above is my output, please can you fix the code to match the Description of Needed Functionality below
Short Description of Needed Functionality.
Parameters (up to six): See the info page, but in short, to use parameters we first store them from the argument registers onto the stack and then access them using a constant offset from the frame pointer (fp) register. Our symbol table record and our AST_VARREF node for each parameter must remember the parameter position (starting with 0 perhaps) in the ival field; then when we generate code we can use an offset based on ival that accesses the correct position on the stack. We should also set the varKind field to V_PARAM. (see the updated addSymbol() function; it has a new last parameter).
Local variables: Local variables are also stored on the stack, so we need to open up space for them, too. We should also use the AST_VARREF ival field to store a position number for them; it is most useful to just continue the parameter counter into the local variables, and give them position counts that continue from the parameters. This way they can be handled exactly the same in code generation. We should also set the varKind field to V_LOCAL.
# Function prologue addi sp, sp,-128 # Allocate 128 bytes on the stack sw ra,0(sp) # Save return address sw fp,4(sp) # Save old frame pointer mv fp, sp # Set new frame pointer # Parameters are at offsets from fp # Local variables start at offset 36(fp) # Function body code here... # Function epilogue mv sp, fp # Restore stack pointer from frame pointer lw ra,0(sp) # Restore return address lw fp,4(sp) # Restore old frame pointer addi sp, sp,128 # Deallocate stack space ret # Return from function # AST Assignment Handling case AST_ASSIGNMENT: fprintf(out,"\t# -- assignment --
"); genCodeFromASTree(node->child[0],0, out); // Generate code for RHS if (node->varKind == V_GLOBAL){ fprintf(out,"\tsw\tt0,%s
", node->strval); // Store in global variable } else if (node->varKind == V_PARAM || node->varKind == V_LOCAL){ fprintf(out,"\tsw\tt0,%d(fp)
", node->ival *4+8); // Store in local/param } else if (node->varKind == V_GLARRAY){// Handle global array assignment fprintf(out,"\taddi\tsp, sp,-4
""\tsw\tt0,0(sp)
"); // Save RHS value onto stack genCodeFromASTree(node->child[1],0, out); // Generate index expression code fprintf(out,"\tla\tt1,%s
"// Load base address of array into t1"\tmul\tt2, t0,4
"// Multiply index by size of int (4)"\tadd\tt1, t1, t2
"// Add offset to base address "\tlw\tt0,0(sp)
"// Pop RHS value from stack into t0"\taddi\tsp, sp,4
"// Restore stack pointer "\tsw\tt0,0(t1)
",// Store value into array element node->strval); } else { fprintf(out,"Unknown variable kind assignment
"); } break; # Function Handling in AST case AST_FUNCTION: fprintf(out,"%s:
" "addi\tsp, sp,-128
""# assume 128 bytes is enough
""sw\tra,0(sp)
""sw\tfp,4(sp)
""mv\tfp, sp
", node->strval); ptr = node->child[0]; while (ptr != NULL){ fprintf(out,"sw\ta%d,%d(fp)
", ptr->ival, ptr->ival *4+8); ptr = ptr->next; } genCodeFromASTree(node->child[2], hval, out); // Generate function body fprintf(out,"mv\tsp, fp
""# for safety restore sp from fp
""lw\tra ,0(sp)
""lw\tfp ,4(sp)
" "addi\tsp ,sp ,128
" "ret
"); break; # Variable Reference Handling in AST case AST_VARREF: if (node->varKind == V_GLOBAL){ fprintf(out,"lw\tt0,%s
", node->strval); } else if (node->varKind == V_PARAM || node->varKind == V_LOCAL){ fprintf(out,"lw\tt0,%d(fp)
", node->ival *4+8); } else if (node->varKind == V_GLARRAY){ genCodeFromASTree(node->child[0], hval, out); // Generate index expression code fprintf(out,"la\tt1,%s
" "mul\tt2,t0,4
" "add\tt1,t1,t2
""lw\tt0,0(t1)
", node->strval); } else { fprintf(out,"Unknown variable kind reference
"); } break;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!