ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# hyphenrf's solution

## to Trinary in the MIPS Assembly Track

Published at Jul 26 2020 · 0 comments
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"``````
``````# unsigned int bct_to_bin( unsigned int bct ) {
#        unsigned int acc = bct;
#
#        acc = acc - (((acc >>  2) & 0x33333333) * (    4 -    3));
#        acc = acc - (((acc >>  4) & 0x0F0F0F0F) * (   16 -    9));
#        acc = acc - (((acc >>  8) & 0x00FF00FF) * (  256 -   81));
#        acc = acc - (((acc >> 16) & 0x0000FFFF) * (65536 - 6561));
#
#        return acc;
# }
# see: http://homepage.divms.uiowa.edu/~jones/ternary/bct.shtml
.globl trinary_convert

.data
magics: .word 2,  0x33333333, 1,
4,  0x0f0f0f0f, 7,
8,  0x00ff00ff, 175,
16, 0x0000ffff, 58975

.text
trinary_convert:
li    \$v0, 0

la    \$t3, magics
li    \$t4, 3 #BCT2B counter

LOOP:
lb    \$t0, 0(\$a0)
beqz  \$t0, BCT2B

andi  \$t0, \$t0, 0x0f
sll   \$v0, \$v0, 2
or    \$v0, \$v0, \$t0

j     LOOP
BCT2B:
beqz  \$t4, STOP

lw    \$t0, 0(\$t3) # word * 0
lw    \$t1, 4(\$t3) # word * 1
lw    \$t2, 8(\$t3) # word * 2

move  \$t5, \$v0
srlv  \$t5, \$t5, \$t0
and   \$t5, \$t5, \$t1
mul   \$t5, \$t5, \$t2
sub   \$v0, \$v0, \$t5

subi  \$t4, \$t4, 1
addi  \$t3, \$t3, 12 # shift by 3 words
j     BCT2B
STOP:
jr    \$ra``````