jump to navigation

Floating Point Addition in MIPS January 20, 2009

Posted by aghus in Assembly Language, Bebas, Free - Gratis Download ?, IT, Ngoprek.
trackback

Addition is a simple process for human. Entry the numbers in calculator or computer, then we’ll get the result. But sometime we should to know how it works. In MIPS processor, this is assembly language that shows the process. How bits go walk  in registers, then appear result in output peripheral. You can use MARS tool from here

# Floating point addition demonstration for 2 positive numbers ———–
# ver.1🙂

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

.text

main :
#————————- human interaction —————————————
la $a0, str1          # show string in screen
jal show
jal rflo
jal movfl
mov.s $f20,$f12     # data from reading float, then saved in $f20
jal pflo
jal nline
la $a0, str3          # show string in screen
jal show
mfc1 $s0,$f20                     # move raw data $s0=$f20
move $a0,$s0
jal print_bin
jal nline

la $a0, str2          # show string in screen
jal show
jal rflo
jal movfl
mov.s $f21,$f12     # data from reading float, then saved in $f21
jal pflo
jal nline
la $a0, str3          # show string in screen
jal show
mfc1 $s1,$f21                     # move raw data $s1=$f21
move $a0,$s1
jal print_bin
jal nline

#————————– get sign bit ——————————————-
move $t5,$s0
srl $t5,$s0,31                  # get sign bit 1st
move $t6,$s1
srl $t6,$s1,31                  # get sign bit 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
move $a0,$t7
jal pint
jal nline

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
move $a0,$t8
jal pint
jal nline
#————————– 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
move $a0,$s4
jal print_bin
jal nline

sll $t9,$s1,9                  # get mantissa bits
srl $t9,$t9,9
move $s5,$t9 #save mantissa at $s4
ori $s5,$s5,0x800000           # 1+ biner fraction
move $a0,$s5
jal print_bin
jal nline

#————————— processing fpa —————————————-

sub $s7,$s2,$s3
move $t0,$s7
move $a0,$t0

chk_comp :
beq $s2,$s3, first_eq_second  # 1st exp = 2nd exp
bgt $s2,$s3, first_gt_second  # 1st exp > 2nd exp
bgt $s3,$s2, first_lt_second  # 1st exp < 2nd exp
j chk_comp

first_gt_second :
srl $s5,$s5,1
move $t0,$s5
move $a0,$t0
jal print_bin
jal nline
addi $s3,$s3,1
j chk_comp

first_lt_second :
srl $s4,$s4,1
move $t1,$s4
move $a0,$t1
jal print_bin
jal nline
addi $s2,$s2,1
j chk_comp

first_eq_second :
add $s6,$s4,$s5
move $a0,$s6
jal print_bin
jal nline
#move $t0,$s6
andi $t0,$s6,0x01000000
srl $t0,$t0,24
addi $t7,$t7,1
bne $t0,$t7,here
srl $s6,$s6,1
addi $s2,$s2,1

here :
andi $s6,$s6,0x7FFFFF #masking mantissa bits
addi $s2,$s2,127  # convert exp 2 bias
sll $s2,$s2,23

andi $s6,$s6,0xffffffff
or $s2,$s2,$s6
la $a0, str3  # show string in screen
jal show
move $a0,$s2
jal print_bin
jal nline

mtc1 $s2,$f12
mov.s $f0,$f20
la $a0, str4  # show string in screen
jal show
jal pflo

jal exit

#############################################
#—————————
show : #showstring
li $v0, 4
syscall
jr $ra
#—————————
exit : # exit
li $v0, 10
syscall
#—————————
rint : # read integer
li $v0,5
syscall
jr $ra
#—————————
pint : # print integer
li $v0, 1
syscall
jr $ra
#—————————
movfl : # moving data at floating register
mov.s $f12,$f0
jr $ra
#—————————
nline : # make newline
la $a0,newline
li $v0,4
syscall
jr $ra
#—————————
print_bin: # print binary digit
add $t0, $zero, $a0 # put our input ($a0) into $t0
add $t1, $zero, $zero # Zero out $t1
addi $t3, $zero, 1 # load 1 as a mask
sll $t3, $t3, 31 # move the mask to appropriate position
addi $t4, $zero, 32 # loop counter
loop:
and $t1, $t0, $t3 # and the input with the mask
beq $t1, $zero, print # Branch to print if its 0

add $t1, $zero, $zero # Zero out $t1
addi $t1, $zero, 1 # Put a 1 in $t1
j print

print: li $v0, 1
move $a0, $t1
syscall

srl $t3, $t3, 1
addi $t4, $t4, -1
bne $t4, $zero, loop
jr $ra
#————————————

Other version with subtraction is available

Comments»

1. Saif - April 18, 2009

WHERE IS RFLO AND PFLO?

2. aghus - April 18, 2009

rflo and pflo are reading and printing floating point syscall services

3. Saif - April 19, 2009

Can you help me out in the following (please email me within a day if possible):

Write and test a MIPS assembly language program to count the words in a text file and compute their frequency. The program should do the following:
■ Open a text file and read all characters into an array. The maximum number of characters to be
read should be limited to the size of the array, which should be 100,000 characters. MARS provides the system calls for opening a file, reading from a file, etc.
■ Traverse the array character by character and detect the beginning and end of each word. A word is defined here to contain only letters (capital or lowercase). Other characters (spaces, commas, periods, parentheses, digits, etc.) should not be counted. Convert all letters to uppercase and convert all non-letter symbols to white space.
■ Construct a second array to contain all unique words encountered in the first array and their
frequencies. For example, if a word appears 100 times in the first array then it should appear
once in the second array and its frequency should be 100.
■ Sort the words in the second array according to their frequency and output the top N words that
have the highest frequencies.

4. negin - March 24, 2011

Hey can you help with writing the floating point addition without using $s registers?

5. lkajsd - April 5, 2013

dude subi doesnt exist in mips


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: