I'm having difficulty translating C code that loops through an array of grades. The C code is as follows:

int A, B, C, D, F;      // declare grade counters
    int i;              // declare loop counter
    int N   ;               // Number of scores to analyze 

    A=0; B=0; C=0; D=0; F=0;    // initialize grade counters

    // Read in test scores into grades array?
                        // N will be equal to the number of grades
                        // assume N = 60

    for (i=0; i<N; i++) {
    if(scores[i] >= 90)
            A = A + 1;
    else if(scores[i] >= 80)
            B = B + 1;  
    else if(scores[i] >= 70)
            C = C + 1;
    else if(scores[i] >= 60)
            D = D + 1;
    else
            F = F + 1;
    }

**Any help is appreciated. What I have so far is this: **

.data

data: 
    A: .word 0 #A=0
    B: .word 0 #B=0
    C: .word 0 #C=0
    D: .word 0 #D=0
    F: .word 0 #F=0
    i: .word 0 #i=0
    N: .word 60 #N=0
    scores: .word 0:60 #scores array with 60 integers


.text

    lw $s0, A #$s0=A=0
    lw $s1, B #$s1=B=0
    lw $s2, C #$s2=C=0
    lw $s3, D #$s3=D=0
    lw $s4, F #$s4=F=0
    lw $s5, i #$s5=i=0
    lw $s6, N #$s6=N=60
    lw $t1, 90
    lw $t2, 80
    lw $t3, 70
    lw $t4, 60
    lw $t5, scores


for: 
    blt $s5, $s6, if 
    li $v0, 4

if: 
    bge $s5($scores), $t1, inc1

inc1:   addi $s0, $s0, 1
    j for 

elseif1:
    bge $s5(scores), $t2, inc2

inc2:   addi $s1, $s1, 1
    j for   

elseif2:
    bge $s5(scores), $t3, inc3

inc3:   addi $s2, $s2, 1
    j for 

elseif3:
    bge $s5(scores), $t4, inc4

inc4:   addi $s3, $s3, 1
    j for       

else:
    addi $s4, $s4, 1
    j exit

exit:

You can also set your compiler to use (for gcc) the -S option, which will generate assembly code as the output. Then, you can look at what that does. Other compilers have similar options, depending upon the operating system and such. There are also gcc options to generate mips code on non-mips systems, so you can also add the -march=mipsNN where NN is the version of mips chip you want code for such as mips1, mips2, mips64r2, etc. You can use the -EB option for big-endian code or -EL for little-endian code.

@rproffitt and @rubberman: I am assuming the OP is writing this for an assembly language course, in which case the C code is simply given as a guide for hand-coding the assembly program.

@Kunyepa: How is the code misbehaving? Can you post the errors you are getting?

It would help if you could tell us the platform (presumably a MIPS simulator such as SPIM or MARS, though I guess it could be an actual MIPS processor), and what constraints your instructor has given you (e.g., the dreaded "no pseudo-instructions", which can be a serious impediment if you need to use any labels for data addressing). Are you expected to match the algorithm exactly, or is it just a guide which you can discard as long as the result is correct?

On a related note, has you instructor mentioned either a) reversing the loop conditional (e.g., using a decrementing count from x to zero rather than counting up) or b) using a jump table to index a test? Also, has the option of pre-sorting the test (i.e., writing a sorting routine to get the grade values in order, which can simplify the test at the cost of greater overhead overall - it's the kind of thing that can be slower when the data set is small, but speeds things up with a large data set) been brought up? Just a few hints.

This article has been dead for over six months. Start a new discussion instead.