簡體   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