Avatar of shmibs

shmibs's solution

to Hexadecimal in the MIPS Assembly Track

Published at Jul 13 2018 · 0 comments
Test suite

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.


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.


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


# 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"


        lw      $s0, n
        la      $s1, ins
        la      $s2, outs

        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

        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

        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

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

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

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

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

        la      $a0, expectedmsg
        li      $v0, 4

        move    $a0, $v1                # print actual that failed on
        li      $v0, 1                  # 1 is print integer

        la      $a0, tobemsg
        li      $v0, 4

        move    $a0, $s4                # print expected value that failed on
        li      $v0, 1                  # 1 is print integer

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

# # Include your implementation here if you wish to run this from the MARS GUI.
# .include "impl.mips"
# read a string representation of a hexadecimal number and
# return its integer equivalent
# a0::String -> v0::Signed
	li	$v0, 0

	lb	$t0, 0($a0)
	beq	$t0, $0, return
	addi	$a0, $a0, 1
	sll	$v0, $v0, 4
	addi	$t1, $t0, -57
	bgtz	$t1, alpha # alpha if above '9'
	addi	$t0, $t0, -48 # if not alpha, shunt '0' down to 0 val
	bltz	$t0, fail # either < '0' or an invalid in-between range
	add	$v0, $v0, $t0
	b	loop

# reduce ascii vals in A-F or a-f to contiguous num vals
	addi	$v0, $v0, 10 # add offset of being above '9' directly
	addi	$t1, $t0, -70
	bgtz	$t1, alpha_lcase # lcase if above 'F'
	addi	$t0, $t0, -65 # shunt 'A' down to 0 val
	addi $t1, $t0, -5
	bgtz	$t1, fail # > 'f'
	b	alpha_done

	addi	$t0, $t0, -32 # shunt 'a' down to 'A'
	b	alpha_continue

	li	$v0, -1

	jr	$ra

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?