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

# hyphenrf's solution

## to Hexadecimal in the MIPS Assembly Track

Published at Jul 26 2020 · 0 comments
Instructions
Test suite
Solution

Convert a hexadecimal number, represented as a string (e.g. "10af8c"), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion).

On the web we use hexadecimal to represent colors, e.g. green: 008000, teal: 008080, navy: 000080).

The program should handle invalid hexadecimal strings.

## Source

All of Computer Science http://www.wolframalpha.com/examples/NumberBases.html

## Submitting Incomplete Solutions

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

### runner.mips

``````#
# Test hex_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
#
# hex_convert must:
# - be named hex_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 9
# input values (null terminated) & expected output values (word sized ints)
ins:  .asciiz "1", "C", "10", "af", "100", "19ACE", "000000", "ffff00", "00fff0"
outs: .word     1,  12,   16,  175,   256,  105166,        0, 16776960,    65520

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     hex_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

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 "impl.mips"``````
``````# This is exactly like binary but we do 4 bits at a time
# Additionally, converting hex digit to its value isn't as trivial
# as saying c - '0', BUT we can do it without branching! ..If we assume we
# always get valid hex string and don't program defensively.

# Some things to note about the ascii table (man ascii):
#   - a starts at 0x61 (7th bit set)
#   - A starts at 0x41 (7th bit set)
#   - 0 starts at 0x30 (7th bit unset)

# Meaning:
#  I. If I check the 7th bit, I can know whether it's _not_ a number
# II. If I unset the left byte, I can get a somewhat immediate value:
# '0'..'9' becomes 0..9 and 'aA'..'fF' becomes 1..6
# so if I use (I.) value, either 0 or 1, as an offset to an array, I can extract
# aA..fF's value from its ascii code and do the routine without a single branch:

# Steps:
# 1. offsets = {0, 9} --> use the value from below as index to this array
# 2. offset  = n >> 6 --> checking the 7th bit, output is 0 or 1 for ascii
# 3. value   = n & 0x0f + offsets[offset] --> calculate the value

.globl hex_convert

.data
offsets: .byte 0, 9 #step 1.

.text
hex_convert:
li    \$v0, 0
LOOP:
lb    \$t0, (\$a0)
beqz  \$t0, STOP

la    \$t1, offsets    #step 1.
srl   \$t2, \$t0, 6     #step 2.
add   \$t1, \$t1, \$t2   #start step 3. // offsets_ptr + offset
lb    \$t1, (\$t1)      #store offsets[0] in offset_value
andi  \$t0, \$t0, 0x0f  #unset left byte to get ascii_position
add   \$t0, \$t0, \$t1   #end step 3. // ascii_position + offset_value

sll   \$v0, \$v0, 4
or    \$v0, \$v0, \$t0

j     LOOP
STOP:
jr    \$ra``````