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

funcName: addi sp, sp,-128 # assume 128 bytes is enough sw ra,0(sp) sw fp,4(sp) mv fp, sp # set frame pointer to this space # now params can be at 8(fp),12(fp),16(fp),20(fp),24(fp), and 32(fp) # and locals can be at 36(fp) and so on, or right after the actual parameter count # ... # ... code for your function body here # ... mv sp, fp # for safety, restore sp from fp lw ra,0(sp) lw fp,4(sp) addi sp, sp,128 ret
please fix this code to generate the instructions in the above output
case AST_ASSIGNMENT:
fprintf(out,"\t#--assignment--
");
genCodeFromASTree(node->child[0],0,out); // child 1 is right hand side
if (node->varKind == V_GLOBAL){
fprintf(out,"\tsw\tt0,%s
", node->strval);
} else if (node->varKind == V_PARAM || node->varKind == V_LOCAL){
fprintf(out,"\tsw\tt0,%d(fp)
", node->ival *4+8);
} else if (node->varKind == V_GLARRAY){//child[0]){
fprintf(out,"\taddi\tsp, sp,-4
\tsw\tt0,0(sp)
"); // save RHS value onto stack
genCodeFromASTree(node->child[1],0,out); // generate code for index expression
fprintf(out,"\tla\tt1,%s
"// Load base address of the array into t1
"\tmul\tt2, t0,4
"// Multiply index (t0) by 4(size of int) into t2
"\tadd\tt1, t1, t2
"// Add offset to base address (t1= t1+ t2)
"\tlw\tt0,0(sp)
"// Pop RHS value from the 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;
case AST_FUNCTION:
fprintf(out,"%s:
\taddi\tsp, sp,-128# assume 128 bytes is enough
\tsw\tra,0(sp)
\tsw fp,4(sp)
\tmv\tfp, sp", node->strval);
ptr = node->child[0];
while (ptr !=NULL){
fprintf(out,"
\tsw\ta%d,%d(fp)
", ptr->ival, ptr->ival *4+8);
ptr =ptr->next;
}
genCodeFromASTree(node->child[2], hval, out); // function body
fprintf(out,"\tmv sp, fp # for safety, restore sp from fp
\tlw\tra,0(sp)
\tlw\tfp,4(sp)
\taddi\tp, sp,128
\tret
");
break;
case AST_VARREF:
if (node->varKind == V_GLOBAL){
// Load the global variable directly
fprintf(out,"\tlw\tt0,%s
", node->strval);
} else if (node->varKind == V_PARAM || node->varKind == V_LOCAL){
// Load the variable using its offset from the frame pointer
fprintf(out,"\tlw\tt0,%d(fp)
", node->ival *4+8);
} else if (node->varKind == V_GLARRAY){// child[0] is the index
genCodeFromASTree(node->child[0], hval, out); // Generate index expression code
fprintf(out,
"\tla\tt1,%s
"// Load base address of the array into t1
"\tmul\tt2, t0,4
"// Multiply index (t0) by 4(size of int) into t2
"\tadd\tt1, t1, t2
"// Add offset to base address (t1= t1+ t2)
"\tlw\tt0,0(t1)
",// Load the array element into t0
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!