Avatar of shmibs

shmibs's solution

to Binary in the MIPS Assembly Track

Published at Jul 13 2018 · 2 comments
Instructions
Test suite
Solution

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

Implement binary to decimal conversion. Given a binary input string, your program should produce a decimal output. The program should handle invalid inputs.

Note

  • Implement the conversion yourself. Do not use something else to perform the conversion for you.

About Binary (Base-2)

Decimal is a base-10 system.

A number 23 in base 10 notation can be understood as a linear combination of powers of 10:

  • The rightmost digit gets multiplied by 10^0 = 1
  • The next number gets multiplied by 10^1 = 10
  • ...
  • The nth number gets multiplied by 10^(n-1).
  • All these values are summed.

So: 23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10

Binary is similar, but uses powers of 2 rather than powers of 10.

So: 101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10.

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 binary_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
#
# binary_convert must:
# - be named binary_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 9
# input values (null terminated) & expected output values (word sized ints)
ins:  .asciiz "0", "1", "10", "11", "100", "1001", "11010", "10001101000", "000011111"
outs: .word     0,   1,   2,     3,    4,       9,      26,          1128,          31

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

        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"
# convert a string representation of a binary number
# to its corresponding integer value
# a0::String -> v0::Signed
binary_convert:
	li	$v0, 0

loop:
	lb	$t0, 0($a0)
	beq	$t0, $0, return
	addi	$a0, $a0, 1
	sll	$v0, $v0, 1
	li	$t1, 48 # '0'
	beq	$t0, $t1, loop
	li	$t1, 49 # '1'
	bne	$t0, $t1, fail
	addi	$v0, $v0, 1
	b	loop

fail:
	li	$v0, -1

return:
	jr	$ra

Community comments

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

been too long since last asm-ing X_X

Avatar of Lifelovinglight
Lifelovinglight
commented over 2 years ago

Somehow I didn't actually grok that labels fall through to each other and was stuck thinking of them as argument-less functions without return calls.

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?