#######################################################################################
#
# fp Addition
#
# input : fp1 in $4
# fp2 in $5
#
# output : fp=fp1+fp2 in $2
#
#
# Implementation:
#
# To add 2 fp numbers, we have to unnormalize onf of them so their exponents
# match. After that, the mantissa's are added (or subtracted) depending on the
# sign. Resulting number has to be normalized again if it became unnormalized.
#
#
#######################################################################################
fpAdd: addi $29, $29, -40 # Allocate stack space
sw $3, 36($29) # push $3
sw $4, 32($29) # push $4
sw $5, 28($29) # push $5
sw $6, 24($29) # push $6
sw $8, 20($29) # push $8
sw $9, 16($29) # push $9
sw $10,12($29) # push $10
sw $11, 8($29) # push $11
sw $16, 4($29) # push $16
sw $31, 0($29) # push $31
#START NICK ADD####################################################
jal fpExtract # send fp1 directly to fpExtract
add $11, $0, $2 # put sign1 in $11
add $12, $0, $3 # put exp1 in $12
add $13, $0, $16 # put mant1 in $13
add $4, $0, $5 # put fp2 into $4
jal fpExtract # send it to extract
sltu $1, $12, $3 # set $1=1 if $12 < $3 (exp1 < exp2)
beq $1, $0, elseif # if ($12 < $3) is not true, go to else
################################################################################
####### if( exp1 < exp2 ){ // EXPONENT2 IS GREATER
################################################################################
subu $24, $3, $12 # subtract exp2-exp1 to get number of shifts
srlv $13, $13, $24 # shift mant1 right for the number of shifts in $24
add $12, $0, $3 # put exponent result will in $12
beq $0, $0, endif # unconditional branch to endif
################################################################################
####### } else if( exp1 > exp2 ){ // EXPONENT1 IS GREATER
################################################################################
elseif: subu $24, $12, $3 # subtract exp1-exp2 to get number of shifts
srlv $16, $16, $24 # shift mant2 right for the number of shifts in $24
# no need to move exponent, it stays in $12
beq $0, $0, endif # unconditional branch to endif
################################################################################
####### } // END EXPONENT BLOCK
################################################################################
################################################################################
####### if(sign1 == sign2){ // Case : (+,+) or (-,-)
################################################################################
endif: bne $11, $4, signelseif # branch to signelseif if $11 != $4
addu $6, $13, $16 # add mant1+mant2
# just keep the sign result in $11
beq $0, $0, signendif # unconditional branch to endif
################################################################################
####### } else if (sign1 == POSITIVE){ // Case : (+,-)
################################################################################
signelseif: bne $11, $0, signelse # branch to signelse if sign1 is negative
sltu $1, $13, $16 # set $1=1 if $13 < $16 (mant1 < mant2)
beq $1, $0, poselse # if ($12 < $3) is not true, go to else
################################################################################
####### if (mant1 < mant2){ // mant1 < mant2
################################################################################
subu $6, $16, $13 # mant2-mant1, store in $6 to combine
lui $4, -32768 # Make the sign negative
beq $0, $0, signendif # unconditional branch to endif
################################################################################
####### } else { // mant2 > mant1
################################################################################
poselse: subu $6, $13, $16 # mant1 - mant2, store in $6 to combine
add $4, $0, $0 # put zero in $4 for the sign
beq $0, $0, signendif # unconditional branch to endif
################################################################################
####### } //
################################################################################
################################################################################
####### } else { // Case : (-,+)
################################################################################
signelse: sltu $1, $13, $16 # set $1=1 if $13 < $16 (mant1 < mant2)
beq $1, $0, negelse # if ($12 < $3) is not true, go to else
################################################################################
####### if (mant1 < mant2){ // mant1 < mant2
################################################################################
subu $6, $16, $13 # mant2-mant1, store in $6 to combine
add $4, $0, $0 # put zero in $4 for the sign
beq $0, $0, signendif # unconditional branch to endif
################################################################################
####### } else { // mant2 > mant1
################################################################################
negelse: subu $6, $13, $16 # mant1 - mant2, store in $6 to combine
lui $4, -32768 # Make the sign negative
beq $0, $0, signendif # unconditional branch to endif
################################################################################
####### } //
################################################################################
################################################################################
####### } //
################################################################################
signendif: add $5, $0, $12 # store the final exponent in $5
#END NICK ADD####################################################
fpAddDone: jal fpCombine # $2 <-- fp1+fp2
lw $31, 0($29) # pop $31
lw $16, 4($29) # pop $16
lw $11, 8($29) # pop $11
lw $10,12($29) # pop $10
lw $9, 16($29) # pop $9
lw $8, 20($29) # pop $8
lw $6, 24($29) # pop $6
lw $5, 28($29) # pop $5
lw $4, 32($29) # pop $4
lw $3, 36($29) # pop $3
addi $29, $29, 40 # Deallocate stack space
jr $31 # return($2)