繁体   English   中英

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

[英]Mips Assembly Language using bit manipulation to check if int is less than 32 & displays 0 if so, otherwise (32 or higher) displays 1

检查用户输入的int是否在0到255(包括)之间小于32,如果是,则显示0,否则显示32(或更高)。

我不知道如何使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行代码,仅涉及以下内容:-syscall

  • syscall支持说明(例如:li加载$ v0)
  • 制作保存副本的指令
  • 位操作指令(“与”,“或”,“异或”,“或非”和移位-仅需要的内容)

      li $v0, 10 # exit syscall 

您需要一个if statement来确定用户输入的值是小于还是大于31。您的代码有太多的错误,因此我在此处编写了一个简单的代码,可以在您的代码中使用它,或者向其中添加一些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
    # 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM