1

我正在尝试对 MIPS 中的浮点整数列表进行排序,我想我能够弄清楚,但是,每个浮点数都对应于游戏中玩家的得分。我试图找出一种方法来记住,例如,鲍比的分数是 12.3,在我按降序对浮点数进行排序之后,我可以返回类似:“鲍比 12.3,约翰尼 10.2,卡尔 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
4

1 回答 1

1

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

话虽如此,您可能更容易为您的姓名制作一个地址列表,而不是一个接一个的所有姓名。因此,为每个名称分配一块内存并存储在列表中。这样,排序会更容易。

于 2013-09-26T22:02:17.187 回答