# shmibs's solution

## to Trinary in the MIPS Assembly Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Convert a trinary number, represented as a string (e.g. '102012'), to its decimal equivalent using first principles.

The program should consider strings specifying an invalid trinary as the value 0.

Trinary numbers contain three symbols: 0, 1, and 2.

The last place in a trinary number is the 1's place. The second to last is the 3's place, the third to last is the 9's place, etc.

``````# "102012"
1       0       2       0       1       2    # the number
1*3^5 + 0*3^4 + 2*3^3 + 0*3^2 + 1*3^1 + 2*3^0    # the value
243 +     0 +    54 +     0 +     3 +     2 =  302
``````

If your language provides a method in the standard library to perform the conversion, pretend it doesn't exist and implement it yourself.

## Source

All of Computer Science http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### runner.mips

``````#
# Test trinary_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
#
# trinary_convert must:
# - be named trinary_convert and declared as global
# - 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 "0", "1", "10", "102101", "22222", "10000"
outs: .word     0,   1,    3,      307,    242,       81

failmsg: .asciiz "failed for test input: "
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     trinary_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:
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

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 "impl.mips"``````
``````# read a string representation of a ternary number and
# return its integer equivalent
# a0::String -> v0::Signed
trinary_convert:
li	\$v0, 0

loop:
lb	\$t0, 0(\$a0)
beq	\$t0, \$0, return
sll	\$t1, \$v0, 1
bltz	\$t0, fail # below the '0' - '2' range
bgez	\$t1, fail # above the '0' - '2' range
b	loop

fail:
li	\$v0, -1

return:
jr	\$ra``````