MIPS程序,用于确定测试等级的通过/失败

时间:2009-04-29 21:15:29

标签: math assembly mips

我正在编写一个MiPS程序,该程序将检查15个测试分数的列表。它将从终端输入。通过标准是50分。终端的输出将包括每个类别的分数以及通过和失败的学生数量。我应该使用输入提示和输出语句。我需要一些帮助,只需要一些建议如何去做。

main:
 li $t1,15         #load 15 into $t1

 la $a1,array      #load a pointer to array into $a1

我有一个循环:

addi $t1,$t1,-1

li $v0,4

la $a0,prompt

syscall

2 个答案:

答案 0 :(得分:2)

我不想放弃它,所以我会抛出一些准则。

您应该阅读Assemblers, linkers and the Spim simulator。这很有帮助。

所以就这样了。

创建两个15字的数组。

 .data
 fail_vector: .word  -1,-1,-1 ...    #15 invalid words 
 passed_vector: .word  -1,-1,-1 ...  #15 invalid words 

在某些寄存器上加载循环控制变量。

 li $t1,15
 beq $t1,$zero,END
 addiu $t1,$t1,-1

现在在这个循环中读取值

 syscall...     #SYS_READ

然后读取这个值(假设你在寄存器t4中有它)并决定是将它存储在失败向量中,还是传递向量。

     addiu t4,t4,-50     #subtract 50 from input value. 
     blez  t4,FAILED     #If its lower than 0, then read value is lower than 50 ->FAIL
PASSED:
     #STORE VALUE INTO passed_vector

FAILED:
     #STORE VALUE INTO failed_vector

完成所有15个值后,打印出矢量。这有点棘手。 在使用程序之前,应该使用一些无效值填充两个向量,例如-1。 因此,当您将矢量打印到屏幕时,应该在找到其中一个无效值时停止。当你在这里时,请保留一个计数器来显示通过/失败的数量。

在伪代码中

for both arrays
   for (i in (0,15) and array[i] not -1)
        print array[i]
        add 1 to scores count //to count passed - failed test scores.

装配(填空)

END:
     li $t4,15
     li $t1,0
     beq $t1,$t4,EXIT   #condition. While ( i < 15) kind of thing.
     addiu $t1,$t1,-1

     #print out vectors and keep count on other registers
     #then print them out.

     syscall... #SYS_WRITE

EXIT: #exit syscall here.

另一个棘手的问题是索引这些向量。因为它们是单词数组,所以你应该乘以4(假设32位字)循环控制变量(C中的经典i变量)来索引向量。如果它们是字节数组,则不需要乘法。如果它们是短阵列......(好吧,你明白我的观点)

例如:

passed_vector[i] #(C style sintax)

并将变量i存储在寄存器$ t1中 结果如下:

  sll $t2,$t1,2             #i * sizeof(word)
  la  $a0,passed_vector     #$a0 points to passed_vector
  add $a0,$a0,$t2           #$a0 now points to passed_vector + i   

所以现在你可以加载/存储到pass_vector [i]

  sw  $t3,0($a0)            #0($a0) is passed_vector[0]
  lw  $t3,0($a0)

解决这类事情的一种方法(即,在汇编中编写)是用C语言(或你知道的其他语言)编写程序,然后将其翻译成汇编,逐条指令。

答案 1 :(得分:1)

好的,这是如何加载两个整数数组(只有那个)

.data
#These are two integer arrays. Each position is 32 bits long.
passed_vector: .word -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
failed_vector: .word -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1

.text

         #
         # Previous code here.
         #

         li $t5,50             #For comparing test_scores against.

         li $t0,0              # for (0..15)
         li $t6,15             #

LOOP:    beq $t0,$t6,CONTINUE  # loops while i<15


         li  $v0,5
         syscall      
         move $t1,$v0           #read test score and move it to register $t1

         bge $t1,$t5,PASSED    #if score >=50, load into passed_vector
FAILED:                        # else: test score lower than 50. Loads into failed vector

         #dont forget to increment the failed counter here
         sll $t2,$t0,2         
         sw  $t1,failed_vector($t2) 

         addiu $t0,$t0,1       #i++
         b     LOOP

PASSED:

         #dont forget to increment the passed counter here.
         sll $t2,$t0,2         
         sw  $t1,passed_vector($t2) 

         addiu $t0,$t0,1       #i++
         b     LOOP

CONTINUE: #other code