简体   繁体   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

Checks if a user-entered int between 0 & 255 (inclusive) is less than 32 & displays 0 if so, otherwise (32 or higher) displays 1. 检查用户输入的int是否在0到255(包括)之间小于32,如果是,则显示0,否则显示32(或更高)。

I can't figure out how to make 64 and 128 not show up as 0. 我不知道如何使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

Write NO MORE THAN 14 lines of code that involve using ONLY the following: - syscall 编写不超过14行代码,仅涉及以下内容:-syscall

  • syscall supporting instructions (eg: li to load $v0) syscall支持说明(例如:li加载$ v0)
  • instruction to make a saved copy 制作保存副本的指令
  • bit manipulating instructions (ANDing, ORing, XORing, NORing and shifting - only whatever that are needed) 位操作指令(“与”,“或”,“异或”,“或非”和移位-仅需要的内容)

      li $v0, 10 # exit syscall 

You need an if statement in order to making decision if the value entered by user is less or bigger than 31. Your code has too many bugs so i wrote a simple code here that you can use in your code, or add some print string to that. 您需要一个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