链接MIPS中的值

时间:2013-09-26 17:18:43

标签: sorting mips

我正在尝试在MIPS中对浮点整数列表进行排序,我想我能够弄清楚,但是,每个浮点数都对应于游戏中玩家的得分。我试图找出一种方法来记住,例如,Bobby的得分是12.3,在我按照降序排列浮点数之后,我可以返回类似的东西: “Bobby 12.3,Johnny 10.2,Carl 8.8”。

目前我正在读取它们时将浮点数存储在F寄存器中,以及动态分配内存块中的名称,其大小是根据用户正在考虑的玩家数量生成的。

    .data
     Prompt1: .asciiz "Please enter number of players.\n"
    Prompt2: .asciiz "Please enter name of a player. (Max len 21 characters).\n"
     Prompt3: .asciiz "Enter player's points per game.\n"
    Prompt4: .asciiz "Enter player's minutes per game.\n"
    numPlayers: .space 4

  .text
.align 2
.globl main

main:
subu $sp, $sp, 32
sw $ra, 0($sp)
li $v0, 4
la $a0, Prompt1
syscall
li $v0, 5
la $a0, numPlayers
syscall
move $a0, $v0 
jal collect



collect:
subu $sp, $sp, 32
sw $ra, 0($sp)
sw $a0, 4($sp)#number of players

addi $t1, $zero, 21 #21 is max length of player name in characters; 21 bytes
mult $t0, 4($sp), $t1 #number of bytes you're going to need for strings for x players
 lw $a0, $t0 #a0 now has number of bytes you want for all of the strings
li $v0, 9
syscall
la $t2, $v0 #store memory allocated address into t2
la $t9, $t2 #remember the head you started at for strings
#memory has been made for strings

#make memory for floats now
addi $t0, $zer0, 32 #bits for a floating int, 1 calculation per player 
mult $t0, $t0, 4($sp) #number of players times floating point space
lw $a0, $t0 #a0 now has number of bytes for all the floats
li $v0, 9
syscall
la $t8, $v0 #store memory allocated address into $t8 for floats
la $t7, $t8 #remember head you started at for floats

loop:
beq $t0, $zero, sort
la $a0, Prompt2 #print string asking for a player name
la $v0, 4
syscall
la $a0, $t2 #load address of huge memory space
la $a1, 21 #max characters to read is 21
la $v0, 8 #read string and store into address supplied by $a0
syscall
addi $t2, $t2, 21 #move up 21 places in the memory for t2 
              #so that you're sure you're at an empty space in memory for the next string
#time to read floats
#ask for the points per game
la $a0, Prompt3 #load string to ask points per game
li $v0, 4
syscall
li $v0, 6
syscall #f0 has the float
mov.s $f1, $f0 #f1 has points per game for player 1
la $a0, Prompt5 #load string to ask minutes per game
li $v0, 4
syscall
li $v0, 6
syscall #f0 has the float
mov.d $f2, $f0 #f2 has minutes per game
div.d $f3, $f1, $f2 #divide f1 by f2 store in f3
mov.d $t8, $f3 #t8 now has the points per minute for player
addi $t8, $t8, 32 #move up 32 spots in memory

#how to associate name and player? 

addi $t0, $t0, -1 #decrement counter of how many more players you need to do
b loop


sort:
#to be figured out later

1 个答案:

答案 0 :(得分:1)

您可以在进行排序时使用与分数相同的名称,即切换位置,附加到临时列表等。

话虽这么说,你可能更容易为你的名字制作地址列表而不是彼此之后的所有名字。因此,为每个名称分配一个内存块并存储在列表中。这样,排序将更容易。