############################## # # Binary to decimal converter # ############################## .data .align 2 prompt: .asciiz "\nInput> " output: .asciiz "\nDecimal = " badinput: .asciiz "\nBad input\n" binin: .space 64 .text .globl main main: la $a0,prompt # Load first prompt li $v0,4 # Used in syscall for printing strings syscall li $a1,64 la $a0,binin # Store binary input number li $v0,8 # Used in syscall for storing strings syscall atoi: la $t0,binin # Store address of binin in $t0 li $t3,0 # Load 0 into $t3 lb $t1,0($t0) # Load byte into $t1 bne $t1,'1',loop # Do nothing if input is positive li $t3,-1 # Negative input; set everything to 1 loop: beq $t1,'\n',final # Check for end of string blt $t1,'0',inputerr # Make sure value is >= 0 bgt $t1,'1',inputerr # Make sure value is <= 1 addi $t2,$t1,-48 # Store value of digit in $t2 sll $t3,$t3,1 # Shift one bit to multiply by 2 add $t3,$t3,$t2 # Add digit to current total addi $t0,$t0,1 # Move pointer to next byte lb $t1,0($t0) # Load the next byte and loop b loop inputerr: # Display an error and jump la $a0,badinput # back to beginning li $v0,4 syscall j main final: move $a0,$t3 li $v0,1 syscall j main Exit: li $v0,10 syscall