jump to navigation

Floating point multiplication in MIPS April 14, 2009

Posted by aghus in Assembly Language, Bebas, IT, Ngoprek.
trackback

This assembly language improve upon multiplication of integer number in previous posting, to floating point multiplication. This program still use same algorithm for multiplication as hand scratch on primary school.🙂

.data
str1: .asciiz “Enter 1st number :\n”
str2: .asciiz “Enter 2nd number :\n”
str3: .asciiz “Content of FP register : ”
str4: .asciiz “The result : ”

main :
#————————- human interaction —————————————
la $a0, str1 # show string in screen
mov.s $f20,$f12 # data from reading float, then saved in $f20
la $a0, str3 # show string in screen
mfc1 $s0,$f20 # move raw data $s0=$f20
la $a0, str2 # show string in screen
mov.s $f21,$f12 # data from reading float, then saved in $f21
la $a0, str3 # show string in screen
mfc1 $s1,$f21 # move raw data $s1=$f21

#————————– get sign bit ——————————————-
move $t5,$s0
srl $t5,$s0,31 # get sign bit 1st and put it at $t5
move $t6,$s1
srl $t6,$s1,31 # get sign bit 2nd and put it at $t6
xor $t5,$t5,$t6
sll $t5,$t5,31 # get xor 1st and 2nd

#————————– get exponent ——————————————
sll $t7,$s0,1 # get 1st exponent bits
srl $t7,$t7,24
subi $t7,$t7,127 # exponent – bias
move $s2,$t7 # save exponen at $s2

sll $t8,$s1,1 # get 2nd exponent bits
srl $t8,$t8,24
subi $t8,$t8,127 # exponent – bias
move $s3,$t8 # save exponen at $s3

#————————– get mantissa ——————————————
sll $t8,$s0,9 # get mantissa bits
srl $t8,$t8,9
move $s4,$t8 #save mantissa at $s4
ori $s4,$s4,0x800000 # 1+ biner fraction —> $s4

sll $t9,$s1,9 # get mantissa bits
srl $t9,$t9,9
move $s5,$t9 #save mantissa at $s5
ori $s5,$s5,0x800000 # 1+ biner fraction —> $s5

move $a0,$s4
move $a0,$s5

add $t8,$t8,$t9 #check
srl $t8,$t8,23
beqz $t8,ovflow
addi $s3,$s3,1
ovflow:

shiftr :
srl $s4,$s4,1
srl $s5,$s5,1
andi $t8,$s4,0x00000001
andi $t9,$s5,0x00000001
or $t8,$t8,$t9
beqz $t8,shiftr

#————————— processing fpm —————————————-
addi $t0, $zero, 0 #prepare for product
addi $t7, $zero, 0
addi $t8, $zero, 0

addi $t7, $s4, 0 #prepare for multiplicand
addi $t8, $s5, 0 #prepare for multiplier
addi $s7, $zero, 1 #intialize for repetition
addi $t9, $zero, 16

forshift :
addi $s7,$s7,1
andi $v1,$t8,0x00000001
beqz $v1,ztimes
andi $t7,$t7,0xFFFFFFFF
add $t0,$t0,$t7 #initial value for prod = prod + mcand
ztimes :
sll $t7,$t7,1 #shift left mcand
srl $t8,$t8,1 #shift right mplier
move $a0,$t0
bne $s7, $t9, forshift

addi $t9,$zero,0
addi $t8,$zero,0
addi $t7,$zero,0

b23 :
sll $t0,$t0,1
addi $t7,$t7,1 #shifting to count exponent
andi $t9,$t0,0x00800000
srl $t8,$t0,23
beqz $t8,b23

move $a0,$t0
andi $t0,$t0,0x7FFFFF
addi $t8,$zero,23

add $t8,$s2,$s3

addi $t8,$t8,127
sll $t8,$t8,23
or $t0,$t0,$t8 # add exponent
or $t0,$t0,$t5 # add sign bit

#————result —————————–
la $a0, str3 # show string in screen

mtc1 $t0,$f12
mov.s $f0,$f20
li $v0, 10
syscall

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: