如何打印带有输入整数的二进制数? [英] How do I print a binary number with an inputed integer?
问题描述
我正在尝试将输入的整数转换为二进制,但似乎无法在程序中找到错误.我能够加载输入的数字以及打印出8位二进制数字,但是1都没有被打印.我究竟做错了什么?
I'm trying to convert a inputed integer to binary and I cannot seem to find the error within my program. I'm able to load the inputed number as well as print out a 8 digit binary number, however none of the 1's are being printed. What am I doing wrong?
enterNum:
.asciiz "Enter your number (base 10): "
printBaseTwo:
.asciiz "The number in base 2 is: "
#-----------------------------------------------------------------------
.text
#Print out string, collect intger input
main: li $v0, 4
la $a0, enterNum
syscall
li $v0, 5
syscall
move $t0, $v0
#create mask/print out the second string and prepare to print out binary
mask:
andi $t1, $zero, 1
sll $t1, $t1, 7
addi $t2, $zero, 8
li $v0, 4
la $a0, printBaseTwo
syscall
# compares mask to integer, starting at the most sig place
# if the mask is zero, print out zero
loop:
and $t3,$t0, $t1
beq $t3, $zero, print
add $t3, $zero, $zero
addi $t3, $zero, 1
j print
print:
#prepares to print integer in $a0
li $v0, 1
# moves either 1 or 0 into $a0 to be printed
move $a0, $t3
syscall
# shifts over right 1, getting closer to 0
srl $t1, $t1, 1
#lowers count
addi $t2, $t2, -1
#loop back to beginning if not finished printing binary Num
bne $t2, $zero, loop
beq $t2, $zero, exit
exit:
li $v0, 10
syscall
推荐答案
我要从32位MIPS版本开始回答这个问题.
I'm answering this question starting from a 32-bit MIPS version.
首先,当要求十进制数时,MIPS将其保存为32位数字在其寄存器中.因此,我做出了以下判断:
First of all, when asking for a decimal number, MIPS saves this as a 32-bit number in its registers. Therefore I did the following adjusments:
andi $t1, $zero, 1
sll $t1, $t1, 7
addi $t2, $zero, 8
成为:
add $t1, $zero, 1
sll $t1, $t1, 31
addi $t2, $zero, 32
注意如何将andi
更改为addi
.如果您使用的是8位版本的MIPS,则这是唯一必要的调整.
Notice how I changed the andi
into addi
. If you were working in an 8-bit version of MIPS, this would be the only necessary adjustment.
希望我能解决您的问题,它可以在此处的MARS模拟器中正常工作!
I hope I solved your question, it works in the MARS-simulator overhere!
这篇关于如何打印带有输入整数的二进制数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!