|
- .text
- .globl main
- main:
- # intro - stack-frame
- addi $sp, $sp, -8 # main
- sw $ra, 4($sp)
- sw $fp, 0($sp)
- addi $fp, $sp, 0 # stack arguments (no)
-
- li $a0, 0xbf800000 # <-- test case goes here
- li $a1, 0x3f800000
- jal multFloat
-
- # epilogue - stack-frame
- lw $fp, 0($sp)
- lw $ra, 4($sp)
- addi $sp, $sp, 8
- jr $ra
-
- #
- # encodeFloat (sign, exp, mant)
- # Arguments
- # $a0: The sign
- # $a1: The exponent without the bias
- # $a2: The normalized mantissa 24bit, with the implied bit
- # Retuen
- # $v0: The floating point word
- encodeFloat:
- # intro - stack-frame
- addi $sp, $sp, -8 # encodeFloat
- sw $ra, 4($sp)
- sw $fp, 0($sp)
- addi $fp, $sp, 0 # stack arguments (no)
-
- sll $v0, $a0, 31 # v0 = sign << 31
-
- addi $a1, $a1, 127 # v0 |= (exp + offset) << 23
- sll $a1, $a1, 23
- or $v0, $v0, $a1
-
- li $t0, 0x7FFFFF # v0 |= (mant & 0x7FFFFF);
- and $a2, $a2, $t0
- or $v0, $v0, $a2
-
- # epilogue - stack-frame
- lw $fp, 0($sp)
- lw $ra, 4($sp)
- addi $sp, $sp, 8
- jr $ra
-
- #
- # decodeFloat(number)
- # arguments
- # $a0 : 32bit floating point
- # return
- # $v0 : sign unsigned int 0 or 1
- # $v1 : exponent the value of bits(30:24) - 127, signed integer
- # $a1 : mantissa with bits 1 followed by bits (22:0)
- decodeFloat:
- # intro - stack-frame
- addi $sp, $sp, -8 # decodeFloat
- sw $ra, 4($sp)
- sw $fp, 0($sp)
- addi $fp, $sp, 0 # stack arguments (no)
-
- srl $v0, $a0, 31 # sign[v0] = a0 >> 31;
-
- srl $t0, $a0, 23 # exp[v1] = ((a0 >> 23) & 0xFF) - 127;
- and $t0, $t0, 0xFF
- addi $v1, $t0, -127
-
- li $t0, 0x7FFFFF # Mantissa[a1] = (a0 & 0x7FFFFF) | 0x800000;
- and $a1, $a0, $t0
- li $t0, 0x800000
- or $a1, $a1, $t0
-
- # epilogue - stack-frame
- lw $fp, 0($sp)
- lw $ra, 4($sp)
- addi $sp, $sp, 8
- jr $ra # return
-
- #
- # multFloat(f1, f2)
- # arguments
- # $a0 : 32bit floating point 1
- # $a1 : 32bit floating point 2
- # return
- # $v0 : 32bit floating point multiplication result
- multFloat:
- # s0: sign and argument 2
- # s1: exp
- # s2: mantissa
- # intro - stack-frame
- addi $sp, $sp, -20 # multFloat
- sw $ra, 16($sp)
- sw $fp, 12($sp)
- sw $s2, 8($sp)
- sw $s1, 4($sp)
- sw $s0, 0($sp)
- addi $fp, $sp, 0 # stack arguments (no)
-
- move $s0, $a1 # save 2nd argument
- jal decodeFloat # decodeFloat(arg1) : [v0, v1, a1] <- [s, e, m]
- move $t0, $s0 # $s0 change variable to $t0
- move $s0, $v0 # load return values to variable regs
- move $s1, $v1
- move $s2, $a1
-
- move $a0, $t0 # load back argument 2
- jal decodeFloat # decodeFloat(arg2) : [v0, v1, a1] <- [s, e, m]
- xor $s0, $s0, $v0 # sign = s1 ^ s2
- add $s1, $s1, $v1 # exp = e1 + e2
-
- multu $s2, $a1 # mantissa = m1 * m2
- mfhi $t4 # t4: mantissa-hi
- mflo $t3 # t3: mantissa-low
- addi $s2, $zero, 0
-
- li $t9, 0x8000 # if (HI & (1<<15))
- and $t8, $t9, $t4
- beq $t8, $zero, _multf_l0
- # (true)not zero, 48-bits result, normalize
- sll $s2, $t4, 8 # mant = HI << 8
- srl $t9, $t3, 24 # mant |= LOW >> (32-8)
- or $s2, $s2, $t9
- addi $s1, $s1, 1 # ++exp, normalize
- li $t9, 0x800000 # if (ml & (1<<23)), rounding check
- and $t8, $t9, $t3
- beq $t8, $zero, _multf_l0_end
- addi $s2, $s2, 1 # ++mant, round-up
- j _multf_l0_end
- _multf_l0:
- # (false)zero, 47-bits result
- sll $s2, $t4, 9 # mant = HI << 9
- srl $t9, $t3, 23 # mant |= LOW >> (32-9)
- or $s2, $s2, $t9
- li $t9, 0x400000 # if (ml & (1<<22)), rounding check
- and $t8, $t9, $t3
- beq $t8, $zero, _multf_l0_end
- addi $s2, $s2, 1 # ++mant, round-up
- _multf_l0_end:
-
- move $a0, $s0
- move $a1, $s1
- move $a2, $s2
- jal encodeFloat # encodeFloat(s, e, m) : float(v0)
-
- # epilogue - stack-frame
- lw $s0, 0($sp)
- lw $s1, 4($sp)
- lw $s2, 8($sp)
- lw $fp, 12($sp)
- lw $ra, 16($sp)
- addi $sp, $sp, 20
- jr $ra # return
-
-
- #
- # fprod(vector, size)
- # Calculates the product x[0] * x[1] * x[2] * ... * x[n-1] as
- # Left fold of product operator on a vector of numbers - foldl(*, x)
- #
- # Arguments
- # $a0: Pointer to array x[] in RAM
- # $a1: Number of elements
- # Return
- # $v0: The product
- fprod:
- # intro - stack-frame
- addi $sp, $sp, -20 # fprod
- sw $ra, 16($sp)
- sw $fp, 12($sp)
- sw $s2, 8($sp)
- sw $s1, 4($sp)
- sw $s0, 0($sp)
- addi $fp, $sp, 0 # stack arguments (no)
-
- # keep input arguments
- move $s0, $a0 # x[]
- move $s1, $a1 # n
-
- lw $v0, 0($s0) # Acc = *x
- addi $s2, $zero, 1 # i=1
- _fprod_loop: # for ( ; i<n ; ) {
- bge $s2, $s1, _fprod_loop_end
- addi $s0, $s0, 4 # a0 <- *++x
- lw $a0, 0($s0)
- move $a1, $v0 # a1 <- use prev result
- jal multFloat
- addi $s2, $s2, 1 # ++i
- j _fprod_loop # }
- _fprod_loop_end:
-
- # epilogue - stack-frame
- lw $s0, 0($sp)
- lw $s1, 4($sp)
- lw $s2, 8($sp)
- lw $fp, 12($sp)
- lw $ra, 16($sp)
- addi $sp, $sp, 20
- jr $ra # return
|