I'm taking a computer architecture class, and they're having us use MIPS assembly. Our current assignment is asking us to take a user-inputted hex string, and turn it into a binary one. The code I've written "works", but it's horrible, to say the least. What would be a better approach to doing this? I need the binary-converted string in a variable, and this is the only way I was able to do it:
### THIS CODE IS AWFUL ###
.data
inputPrompt: .asciiz "Please enter a 32-bit hexadecimal integer: "
outputMsg: .asciiz "\nConverted Hexadecimal String: "
hexInput: .space 9 # Enough space for 8 digit hex number
binaryConvertedStr: .space 33 # Enough space for a 32 digit binary number
var0: .asciiz "0000"
var1: .asciiz "0001"
var2: .asciiz "0010"
var3: .asciiz "0011"
var4: .asciiz "0100"
var5: .asciiz "0101"
var6: .asciiz "0110"
var7: .asciiz "0111"
var8: .asciiz "1000"
var9: .asciiz "1001"
varA: .asciiz "1010"
varB: .asciiz "1011"
varC: .asciiz "1100"
varD: .asciiz "1101"
varE: .asciiz "1110"
varF: .asciiz "1111"
sym0: .word 0x00000030
sym1: .word 0x00000031
sym2: .word 0x00000032
sym3: .word 0x00000033
sym4: .word 0x00000034
sym5: .word 0x00000035
sym6: .word 0x00000036
sym7: .word 0x00000037
sym8: .word 0x00000038
sym9: .word 0x00000039
symA: .word 0x00000041
symB: .word 0x00000042
symC: .word 0x00000043
symD: .word 0x00000044
symE: .word 0x00000045
symF: .word 0x00000046
.text
main: li $v0, 4
la $a0, inputPrompt
syscall # Displays a message prompting user for 8 digit hex number
li $v0, 8
la $a0, hexInput
li $a1, 9
syscall # Stores user-inputted 8 digit hex number into "hexInput"
la $t0, hexInput # Loads address of hexInput into t0
la $t1, binaryConvertedStr # Loads address of (pre-converted) binaryConvertedStr into t1
findChar:
lbu $s0, ($t0) # Loads Nth byte of t0 (hexInput) into s0
beqz $s0, Done # Checks if hexInput's null terminator has been reached
## LONG, TERRIBLE WAY OF CHECKING WHAT EACH INTEGER OF hexInput IS. JUMPS TO writeToConverted IF FOUND. ##
la $t9, sym0
lbu $t8, ($t9)
la $t2, var0
beq $s0, $t8, writeToConverted
la $t9, sym1
lbu $t8, ($t9)
la $t2, var1
beq $s0, $t8, writeToConverted
la $t9, sym2
lbu $t8, ($t9)
la $t2, var2
beq $s0, $t8, writeToConverted
la $t9, sym3
lbu $t8, ($t9)
la $t2, var3
beq $s0, $t8, writeToConverted
la $t9, sym4
lbu $t8, ($t9)
la $t2, var4
beq $s0, $t8, writeToConverted
la $t9, sym5
lbu $t8, ($t9)
la $t2, var5
beq $s0, $t8, writeToConverted
la $t9, sym6
lbu $t8, ($t9)
la $t2, var6
beq $s0, $t8, writeToConverted
la $t9, sym7
lbu $t8, ($t9)
la $t2, var7
beq $s0, $t8, writeToConverted
la $t9, sym8
lbu $t8, ($t9)
la $t2, var8
beq $s0, $t8, writeToConverted
la $t9, sym9
lbu $t8, ($t9)
la $t2, var9
beq $s0, $t8, writeToConverted
la $t9, symA
lbu $t8, ($t9)
la $t2, varA
beq $s0, $t8, writeToConverted
la $t9, symB
lbu $t8, ($t9)
la $t2, varB
beq $s0, $t8, writeToConverted
la $t9, symC
lbu $t8, ($t9)
la $t2, varC
beq $s0, $t8, writeToConverted
la $t9, symD
lbu $t8, ($t9)
la $t2, varD
beq $s0, $t8, writeToConverted
la $t9, symE
lbu $t8, ($t9)
la $t2, varE
beq $s0, $t8, writeToConverted
la $t9, symF
lbu $t8, ($t9)
la $t2, varF
beq $s0, $t8, writeToConverted
writeToConverted: # Writes the converted hex digit into binary, then writes that binary string to binaryConvertedStr
lbu $s1, ($t2) # Loads next byte of var(N) into s1
beqz $s1, increment # Checks if var(N)'s null terminator has been reached; if so, then jumps back to findChar after incrementing
# t0 (hexInput's address) by 1
sb $s1, ($t1) # Writes s1 (byte of var(N)) to t1 (binaryConvertedStr)
addi $t1, $t1, 1 # Increments the address-to-be-edited of t1 (binaryConvertedStr)
addi $t2, $t2, 1 # Increments the address-to-be-read of t2 (var(N))
j writeToConverted # Loops back around until the null terminator of var(N) is found
increment: # Increments t0 (hexInput's address) by 1
addi $t0, $t0, 1
j findChar # Loop back to findChar
Done: # Tells the user what their string in binary is.
li $v0, 4
la $a0, outputMsg
syscall
li $v0, 4
la $a0, binaryConvertedStr
syscall
li $v0, 10
syscall
User contributions licensed under CC BY-SA 3.0