In: Computer Science
All in MIPS assembly
1. Have the user input a 32 bit binary string
2. convert that into decimal
3. Print the decimal
.data
msg1: .asciiz
msg2: .asciiz
buffer: .space
.text
main:
li $v0, 4
la $a0 ,msg1
syscall
li $v0, 8
la $a0 ,buffer
li $a1, 33
move $t2 , $a0
syscall
li $s1, 32
li $s2, 30
biToDecLoop:
li $t0 , '0'
li $t1, '1'
lb $t3, ($t2)
beq $t3, $t0, binaryZero
beq $t3, $t1, binaryOne
binaryZero:
subi $s2. $s2, 1
j biToDecNext
binaryOne
add $t3,$t3,1
mul $t3,$t3,2
subi $s2,$s2,1
bne $s2, $zero, binaryOne
move $t4, $t3
add $t5, $t5,$t4
li $t0,32
li $t1,33
beq $s1, $t0, addTwo
beq $s1, $t1, addOne
j biToDecNext
addTwo: # Check if there is a 1 in the second to last index of string
add $t5, $t5, 2 # Add 2 to total ($t5)
j biToDecNext
addOne: # Check if there is a 1 in the last index of string
add $t5, $t5, 1 # Add 1 to total ($t5)
j biToDecNext
biToDecNext:
add $t2, $t2, 1
subi $s1, $s1, 1 # Decrement outer loop counter
bne $s1, $zero, biToDecLoop # Loop until outer counter reaches zero
end:
li $v0, 4
la $a0, newLine
syscall
li $v0, 4
la $a0, msg2
syscall
#move $a0, $t5
li $v0, 1
la $a0 ($t5)
syscall
li $v0, 10
syscall
: