MIPS使用位移算子在二进制中打印十进制

时间:2017-02-02 19:06:43

标签: assembly mips bit bit-shift

我已经在线阅读了很多有关此主题的线索。关于位移的重要主题(不一定与大会相关,但主题一般是:What are bitwise shift (bit-shift) operators and how do they work?我已经走到了复制和粘贴来自OP的代码:How do I print a binary number with an inputed integer?并制作回复者建议的变化,无论我做什么,我都会继续产生一串零。

我理解什么是比特转换以及它是如何工作的。通过' n'向右移动将数字除以2 ^ n,向左移位将数字乘以2 ^ n。

我有一个实验室应该在上周到期,其中第三部分是提供一个程序,可以接受用户输入,打印出它的二进制版本,然后是十六进制版本。一旦完成,程序就会打印出字符串中心的某些位,并生成其二进制和十六进制版本。

我最初的想法是取字符串,然后用0x01打印出来的' 1'或者' 0',然后将它右移'1'。这包含在一个循环中,并将继续,直到我的柜台遇到了'32'要求并完成它。但我非常困惑为什么它会打印所有的' 0。我尝试过其他版本,例如:

  • 将用户输入左移31,然后对每个位进行循环,但在循环内它向右移动(以弥补相反的位顺序) - 失败 - 全部为零'再次

  • 做与上面相反的事情 - 再次失败,全部为零

我知道我想做的事情是这样的:

(NOT CODE OBVIOUSLY)
User input is: 25 <-- store at $t0 
    Binary rep is: 0000 0000 0000 0000 0000 0000 0001 1001 # 25 in $t0

LOOP:
    AND with 0x01: 0000 0000 0000 0000 0000 0000 0000 0001 #saved to $t1

    Produces:      0000 0000 0000 0000 0000 0000 0000 0001 #saved to $t2

    Print to Console: 1 #send contents of $t2 to syscall

    Shift $t0 Right 1: 0000 0000 0000 0000 0000 0000 0000 1100 #

j LOOP (until the beq branch was met and I left the loop)

我原本以为这会奏效。即使它向后产生了结果,我想我仍然会在我生成的字符串中收到我会注意到并相应处理的字母(将$t0中剩下的整个数字移​​到&#39; 31&#39;然后执行上述说明。仍然是所有&#39; 0&#39;

这是我的代码。再一次,它通过我的无数次尝试和变化而变得腐败。我只是想要一些帮助来理解我上面强调的方法是否完全偏离基础以及我可以对下面的示例代码做些什么来解决这个问题。

了解此作业已经上交,但由于我的困惑,我没有完成。我希望进一步了解这一点。我没有完成整个代码,因为我对这部分感到难过。所以我只是要求完成这项任务的第一部分。

#Read integer A from user and store it into a register
#Display the integer in binary
#Display the integer in Hex
#set Register $a0 to contain only bits 12,13,14,15 of $a0
#Display the integer in binary contained in $a0
#Display the integer in hex contained in $a0

.data

    userInput: .asciiz "Please enter your integer:\n"
    binaryInput: .asciiz "Here is the input in binary: "
    nl: .asciiz "\n\n"
    hexInput: .asciiz "Here is the input in hexidecmal: "
    binaryOutput: .asciiz "Here is the output in binary: "
    hexOutput: .asciiz "Here is the output in hexidecimal: "

.text
    main:

    #ask end-user to submit an integer value
    li $v0, 4
    la $a0, userInput
    syscall

    #read user-input
    li $v0, 5
    syscall

    #enter user input into $t0
    move $t0, $v0
    add $t1, $zero, $zero       #counter
    addi $t2, $zero, 32     #target
    sll $s1, $t0, 31        #shift left number 31 bits to s1
    li $v0, 4
    la $a0, binaryInput     #print out string to user
    syscall


loop:
    andi $s2, $s1, 1        #and 0x01 with s1 to s2
    srl $s1, $s1, 1         #shift right s1 by 1 bit
    li $v0, 1
    la $a0, ($s2)           #print digit held in s2 to screen
    syscall
    add $t1, $t1, 1         #add 1 to counter
    bne $t1, $t2, loop      #check if counter is equal to target, if not continue loop

    #exit the program 
    li $v0, 10
    syscall

1 个答案:

答案 0 :(得分:2)

这是一种与您的基本方法略有不同的方法。它将二进制输出和十六进制输出视为公共输出函数的参数。位屏蔽/位移类似,但屏蔽和位宽是可变的。

# Read integer A from user and store it into a register
# Display the integer in binary
# Display the integer in Hex
# set Register $a0 to contain only bits 12,13,14,15 of $a0
# Display the integer in binary contained in $a0
# Display the integer in hex contained in $a0

    .data
userInput:  .asciiz     "Please enter your integer: "
binaryInput:    .asciiz "Here is the input in binary: "
nl:         .asciiz     "\n"
hexInput:   .asciiz     "Here is the input in hexadecimal: "
binaryOutput:   .asciiz "Here is the output in binary: "
hexOutput:  .asciiz     "Here is the output in hexadecimal: "
hexDigit:   .asciiz     "0123456789ABCDEF"
obuf:       .space      100
obufe:

    .text
    .globl  main
main:
    # ask end-user to submit an integer value
    li      $v0,4
    la      $a0,userInput
    syscall

    # read user-input
    li      $v0,5
    syscall
    move    $s0,$v0

    # output original in binary
    la      $a0,binaryInput
    li      $a1,32
    jal     prtbin

    # output original in hex
    la      $a0,hexInput
    li      $a1,32
    jal     prthex

    # isolate bits 12,13,14,15
    srl     $s0,$s0,12
    andi    $s0,$s0,0x0F

    # output isolated in binary
    la      $a0,binaryOutput
    li      $a1,4
    jal     prtbin

    # output isolated in hex
    la      $a0,hexOutput
    li      $a1,4
    jal     prthex

    # exit the program
    li      $v0,10
    syscall

# prtbin -- print in binary
#
# arguments:
#   a0 -- output string
#   a1 -- number of bits to output
prtbin:
    li      $a2,1                   # bit width of number base digit
    j       prtany

# prthex -- print in binary
#
# arguments:
#   a0 -- output string
#   a1 -- number of bits to output
prthex:
    li      $a2,4                   # bit width of number base digit
    j       prtany

# prtany -- print in given number base
#
# arguments:
#   a0 -- output string
#   a1 -- number of bits to output
#   a2 -- bit width of number base digit
#   s0 -- number to print
#
# registers:
#   t0 -- current digit value
#   t5 -- current remaining number value
#   t6 -- output pointer
#   t7 -- mask for digit
prtany:
    li      $t7,1
    sllv    $t7,$t7,$a2             # get mask + 1
    subu    $t7,$t7,1               # get mask for digit

    la      $t6,obufe               # point one past end of buffer
    subu    $t6,$t6,1               # point to last char in buffer
    sb      $zero,0($t6)            # store string EOS

    move    $t5,$s0                 # get number

prtany_loop:
    and     $t0,$t5,$t7             # isolate digit
    lb      $t0,hexDigit($t0)       # get ascii digit

    subu    $t6,$t6,1               # move output pointer one left
    sb      $t0,0($t6)              # store into output buffer

    srlv    $t5,$t5,$a2             # slide next number digit into lower bits
    sub     $a1,$a1,$a2             # bump down remaining bit count
    bgtz    $a1,prtany_loop         # more to do? if yes, loop

    # output string
    li      $v0,4
    syscall

    # output the number
    move    $a0,$t6                 # point to ascii digit string start
    syscall

    # output newline
    la      $a0,nl
    syscall

    jr      $ra                     # return