Home:ALL Converter>MIPS: How to sort

MIPS: How to sort

Ask Time:2011-03-28T04:17:08         Author:Fahd

Json Formatter

I need your help with this problem of sorting in MIPS assembly :

how to write a MIPS program to read a text file containing only decimal integers and sort them in descending order.

The program should do the following:

■ Open a text file and read its content into an array of characters. The array should be limited to 1000 characters. MARS provides the system calls for opening and reading from a text file.

■ Traverse the array character by character. Convert each decimal string into binary. A decimal string consists of one or multiple decimal characters. It should terminate by white space or a newline character. Ignore and skip all other characters. Store all the decimal integers into an array of words. The size of the integer array should be limited to 100 words.

■ Sort the integer array in descending order.

■ Display the sorted array


Actualy I have no problem with sorting the array since I have it, but the problem with dealing with the text file, reading from it, converting to decimal plugging in the array.

Do you have any ideas ? comments ? suggestions ?

Thx in advance


Update: some has asked what is the question? the question is how to read from a txt file, convert the numbers to decimal ? this is the question.

Author:Fahd,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5452100/mips-how-to-sort
Manaf Abu.Rous :

Ok ,,, I will post some parts of the solution to a problem that is similar to yours (much complicated though) ,,,\n\nData Initialization \n\n# Data Buffers\n m1Buff: .space 20000 # File 1 Data Buffer\n numBuff: .space 200 # String Number Buffer\n# Arrays\n m1: .double 1:100\n\n\n\n\nCode to read from a file and save into a buffer\n\n\n\n############################################################\n#### Open the file for reading \nli $v0, 13 # system call for open file\nla $a0,m1File # input file name\nli $a1, 0 # flags\nli $a2, 0 # Open for reading (mode is 0: read, 1: write)\nsyscall # open a file (file descriptor returned in $a0)\nmove $t0, $a0 # save the fd (syscall below will overwrite $a0)\n############################################################\n#### read from file just opened\nli $v0, 14 # system call to read from file\nla $a1,m1Buff # address of buffer to store read data.\nli $a2, 20000 # max num of cahrs to read.\nsyscall # read from file\n\n############################################################\n #### Getting number of characters read from file.\n move $s0,$a0 # $s0 = number of read characters\n############################################################\n#### Close the file \nli $v0, 16 # system call for close file\nmove $a0, $t0 # Restore fd\nsyscall # close file\n############################################################\n\n\n\n\nAs for converting to decimal numbers ,,, I've written this procedure 2 years ago to convert a string to a floating point number (it also reads exponents i.e. 2.23E5) ,,, In your case you need to convert a string to a decimal which is much easier ,,, you should be able to figure out how to do it on your own giving the following procedure ,,, \n\n\n\n#####################################################################################################\n#####################################################################################################\n## String To FP Procedure\n## \n## INPUTS : A String That Is Terminated With Null Char.\n## $a0 = address of String\n##\n## Author : Manaf Abu.Rous\n#####################################################################################################\n#####################################################################################################\nstr2float:\n\n move $t1,$a0 # load Address Of Sttring In $t1\n\n li $t0,10 # t0 = 10\n mtc1 $t0,$f2 # move $t0 to $f2\n cvt.d.w $f2,$f2 # f2 = 10 ( Used for Multiplication & Division )\n\n li $t0,0 # t0 = 0\n mtc1 $t0,$f4 # move $t0 to $f4\n cvt.d.w $f4,$f4 # f4 = 0 = Integer Part. (Used To Generage The Integer Part Of The Number)\n\n li $t0,0 # t0 = 0\n mtc1 $t0,$f6 # move $t0 to $f6\n cvt.d.w $f6,$f6 # f6 = 0 = Fraction Part. (Used To Generage The Fraction Part Of The Number)\n\n li $t6,0 # $t6 = 0 = Exponent Part. (Used To Generage The Exponent Part Of The Number)\n\n li $t3,0 # $t3 = 0 ( Used To Determine The Sign Of The Number, +ve = 0, -ve = 1 )\n li $t4,0 # $t4 = 0 ( Used To Count The Number of Digits In a Fraction )\n li $t5,0 # $t5 = 0 ( Used To Determine If The Next Char Is a Fraction , Yes = 1, No = 0)\n li $t7,0 # $t5 = 0 ( Used To Determine If The Next Char Is an Exponent , Yes = 1, No = 0)\n\n #---------------------------------------------------------\n\n #------------------------------------------------------------------------------\n # Loop for visiting the buffer Char by Char and Constructing an Integer String\n #------------------------------------------------------------------------------\n\nreadLoop:\n\n lb $t2,0($t1) # load byte (char) in $t2\n\n #--------------------------------------------------------------------\n # -------- Check For \"-\" Sign\n bne $t2,0x2d,skipNeg # check if char = \"-\" ? if yes > sign reg = 1.\n li $t3,1 # $t3 = 1 ( Used To Determine The Sign Of The Number )\n j NextChar\n skipNeg: \n #--------------------------------------------------------------------\n\n #--------------------------------------------------------------------\n # -------- Check For \".\" Dot\n bne $t2,0x2e,skipDot # check if char = \".\" ? if yes > Go Read Fraction\n li $t5,1 # $t3 = 1 > Next Chars Are Fractinos\n j NextChar\n skipDot:\n #--------------------------------------------------------------------\n\n #--------------------------------------------------------------------\n # -------- Check For \"E\" Exponent \n bne $t2,0x45,skipE # check if char = \"E\" ? if yes > Go Read Exponent\n li $t7,1 # $t3 = 1 > Next Chars Are Exponent\n j NextChar\n skipE:\n #--------------------------------------------------------------------\n\n #--------------------------------------------------------------------\n # -------- Check For Null Char ( End Of String) \n beq $t2,0x00,FinishNumber # check if char = Null ? if yes > we are done with the number.\n #-------------------------------------------------------------------- \n\n #---------------------------------\n # Check What's The Current Char\n #---------------------------------\n beq $t7,1,readExponent # if $t7 = 1 (Next Char is Exponenet) Jump To ReadExponent\n beq $t5,1,readFraction # if $t7 = 1 (Next Char is Exponenet) Jump To ReadFraction\n\n #-------------------------------------------------------------------------\n # --------- Read Integer Part Of Char And Convert Them To Float\nreadInteger:\n subi $t2,$t2,0x30 # subtract 0x30 from char to convert it to a number.\n mtc1 $t2,$f10 # move $t2 to $f6\n cvt.d.w $f10,$f10 # f6 = converte integer to a FP number.\n mul.d $f4,$f4,$f2 # =((old)+2 X 10) \n add.d $f4,$f4,$f10 # =((old)+ 2)\n j NextChar\n #-------------------------------------------------------------------------\n #-------------------------------------------------------------------------\n # --------- Read Fraction Part Of Char And Convert Them To Float\nreadFraction:\n subi $t2,$t2,0x30 # subtract 0x30 from char to convert it to a number.\n mtc1 $t2,$f10 # move $t2 to $f6\n cvt.d.w $f10,$f10 # f6 = converte integer to a FP number.\n mul.d $f6,$f6,$f2 # =((old)+2 X 10) \n add.d $f6,$f6,$f10 # =((old)+ 2)\n addi $t4,$t4,1 # Increment Fractions Digits Counter\n j NextChar\n #-------------------------------------------------------------------------\n #-------------------------------------------------------------------------\n # --------- Read Exponenet Part Of Char And Convert Them To Integer\nreadExponent:\n li $t0,10\n subi $t2,$t2,0x30 # subtract 0x30 from char to convert it to a number.\n mult $t6,$t0 # =((old)+2 X 10)\n mflo $t6 \n add $t6,$t6,$t2 # =((old)+ 2)\n j NextChar\n #-------------------------------------------------------------------------\n\n\nNextChar:\n addiu $t1,$t1,1 # increment the address for the next buffer byte.\n j readLoop \n\n###################################### Finish Number Code ######################################\n\nFinishNumber: \n # Finalizing Fraction Part And Adding It To Integer Part ------------------\n beq $t5,0,skipFrac # If There's No Fraction Part Then Skip. ($t5 != 1) > Skip\n\n li $t0,1 # $t0 = 1\n mtc1 $t0,$f20 # move $t0 (Counter) to $f20\n cvt.d.w $f20,$f20 # f20 = 1\n\nFracLoop: #-------- Loop To Multiply 10 By It Self (Fraction Ditigs Counter) Times \n mul.d $f20,$f20,$f2 # $f20 = $f20 X $f2 ($f20 = $f20 X 10)\n addi $t4,$t4,-1 # Decrement Fraction Digits Counter\n bgtz $t4,FracLoop\n\n div.d $f6,$f6,$f20 # $f6 = $f6 / $f20 ($f6 = Fraction Part / FractionsDigits X 10 )\n add.d $f4,$f4,$f6 # $f4 = $f4 + $f6 ( $f4 = IntegerPart + Fraction Part )\n skipFrac:\n # -------------------------------------------------------------------------\n\n\n # Exponent Part -----------------------------------------------------------\n beq $t7,0,skipExp # If There's No Exponenet Part Then Skip. ($t7 != 1) > Skip\n beq $t6,1,skipExp # If The Exponent Is = 1 Then Skip\n addi $t6,$t6,-1 # Correct Exponent.\n mov.d $f18,$f4\n\nExpLoop: #-------- Loop To Compute The Exponenet (Multiply Number By It Self (Expoenet) Times)\n mul.d $f4,$f4,$f18 # $f4 = $f4 X $f4 ( Multiply Number By It Self )\n addi $t6,$t6,-1 # Decrement Fraction Digits Counter\n bgtz $t6,ExpLoop\n\n skipExp: \n # -------------------------------------------------------------------------\n\n\n # Checking Sign Register --------------------------------------------------\n bne $t3,1,skipSign # if $t3 != 1 Then Skip \n neg.d $f4,$f4 # $f4 = - $f4\n skipSign: \n # -------------------------------------------------------------------------\n\n # Save The Number In $f0 To Be Returned\n mov.d $f0,$f4 # $f0 = Converted String ( To Be Returned )\n\n################################### End Of Finish Number Code ##################################\n jr $ra # Return\n\n#####################################################################################################\n# End Of Procedure\n#####################################################################################################\n\n\nNow what's left is reading from the file buffer ,,, looping through the chars ,,, converting them to decimal numbers ,,,, and saving them into the array.",
2011-03-28T23:04:33
yy