-
Notifications
You must be signed in to change notification settings - Fork 0
/
octalrunner.mips
97 lines (76 loc) · 3.15 KB
/
octalrunner.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#
# Test octal_convert 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
#
# octal_convert must:
# - be named octal_convert and declared as global
# - read input address of string 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 6
# input values (null terminated) & expected output values (word sized ints)
ins: .asciiz "1", "10", "17", "130", "2047", "1234567"
outs: .word 1, 8, 15, 88, 1063, 342391
failmsg: .asciiz "failed for test input: "
expectedmsg: .asciiz ". expected "
tobemsg: .asciiz " to be "
okmsg: .asciiz "all tests passed"
.text
runner:
lw $s0, n
la $s1, ins
la $s2, outs
run_test:
move $a0, $s1 # move address of input str to a0
jal octal_convert # 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
scan:
addi $s1, $s1, 1 # move input address on byte forward
lb $s3, 0($s1) # load byte
beq $s3, $zero, done_scan # if char null, break loop
j scan # loop
done_scan:
addi $s1, $s1, 1 # move input address on byte past null
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
la $a0, expectedmsg
li $v0, 4
syscall
move $a0, $v1 # print actual that failed on
li $v0, 1 # 1 is print integer
syscall
la $a0, tobemsg
li $v0, 4
syscall
move $a0, $s4 # print expected value that failed on
li $v0, 1 # 1 is print integer
syscall
li $a0, 1 # set error code to 1
li $v0, 17 # 17 is exit with error
syscall
# # Include your implementation here if you wish to run this from the MARS GUI.
.include "octalConversion.asm"