用于确定测试成绩通过/失败的 MIPS 程序

发布于 2024-07-18 04:37:47 字数 354 浏览 5 评论 0原文

我正在编写一个 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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入怼 2024-07-25 04:37:47

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

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

所以就这样了。

创建两个 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.

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

例如:

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   

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

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

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

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.

So here it goes.

Create two 15- word arrays.

 .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.

     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.

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)

For example:

passed_vector[i] #(C style sintax)

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

  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]

  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.

时光匆匆的小流年 2024-07-25 04:37:47

好的,这是如何加载两个整数数组(仅此而已)

.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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文