A recursive method to solve the assignment2 1. $v0 = EVALU($a0

advertisement
A recursive method to solve the assignment2
1. $v0 = EVALU($a0, $a1);
Description: We combine evaluating the expression and converting it to a postfix
notation in one recursive procedure EVALU.
Argument:
$a0: address of first character of the expression.
$a1: address of last character of the expression.
Return value: $v0 = value of expression
Details:
The definition of postfix notation,
(a) if e is constant then postfix notation e is e itself
(b) if e = e1 op e2, the postfix notation is e’1 e’2 op, where e’1 and e’2 are the postfix
notation for e1 and e2.
Pseudo-code:
(1)If (a0 == a1)
return $v0 = CTOI(character char in address a0)
Note: CTOI is a function that converts a char to an integer.
output char to postfix notation.
(2)else
parse e = e1 op e2 (you should implement an sub procedure to do this job)
recursive call EVALU to evaluate e1 and output e’1, assume $t0= the value of e 1.
recursive call EVALU to evaluate e2 and output e’2, assume $t1= the value of e 2.
return $v0 = $t1 op $t2
output op to postfix notation.
In order to output the postfix notation, a global register $t9 can be used to record the
current pointer. $t9 increments after output one character in postfix notation.
Note: Some register should be saved at the beginning of the procedure and restore them
just before return from this procedure.
(1) save $ra to stack
(2) if the value of a register is need after a recursive call, save it also.
Example:
((1-3)+5):
e1 = (1-3) , e2 = 5
2. [$v0, $v1] = FIND ($a0: address)
Description: FIND is auxiliary sub procedure. It finds the address of the op which is the
most outside operator.
Argument: a0: address of first character of the expression.
Return value: v0 is the address of the operator. v1 = 0 if the operator is ‘+’ and v1 = 1 if
the operator is ‘-’
Details:
Keep a counter which is initialized to 0.
‘(’
counter++
‘)’
counter—
Scanning the expression from left to right,
Example:
((1-3)+5): v0 = address of ‘+’, v1= 0;
(1-(3+5)): v0 = address of ‘-’, v1= 1.
Note: If the input is not a fully parenthesized expression, the return value is unexpected.
Download