Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly MIPS:如何排序_Assembly_Mips_Mars Simulator - Fatal编程技术网

Assembly MIPS:如何排序

Assembly MIPS:如何排序,assembly,mips,mars-simulator,Assembly,Mips,Mars Simulator,我需要您帮助解决MIPS程序集中的排序问题: 如何编写一个MIPS程序来读取只包含十进制整数的文本文件,并按降序对它们进行排序 程序应执行以下操作: ■ 打开文本文件并将其内容读入字符数组。数组应限制为1000个字符。MARS提供打开和读取文本文件的系统调用 ■ 逐个字符遍历数组。将每个十进制字符串转换为二进制。十进制字符串由一个或多个十进制字符组成。它应该以空格或换行符结尾。忽略并跳过所有其他字符。将所有十进制整数存储到一个字数组中。整数数组的大小应限制为100个字 ■ 按降序对整数数组排序

我需要您帮助解决MIPS程序集中的排序问题:

如何编写一个MIPS程序来读取只包含十进制整数的文本文件,并按降序对它们进行排序

程序应执行以下操作:

■ 打开文本文件并将其内容读入字符数组。数组应限制为1000个字符。MARS提供打开和读取文本文件的系统调用

■ 逐个字符遍历数组。将每个十进制字符串转换为二进制。十进制字符串由一个或多个十进制字符组成。它应该以空格或换行符结尾。忽略并跳过所有其他字符。将所有十进制整数存储到一个字数组中。整数数组的大小应限制为100个字

■ 按降序对整数数组排序

■ 显示已排序的数组


实际上,我对数组的排序没有问题,因为我有它,但是处理文本文件,从中读取,转换为十进制插入数组的问题

你有什么想法吗?评论?建议?

提前Thx


更新:有人问问题是什么?
问题是如何从txt文件中读取,将数字转换为十进制?这就是问题所在。

使用带有$v0=13、$a1=0、$a2=0的syscall打开文件


然后使用带有$v0=14的syscall将其读入缓冲区。

使用带有$v0=13、$a1=0、$a2=0的syscall打开文件


然后使用带$v0=14的syscall将其读入缓冲区。

好的,,,我将发布一个问题的解决方案的某些部分,该问题与您的问题类似(尽管非常复杂)

数据初始化

# Data Buffers
  m1Buff:       .space  20000           # File 1 Data Buffer
  numBuff:       .space  200             # String Number Buffer
# Arrays
  m1:            .double    1:100

从文件读取并保存到缓冲区的代码



至于转换为十进制数,,,我在两年前写了这个程序,将字符串转换为浮点数(它还读取指数,即2.23E5),,,在您的情况下,您需要将字符串转换为十进制数,这要容易得多,,,您应该能够通过下面的程序,自己解决如何转换的问题,,,



现在剩下的就是从文件缓冲区读取,循环遍历字符,将它们转换为十进制数,并将它们保存到数组中。

好的,,,我将发布一个问题的解决方案的一些部分,这个问题与您的问题类似(虽然非常复杂)

数据初始化

# Data Buffers
  m1Buff:       .space  20000           # File 1 Data Buffer
  numBuff:       .space  200             # String Number Buffer
# Arrays
  m1:            .double    1:100

从文件读取并保存到缓冲区的代码



至于转换为十进制数,,,我在两年前写了这个程序,将字符串转换为浮点数(它还读取指数,即2.23E5),,,在您的情况下,您需要将字符串转换为十进制数,这要容易得多,,,您应该能够通过下面的程序,自己解决如何转换的问题,,,



现在剩下的是从文件缓冲区读取,循环字符,将它们转换为十进制数,并将它们保存到数组中。

请问一个具体问题。Q:如何从txt文件读取,将数字转换为十进制数?这就是问题。请问一个具体的问题。问:如何从txt文件中读取,将数字转换为十进制?这就是问题所在。
#####################################################################################################
#####################################################################################################
## String To FP Procedure
##  
## INPUTS : A String That Is Terminated With Null Char.
##   $a0 = address of String
##
## Author : Manaf Abu.Rous
#####################################################################################################
#####################################################################################################
str2float:

    move    $t1,$a0             # load Address Of Sttring In $t1

    li      $t0,10              # t0 = 10
    mtc1    $t0,$f2             # move $t0 to $f2
    cvt.d.w $f2,$f2             # f2 = 10 ( Used for Multiplication & Division )

    li      $t0,0               # t0 = 0
    mtc1    $t0,$f4             # move $t0 to $f4
    cvt.d.w $f4,$f4             # f4 = 0 = Integer Part. (Used To Generage The Integer Part Of The Number)

    li      $t0,0               # t0 = 0
    mtc1    $t0,$f6             # move $t0 to $f6
    cvt.d.w $f6,$f6             # f6 = 0 = Fraction Part. (Used To Generage The Fraction Part Of The Number)

    li      $t6,0               # $t6 = 0 = Exponent Part. (Used To Generage The Exponent Part Of The Number)

    li      $t3,0               # $t3 = 0 ( Used To Determine The Sign Of The Number, +ve = 0, -ve = 1 )
    li      $t4,0               # $t4 = 0 ( Used To Count The Number of Digits In a Fraction )
    li      $t5,0               # $t5 = 0 ( Used To Determine If The Next Char Is a Fraction , Yes = 1, No = 0)
    li      $t7,0               # $t5 = 0 ( Used To Determine If The Next Char Is an Exponent , Yes = 1, No = 0)

    #---------------------------------------------------------

    #------------------------------------------------------------------------------
    #  Loop for visiting the buffer Char by Char and Constructing an Integer String
    #------------------------------------------------------------------------------

