将十进制转换为二进制间距

时间:2018-09-26 02:01:49

标签: mips

难以正确分隔代码。有人可以指出我正确的方向吗?应该看起来像0000 0000而不是0 0 0 0 0 0 0 0 我想我应该使用ascii空格字符,但是我错误地实现了它。.

.data
the_num:    .asciiz "Enter a number in decimal: "
result_str: .asciiz ""
space: .asciiz          " "


.text
.globl __start

__start:
                # ask and store the first number
li  $v0, 4
la  $a0, the_num
syscall
li  $v0, 5
syscall
move    $a0, $v0

jal print_bin

                    # New Line
li  $v0, 11
li  $a0, 10
syscall

j __start


print_bin:

add $t0, $zero, $a0         # $a0 goes into $t0
add $t1, $zero, $zero
addi $t3, $zero, 1          # mask becomes1
sll $t3, $t3, 7                 # move to postion

addi $t4, $zero, 8          # counter


loop:

and $t1, $t0, $t3          # and the input with the mask
beq $t1, $zero, print          # Branch to print if its 0

add $t1, $zero, $zero          # Zero out $t1
addi $t1, $zero, 1         # Put a 1 in $t1
j print


print:  li $v0, 1
move $a0, $t1
syscall

la  $a0, space      # print space
li  $v0, 4          
syscall

srl   $t3, $t3, 1
addi  $t4, $t4, -1
bne   $t4, $zero, loop

jr  $ra

1 个答案:

答案 0 :(得分:0)

您仅需要在循环中每四次打印一次空格。这样的事情应该起作用:

    andi $t8, $t4, 0x3    # test if $t4 is not a multiple of 4
    bne  $t8, $zero, skip_space
    la  $a0, space        # print space
    li  $v0, 4          
    syscall
skip_space:
相关问题