1
exercism fetch mips nth-prime

runner.mips

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#
# Test nth_prime with some examples
#
# s0 - num of tests left to run
# s1 - address of input word
# s2 - address of expected output word
# s3 - char byte
# s4 - output word
#
# nth_prime must:
# - be named nth_prime and declared as global
# - read prime index from a0
# - follow the convention of using the t0-9 registers for temporary storage
# - (if it uses s0-7 then it is responsible for pushing existing values to the stack then popping them back off before returning)
# - write integer result to v0

.data

# number of test cases
n: .word 10
# input values (null terminated) & expected output values (word sized ints)
ins:  .word     1,  2,  4,  7,   10,  47,   50,   57,   100,  101
outs: .word		2,  3,  7,  17,  29,  211,  229,  269,  541,  547

failmsg: .asciiz "failed for test input: "
okmsg: .asciiz "all tests passed"


.text

runner:
        lw      $s0, n
        la      $s1, ins
        la      $s2, outs
        j	run_test

run_test:
        lw      $a0, 0($s1)             # move input number to a0
        jal     nth_prime               # call subroutine under test
        move    $v1, $v0                # move return value in v0 to v1 because we need v0 for syscall

        lw      $s4, 0($s2)             # read expected output from memory
        bne     $v1, $s4, exit_fail     # if expected doesn't match actual, jump to fail

        addi    $s1, $s1, 4             #move to next word in input
        addi    $s2, $s2, 4             #move to next word in output

        sub     $s0, $s0, 1             # decrement num of tests left to run
        bgt     $s0, $zero, run_test    # if more than zero tests to run, jump to run_test

exit_ok:
        la      $a0, okmsg              # put address of okmsg into a0
        li      $v0, 4                  # 4 is print string
        syscall

        li      $v0, 10                 # 10 is exit with zero status (clean exit)
        syscall

exit_fail:
        la      $a0, failmsg            # put address of failmsg into a0
        li      $v0, 4                  # 4 is print string
        syscall

        move    $a0, $s1                # print input that failed on
        li      $v0, 4
        syscall

        li      $a0, 1                  # set error code to 1
        li      $v0, 17                 # 17 is exit with error
        syscall

        jr      $ra

# # Include your implementation here if you wish to run this from the MARS GUI.
# .include "impl.mips"