Skip to content

Instantly share code, notes, and snippets.

@ak-seyam
Created January 3, 2020 16:37
Show Gist options
  • Save ak-seyam/e35e76bd5856b59c893348865955c6a0 to your computer and use it in GitHub Desktop.
Save ak-seyam/e35e76bd5856b59c893348865955c6a0 to your computer and use it in GitHub Desktop.
# $s0 > sign of number1
# $s1 > sign of number 2
# $s2 > exp of number 1
# Ss3 > exp of number 2
# $s4 > mantissa number 1
# $s5 > mantissa of number2
# $s6 > final mantissa
# $s7 > final exponent
# $t7 > final sign no need for this my task was to write strick +ve numbers addition code
#$t4 > the final result
#note: by overflow here i mean overflow of mantissa bits
.data
#test1 result = 0x40000000 (check)
#number1: .word 0x3f800000
#number2: .word 0x3f800000
#test2 result = 0x40400000 (check)
#number1: .word 0x40000000
#number2: .word 0x3f800000
#test3 result = 0x40d00000 (check)
#number1: .word 0x40400000
#number2: .word 0x40600000
#test4 result = 0x47800018 (check)
number1: .word 0x3f00ffc0
number2: .word 0x477fffb0
.text
.globl main
main:
addi $t7 , $zero , 0 #sign will always be positive
#masking sign
lw $t0 , number1
andi $s0 ,$t0 , 2147483648
lw $t0 , number2
andi $s1 , $t0 ,2147483648
srl $s0 ,$s0 ,31
srl $s1 ,$s1 ,31
#masking exp
lw $t0 , number1
andi $s2 , $t0 ,0x7f800000
lw $t0 , number2
andi $s3 , $t0 ,0x7f800000
srl $s2 ,$s2 ,23
srl $s3 ,$s3 ,23
#masking mantissa
lw $t0 , number1
andi $s4 , $t0 ,0x7fffff
lw $t0 , number2
andi $s5 , $t0 ,0x7fffff
#addition logic
#if number1's exp > 2's exp
#take number 1 as a final exp
# shift the 2nd's mantissa right by the difference
# add the 2 mantissas
# if 2 > 1 do opesite
#
#if equal
# preparing each number to be added adding one to the bit no 24 in each mantissa
###############################################
#~start addition~#
#t5 is used as global exp
###############################################
#1- preparing each mantissa
ori $s4 , $s4 ,0x800000
ori $s5 , $s5 ,0x800000
#condition 1
slt $t0 , $s2 ,$s3 # 1st exp < 2nd exp => t0 == 1
beq $t0 , $0 , doOp #else do the opisite
# exp 2 > exp1
sub $t0 , $s3 ,$s2 # take 2nd exp as final exp
srav $s4 ,$s4 ,$t0 #shift number1's mantessa t0s steps
addi $s7 , $s3 ,0 #assigning the final exponent
addi $t5 , $s7 ,0 #exponent flag 2b the final expnent
j equal
#exp 1> exp2
doOp: beq $s2 , $s3 ,equal #if s2 != s3 => do the opisite of condition 1
sub $t0 , $s2 ,$s3
srav $s5 ,$s5 ,$t0
addi $s7 , $s2 ,0 #assigning the final exponent tobe the first's
addi $t5 , $s7 ,0 #exponent flag 2b the final exponent
j equal
equal:
add $s6 , $s4 , $s5 #sum fractions and add the result to the final mantissa
#if $t4 (sum of functions) is less than 0xffffff [24 ones] (no overflow)
addi $t3 , $0 ,0xffffff
blt $s6, $t3,s6IsLessThan24Ones #cheack for over flow mantissa > 2^24 -1
#shift to the right
srl $s6, $s6 ,1
#remove the last one
andi $s6 , $s6 ,0x7fffff
#exponent logic
#if there's a bigger exp t6 will not be zero so go to useTheT7Resulting exp
bne $t5 , $0,useTheT5asResultingExp #if (t5 ==0){
addi $s7 ,$s2 , 1 #use whatever exp but increase it by one because (1+1=10)
# so you will always increase
# the value of exp in this situation
#then save it to the resulting exp
j done
useTheT5asResultingExp: #else{
addi $s7 ,$t5 , 1 #use the value saved in exp temp (t6)
j done #}
s6IsLessThan24Ones: # no overflow
#remove the last one
andi $s6 , $s6 ,0x7fffff #you now have a the final mantessa by removing the implicit one
#exponent logic
bne $t5 , $0,useTheT5asResultingExp2 #if t5 = 0 which means exponents were equal
addi $s7 ,$s2 , 0 #use anyone without increasing
j done
useTheT5asResultingExp2: #else
addi $s7 ,$t5 , 0 #use the one in the temp as the resulting one
done:
#collecting everything togther
sll $t4 , $s7 ,23 #add exponents
add $t4 ,$t4 , $s6 #add mantissa
addi $a0 , $t4 , 0 #add the result t4 to a0 to be shown
li $v0 , 1 #show intger
syscall #send to system
#finish
li $v0 , 10 #send finish procedural
syscall #send to system
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment