Mips 汇编语言使用位操作来检查 int 是否小于 32,如果是则显示 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
检查用户输入的介于 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 加载 $v0)
- 制作保存副本的说明
位操作指令(ANDing, ORing, XORing,
NORing 和移位 - 只需要任何东西)
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,[=10=],0 # v0 = 0
b endif
L1:
addi $v0,[=10=],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
检查用户输入的介于 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 加载 $v0)
- 制作保存副本的说明
位操作指令(ANDing, ORing, XORing, NORing 和移位 - 只需要任何东西)
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,[=10=],0 # v0 = 0
b endif
L1:
addi $v0,[=10=],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