使用MIPS汇编语言输入二进制转化成10进制输出
##############################
# Register Usege in Main:
# $a0 : number
# $t1 : save '\n'
# $t2 : save a char
# $t3 : buffer pointer
# $t4 : save '0'
# $t5 : save '1'
###############################
.data
binbuf: .space 33
error: .asciiz "wrong input"
.text
li $v0,8
la $a0,binbuf
li $a1,33
syscall
li $a0,0x0a #0x0a = 10 ,means '\n'
li $v0,11
syscall
li $t1,0x0a
li $t4,0x30 #0x30 is '0'
li $t5,0x31 #0x31 is '1'
la $t3,binbuf
move $a0,$0
redo:
lb $t2,0($t3)
beq $t2,0x0a,finish #if $t2 = '\n' or space ,means the end of the string
beq $t2,$0,finish
blt $t2,$t4,err #if $t2 <'0' or > '1' ,the input is invalid
bgt $t2,$t5,err
addi $t2,$t2,-0x30 #transfer the char to int
sll $a0,$a0,1 #$a0 << 1
add $a0,$a0,$t2
addi $t3,$t3,1 #binbuf pointer + 1
j redo
err:
la $a0,error
li $v0,4
syscall
li $v0,10
syscall
finish:
li $v0,1
syscall
li $v0,10
syscall