views:

631

answers:

3

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
+1  A: 

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.

Tom
i m confuse , but what if i give an C program, would you be able to translate it to me in MIPs program?
@chicagoman. Of course. You could as well. http://gavare.se/gxemul/
Tom
A: 

Here is my C program on above question, please can someone help me put it together for MIPS program.....

include

int main()

{

double score[15];

int i;

int fail_count=0,pass_count=0;

for(i=0;i<15;i++)

{

printf("\n\nEnter score for student %d :\ninput>",(i+1));

scanf("%lf",&score[i]);

if(score[i]<50)

fail_count++;

else

pass_count++;

}

for(i=0;i<15;i++)

{

if(score[i]<50)

printf("\nstudent %d %10lf %s",i+1,score[i],"pass");

else

printf("\nstudent %d %10lf %s",i+1,score[i],"fail");

}

printf("\n\n Total fail count = %d\nTotal pass count=%d\n\n",fail_count,pass_count);

system("pause");

}

Ok. Breathe slowly. Step instruction by instruction and translate it into mips. If you have any specific problems (i dont know how to use doubles, i cant load an array), i'll be glad to help. Have you read my answer, understood it, any doubts? Do your homework entirely? That's another issue.
Tom
Tom - if you can do by loading array will be more than happy ...thanks
OK, See my answer below.
Tom
A: 

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
Tom