readLoop:

    lb      $t2,0($t1)          # load byte (char) in $t2

    #--------------------------------------------------------------------
    # -------- Check For "-" Sign
    bne     $t2,0x2d,skipNeg    # check if char = "-" ? if yes > sign reg = 1.
    li      $t3,1               # $t3 = 1 ( Used To Determine The Sign Of The Number )
    j       NextChar
    skipNeg:    
    #--------------------------------------------------------------------

    #--------------------------------------------------------------------
    # -------- Check For "." Dot
    bne $t2,0x2e,skipDot        # check if char = "." ? if yes > Go Read Fraction
    li      $t5,1               # $t3 = 1 > Next Chars Are Fractinos
    j       NextChar
    skipDot:
    #--------------------------------------------------------------------

    #--------------------------------------------------------------------
    # -------- Check For "E" Exponent 
    bne $t2,0x45,skipE          # check if char = "E" ? if yes > Go Read Exponent
    li      $t7,1               # $t3 = 1 > Next Chars Are Exponent
    j       NextChar
    skipE:
    #--------------------------------------------------------------------

    #--------------------------------------------------------------------
    # -------- Check For Null Char ( End Of String) 
    beq     $t2,0x00,FinishNumber   # check if char = Null ? if yes > we are done with the number.
    #--------------------------------------------------------------------   

    #---------------------------------
    # Check What's The Current Char
    #---------------------------------
    beq     $t7,1,readExponent      # if $t7 = 1 (Next Char is Exponenet) Jump To ReadExponent
    beq     $t5,1,readFraction      # if $t7 = 1 (Next Char is Exponenet) Jump To ReadFraction

    #-------------------------------------------------------------------------
    # --------- Read Integer Part Of Char And Convert Them To Float
readInteger:
    subi    $t2,$t2,0x30        # subtract 0x30 from char to convert it to a number.
    mtc1    $t2,$f10            # move $t2 to $f6
    cvt.d.w $f10,$f10           # f6 = converte integer to a FP number.
    mul.d   $f4,$f4,$f2         # =((old)+2 X 10)   
    add.d   $f4,$f4,$f10        # =((old)+ 2)
    j       NextChar
    #-------------------------------------------------------------------------
    #-------------------------------------------------------------------------
    # --------- Read Fraction Part Of Char And Convert Them To Float
readFraction:
    subi    $t2,$t2,0x30        # subtract 0x30 from char to convert it to a number.
    mtc1    $t2,$f10                # move $t2 to $f6
    cvt.d.w $f10,$f10           # f6 = converte integer to a FP number.
    mul.d   $f6,$f6,$f2         # =((old)+2 X 10)   
    add.d   $f6,$f6,$f10            # =((old)+ 2)
    addi    $t4,$t4,1           # Increment Fractions Digits Counter
    j       NextChar
    #-------------------------------------------------------------------------
    #-------------------------------------------------------------------------
    # --------- Read Exponenet Part Of Char And Convert Them To Integer
readExponent:
    li      $t0,10
    subi    $t2,$t2,0x30        # subtract 0x30 from char to convert it to a number.
    mult    $t6,$t0             # =((old)+2 X 10)
    mflo    $t6 
    add     $t6,$t6,$t2         # =((old)+ 2)
    j       NextChar
    #-------------------------------------------------------------------------


NextChar:
    addiu   $t1,$t1,1           # increment the address for the next buffer byte.
    j       readLoop        

###################################### Finish Number Code ######################################

FinishNumber:   
    # Finalizing Fraction Part And Adding It To Integer Part ------------------
    beq     $t5,0,skipFrac      # If There's No Fraction Part Then Skip. ($t5 != 1) > Skip

    li      $t0,1               # $t0  = 1
    mtc1    $t0,$f20            # move $t0 (Counter) to $f20
    cvt.d.w $f20,$f20           # f20 = 1

FracLoop:       #-------- Loop To Multiply 10 By It Self (Fraction Ditigs Counter) Times 
                mul.d   $f20,$f20,$f2       # $f20 = $f20 X $f2 ($f20 = $f20 X 10)
                addi    $t4,$t4,-1          # Decrement Fraction Digits Counter
                bgtz $t4,FracLoop

    div.d   $f6,$f6,$f20        # $f6 = $f6 / $f20 ($f6 = Fraction Part / FractionsDigits X 10 )
    add.d   $f4,$f4,$f6         # $f4 = $f4 + $f6 ( $f4 = IntegerPart + Fraction Part )
    skipFrac:
    # -------------------------------------------------------------------------


    # Exponent Part -----------------------------------------------------------
    beq     $t7,0,skipExp       # If There's No Exponenet Part Then Skip. ($t7 != 1) > Skip
    beq     $t6,1,skipExp       # If The Exponent Is = 1 Then Skip
    addi    $t6,$t6,-1          # Correct Exponent.
    mov.d   $f18,$f4

ExpLoop:    #-------- Loop To Compute The Exponenet (Multiply Number By It Self (Expoenet) Times)
            mul.d   $f4,$f4,$f18        # $f4 = $f4 X $f4 ( Multiply Number By It Self )
            addi    $t6,$t6,-1          # Decrement Fraction Digits Counter
            bgtz $t6,ExpLoop

    skipExp:    
    # -------------------------------------------------------------------------


    # Checking Sign Register --------------------------------------------------
    bne     $t3,1,skipSign      # if $t3 != 1  Then Skip    
    neg.d   $f4,$f4             # $f4 = - $f4
    skipSign:   
    # -------------------------------------------------------------------------

    # Save The Number In $f0 To Be Returned
    mov.d   $f0,$f4             # $f0 = Converted String ( To Be Returned )

################################### End Of Finish Number Code ##################################
    jr      $ra         # Return

#####################################################################################################
#                                        End Of Procedure
#####################################################################################################