Pages

Saturday, November 6, 2010

LabD1Client : Translate Java class into MIPS (2)



# =====================  Java Code  ====================

public class FractionClient
{
public static void main(String[] args)
{
Fraction a = new Fraction(3,8);
Fraction b = new Fraction(1,2);
a.add(b);

System.out.print(a.getNumerator());
System.out.print('/');
System.out.print(a.getDenominator());
}
 }


# =================== MIPS =======================
LabD1Client.s

.text
#--------------------
main:
sw $ra, 0($sp)
addi $sp, $sp, -4


# to translate Fraction a = new Fraction(3,8);
# step 1: new Fraction(3,8)  the instance of Fraction is created in the heap memory, allocating 8 bytes for 2 int type properties
# step 2: the reference a = ; save the start address of the instance in the registrer $s0  

# Fraction a = new Fraction(3,8);
addi $a0, $0, 3
addi $a1, $0, 8
jal Fraction
add $s0, $0, $v0 # a = 
# Fraction b = new Fraction(1,2);
addi $a0, $0, 1
addi $a1, $0, 2
jal Fraction
add $s1, $0, $v0 # b = 
# a.getNumerator();
#  getNumerator does not take any parameter, why do we need to pass $a0 = $v0?
#  Because many instance of Fraction is created, and we want to retrive the numerator value of instance "a" not instance "b"

add $a0, $0, $s0
jal getNumerator


# a.add(b)
#  two instance is needed, instance of "a" and instance of "b"
#  so pass the address of those 2 instance
#  why not just passing the int variable???????
add $a0, $0, $s0
add $a1, $0, $s1
jal adding

# a.setNumerator(6)
# a.setDenominator(7)
add $a0, $0, $s0
addi $a1, $0, 6
jal setNumerator
addi $a1, $0, 7
jal setDenominator
# --------- return --------------------
addi $sp, $sp, 4
lw $ra, 0($sp)
jr $ra



No comments:

Post a Comment