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

时间:2021-05-05 11:29:22

I'm writing a MiPS program that will examine a list of 15 test scores. And it is going to input from the terminal. The passing criterion is the score of 50. The outputs to the terminal will include the scores in each category and the number of students passing and failing. I should use input prompts and output statement. Please I need some help, just need some advice how to do it.

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

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

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

I have a loop:

我有一个循环:

addi $t1,$t1,-1

li $v0,4

la $a0,prompt

syscall

2 个解决方案

#1


I don´t want to give it away, so i´ll throw some guidelines.

我不想放弃它,所以我会抛出一些指导方针。

You should read Assemblers, linkers and the Spim simulator. It´s a lot of help.

您应该阅读汇编程序,链接器和Spim模拟器。这有很多帮助。

So here it goes.

所以这就是它。

Create two 15- word arrays.

创建两个15字的数组。

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

Load on some register the loop control variable.

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

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

Now inside this loop read values

现在在这个循环中读取值

 syscall...     #SYS_READ

Then read this value (suppose you have it in register t4) and decide whether to store it in fail vector, or pass vector.

然后读取这个值(假设你在寄存器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

When you are done with all the 15 values, print out the vectors. This is kind of tricky. Before using your program, you should fill both vectors with some invalid value, like -1. So when you are printing vector to screen, you should stop when you find one of this invalid values. And while you are at it, keep a counter to show how many passed / failed.

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

In pseudo-code

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.

assembly (fill in the blanks)

装配(填空)

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.

Another tricky issue is the indexing of these vectors. Since they are arrays of words, then you should multiply by 4 (assuming 32 bit words) the loop control variable (classical i variable in C) to index the vector. If they were byte arrays, then no multiplication would be needed. And if they were short arrays...(well, you get my point)

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

For example:

passed_vector[i] #(C style sintax)

and let variable i be stored in register $t1 would turn out as:

并且将变量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   

So now you could load/store to passed_vector[i]

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

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

One way of solving these kind of things (that is, writing in assembly) is to write the program in C ( or some other language that you know ), and then translating it to assembly, instruction by instruction.

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

#2


Ok, here's how to load both integer arrays (and only that)

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

.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

#1


I don´t want to give it away, so i´ll throw some guidelines.

我不想放弃它,所以我会抛出一些指导方针。

You should read Assemblers, linkers and the Spim simulator. It´s a lot of help.

您应该阅读汇编程序,链接器和Spim模拟器。这有很多帮助。

So here it goes.

所以这就是它。

Create two 15- word arrays.

创建两个15字的数组。

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

Load on some register the loop control variable.

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

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

Now inside this loop read values

现在在这个循环中读取值

 syscall...     #SYS_READ

Then read this value (suppose you have it in register t4) and decide whether to store it in fail vector, or pass vector.

然后读取这个值(假设你在寄存器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

When you are done with all the 15 values, print out the vectors. This is kind of tricky. Before using your program, you should fill both vectors with some invalid value, like -1. So when you are printing vector to screen, you should stop when you find one of this invalid values. And while you are at it, keep a counter to show how many passed / failed.

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

In pseudo-code

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.

assembly (fill in the blanks)

装配(填空)

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.

Another tricky issue is the indexing of these vectors. Since they are arrays of words, then you should multiply by 4 (assuming 32 bit words) the loop control variable (classical i variable in C) to index the vector. If they were byte arrays, then no multiplication would be needed. And if they were short arrays...(well, you get my point)

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

For example:

passed_vector[i] #(C style sintax)

and let variable i be stored in register $t1 would turn out as:

并且将变量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   

So now you could load/store to passed_vector[i]

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

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

One way of solving these kind of things (that is, writing in assembly) is to write the program in C ( or some other language that you know ), and then translating it to assembly, instruction by instruction.

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

#2


Ok, here's how to load both integer arrays (and only that)

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

.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