Skip to content

Instantly share code, notes, and snippets.

@nsporillo
Created May 12, 2015 17:45
Show Gist options
  • Save nsporillo/af2eef2bf68d9f515a19 to your computer and use it in GitHub Desktop.
Save nsporillo/af2eef2bf68d9f515a19 to your computer and use it in GitHub Desktop.
final project
##########################################################
# Nick Porillo #
# Final Project: Base2 to Base10 and Base16 converter #
# Description: Input base 2, get the base 10 and base 16 #
##########################################################
# Program logic #
# 1) Run program, enter a base2 string #
# 2) Output will be 1 line with base10, 1 with base 16 #
##########################################################
.text
.globl main
main:
li $v0,4 # Print string system call
la $a0,askBase2
syscall
la $a0,b10
li $a1,32 # load 16 as max length to read into $a1
li $v0,8 # 8 is string system call
syscall
jal toBase10 # call subprogram to calculate base 10
jal toBase16 # call subprogram to calculate base 16
j eop # end program
### Base 10 function registers ###
# s0 - user input (base 2 string)#
# s1 - address of b10 space #
# s2 - counter #
# s3 - sum (base 10) #
##################################
toBase10:
la $a0,base10 # print "Base10: "
li $v0,4
syscall
sub $sp,$sp,20 # push registers on stack
sw $s0,0($sp)
sw $s1,4($sp)
sw $s2,8($sp)
sw $s3,12($sp)
sw $ra,16($sp)
move $s0,$a0 # move base 2 to s0
li $s3,0 # initialize sum to 0
li $s2,16 # initialize counter to 16
la $s1,b10 # load base 10 space to s1
nextBit:
lb $s0,($s1) # load next 'byte'
blt $s0,48,printB10 # print sum if s0 < 48
addi $s1,$s1,1 # increment offset
subi $s0,$s0,48 # convert to int value
subi $s2,$s2,1 # decrement counter
beq $s0,0,isZero # branch if byte is 0
beq $s0,1,isOne # branch if byte is 1
isZero:
j nextBit # skip 0 bits
isOne:
li $t8,1 # load 1 as base
sllv $t5,$t8,$s2 # shift left by count: 1 * 2^counter
add $s3,$s3,$t5 # add to sum
j nextBit
printB10:
srlv $s3,$s3,$s2 # shift sum
move $a0,$s3 # load sum
li $v0,1 # print int
syscall
move $s7,$s3 # store base 10 answer in s7
lw $s0,0($sp)
lw $s1,4($sp)
lw $s2,8($sp)
lw $s3,12($sp)
lw $ra,16($sp)
add $sp,$sp,20
jr $ra
### Base 16 function registers ###
# s0 - counter #
# s1 - bit in base 2 answer #
# s2 - base 10 calculation #
# s3 - base 16 answer #
##################################
toBase16:
sub $sp,$sp,20 # push registers onto stack
sw $s0,0($sp)
sw $s1,4($sp)
sw $s2,8($sp)
sw $s3,12($sp)
sw $ra,16($sp)
move $s2,$s7 # load base 10 from s7 into s2
la $a0,base16 # display the string before result
li $v0,4
syscall
li $s0,8 # eight hex digits in word
la $s3,b16 # answer string set up here
loop:
rol $s2,$s2,4 # start with left most digit
and $s1,$s2,0xf # mask one digit in s2 and place results in s1
ble $s1,9,printB16 # is s1 <= 9, if so go to print
add $s1,$s1,7 # if not add 7 to get to A..F
printB16:
add $s1,$s1,48 # add 48 (30 hex) to get ascii character
sb $s1,($s3) # store the byte in result - (s3) points to result
add $s3,$s3,1 # s3++
add $s0,$s0,-1 # s0--
bnez $s0,loop # repeat loop as long as s0 != 0
la $a0,b16 # display result on terminal
li $v0,4
syscall
lw $s0,0($sp) # pop registers from stack
lw $s1,4($sp)
lw $s2,8($sp)
lw $s3,12($sp)
lw $ra,16($sp)
add $sp,$sp,20
jr $ra
eop:
li $v0,10
syscall # end of program
.data
askBase2: .asciiz "Enter a number in base 2: "
b10: .space 16
b16: .space 1
base10: .asciiz "Base10: "
base16: .asciiz "\nBase16: "
################ Sample Output ##################
# Enter a number in base 2: 11010011
# Base10: 211
# Base16: 000000D3
#
# Enter a number in base 2: 1010101
# Base10: 85
# Base16: 00000055
#
# Enter a number in base 2: 11110001Base10: 241
# Base16: 000000F1
#################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment