Mips汇编语言使用位操作来检查int是否小于32&如果是,则显示0,否则(32或更高)显示1

时间:2017-03-01 07:15:14

标签: assembly bit-manipulation mips

检查用户输入的int是否介于0和0之间。 255(含)小于32   &安培;如果是,则显示0,否则(32或更高)显示1。

我无法弄清楚如何使64和128不显示为0.

.data
legend1:    .asciiz "0: less than 32\n"
legend2:    .asciiz "1: 32 or higher\n"
inPrompt:   .asciiz "Enter an integer between 0 and 255: "
outLab:     .asciiz "It is "

    .text
    .globl main
main:
    li $v0, 4
    la $a0, legend1        
    syscall                   # print legend line 1
    la $a0, legend2        
    syscall                   # print legend line 2
    la $a0, inPrompt        
    syscall                   # print input prompt

    li $v0, 5
    syscall                   # read input integer

    move $t0, $v0       #stores input integer into $t0 and prints outLab
    li $v0, 4
    la $a0, outLab
    syscall

    andi $t3, $t0, 0x031    #and'ing the input integer and masking number
    li $v0, 1       #code to print integer
    move $a0, $t4       #move t3 value into argument
    syscall

写入不超过14行涉及使用的代码 仅限以下内容: - 系统调用

  • 系统调用支持说明(例如:li to load $ v0)
  • 制作保存副本的说明
  • 位操作指令(ANDing,ORing,XORing, 挪威和转移 - 只需要任何需要的东西)

            li $v0, 10               # exit
            syscall
    

2 个答案:

答案 0 :(得分:0)

如果用户输入的值小于或大于31,您需要if statement才能做出决定。 你的代码有太多的bug,所以我在这里写了一个简单的代码,你可以在你的代码中使用,或者为它添加一些print string

.text
main:
li       $v0,5          #read user input
syscall
move     $t0,$v0        #t0 = user input

addi     $t1,$zero,32   #t1 = 32

bgt      $t0,$t1,L1     # branch to L1 if t0 > t1
nop
addi     $v0,$0,0       # v0 = 0
b        endif
L1:
addi     $v0,$0,1       # v0 = 1

endif:
move     $a0,$v0        # a0 = 1 or 0 depend on the v0 

li       $v0,1          # print it out
syscall         

li       $v0,10         #exit
syscall

答案 1 :(得分:0)

    # prepare the three significant bits b5-b7 to lowest bit position
    srl   $t0, $v0, 5    # t0.b0 = v0.b5 (div 32)
    srl   $t1, $v0, 6    # t1.b0 = v0.b6 (div 64)
    srl   $a0, $v0, 7    # a0.b0 = v0.b7 (div 128)
    # compose all three significant bits into single a0.b0 bit:
    or    $a0, $a0, $t0
    or    $a0, $a0, $t1  # a0.b0 = (b5 | b6 | b7)
    andi  $a0, $a0, 0x1  # mask out only the resulting b0 (0/1 result)
    # here a0 = 0/1 for v0 = 0..31/32..255