Selaa lähdekoodia

Init commit the submitted code

master
Christos Choutouridis 4 vuotta sitten
commit
cc06d1abc0
2 muutettua tiedostoa jossa 202 lisäystä ja 0 poistoa
  1. +1
    -0
      .gitignore
  2. +201
    -0
      fpmul.asm

+ 1
- 0
.gitignore Näytä tiedosto

@@ -0,0 +1 @@
*.jar

+ 201
- 0
fpmul.asm Näytä tiedosto

@@ -0,0 +1,201 @@
.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
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
# 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
and $a1, $a0, $t0 #Mantissa[a1] = (a0 & 0x7FFFFF) | 0x800000;
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
# arguments
# $a0 : 32bit floating point 1
# $a1 : 32bit floating point 2
# return
# $v0 : 32bit floating point multiplication result
multFloat:
# s0: sign / 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
jal decodeFloat #[v0, v1, a1] <- [s, e, m]
move $t0, $s0 # change variable
move $s0, $v0
move $s1, $v1
move $s2, $a1
move $a0, $t0 # load argument 2
jal decodeFloat #[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
# not zero
sll $s2, $t4, 8 # mant = HI << 8
srl $t9, $t3, 24 # mant |= LOW >> (32-8)
or $s2, $s2, $t9
addi $s1, $s1, 1 # ++exp
li $t9, 0x800000 # if (ml & (1<<23)), check to normalize
and $t8, $t9, $t3
beq $t8, $zero, _multf_l0_end
addi $s2, $s2, 1 # ++mant
j _multf_l0_end
_multf_l0:
# zero
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)), check to normalize
and $t8, $t9, $t3
beq $t8, $zero, _multf_l0_end
addi $s2, $s2, 1 # ++mant
_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(x,n) Calculates the product x[0]*x[1]*x[2]*...x[n-1]
# Arguments
# $a0: Pointer to array x[] 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:
bge $s2, $s1, _fprod_loop_end # for ( ; i<n ; )
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


Loading…
Peruuta
Tallenna