程序的要求与我的解决方案一起发布在下面的代码中。有人可以告诉我我做错了什么,告诉我如何解决吗?我输入的每个输入结果始终为0。在大多数情况下,我相信我掩盖了错误,而不是错误地移位了位。
##########################################################################
############################
# prompt user to enter an integer, read the integer, and display a 0 if the bits at the
# 16 and 256 place value positions of the integer are both 1 and display a 1 otherwise
############################ data segment ################################
.data
outputLegend1: .asciiz "0 = both bits at 16 & 256 place value positions are 1\n"
outputLegend2: .asciiz "1 = bits at 16 & 256 place value positions NOT both 1\n\n"
inputPrompt: .asciiz "Enter integer: "
outputLabel: .asciiz "Integer entered is of type "
############################ code segment ################################
.text
.globl main
main:
li $v0, 4
la $a0, outputLegend1
syscall # print output legend part 1
la $a0, outputLegend2
syscall # print output legend part 2
la $a0, inputPrompt
syscall # print integer prompt
li $v0, 5
syscall # read integer
move $t0, $v0 # save integer read in $t0
li $v0, 4
la $a0, outputLabel
syscall # print output label
li $v0, 1
##########################################################
# Insert NO MORE THAN 6 lines of code that involve ONLY
# bit manipulating instructions (ANDing, ORing, XORing,
# NORing and shifting - only whatever that are needed)
# so that the program will work just like the sample runs
# shown at the bottom (some blank lines edited out).
#my solution
sll $t1,$t1,4
andi $t2,$t1,1
sll $t2,$t2,4
andi $t3,$t2,1
xor $a0, $t2,$t3
syscall # display desired output
##########################################################
li $v0, 10 # exit gracefully
syscall
########################## sample test runs ##############################
# 0 = both bits at 16 & 256 place value positions are 1
# 1 = bits at 16 & 256 place value positions NOT both 1
#
# Enter integer: 0
# Integer entered is of type 1
# -- program is finished running --
#
# Reset: reset completed.
#
# 0 = both bits at 16 & 256 place value positions are 1
# 1 = bits at 16 & 256 place value positions NOT both 1
#
# Enter integer: 16
# Integer entered is of type 1
# -- program is finished running --
#
# Reset: reset completed.
#
# 0 = both bits at 16 & 256 place value positions are 1
# 1 = bits at 16 & 256 place value positions NOT both 1
#
# Enter integer: 256
# Integer entered is of type 1
# -- program is finished running --
#
# Reset: reset completed.
#
# 0 = both bits at 16 & 256 place value positions are 1
# 1 = bits at 16 & 256 place value positions NOT both 1
#
# Enter integer: 272
# Integer entered is of type 0
# -- program is finished running --
#
# Reset: reset completed.
#
# 0 = both bits at 16 & 256 place value positions are 1
# 1 = bits at 16 & 256 place value positions NOT both 1
#
# Enter integer: 12349876
# Integer entered is of type 0
# -- program is finished running --
#
# Reset: reset completed.
#
# 0 = both bits at 16 & 256 place value positions are 1
# 1 = bits at 16 & 256 place value positions NOT both 1
#
# Enter integer: 12346789
# Integer entered is of type 1
# -- program is finished running --
#
######################## end sample test runs ############################
您的想法正确(但有很多错别字,所以继续吧!
$t1
中没有什么有趣的东西,当您第一次移动它时。我认为您最初想将$t0
移到$t1
,所以您仍然具有用户输入的原始值,以便以后进行操作。
观看您的注册用法:
使用单步验证,以确保您在每条指令后都得到了期望的中间值。如果不是,则检查该指令是否正在寻找正确的寄存器,或者源寄存器被先前的指令清除了!
此外,当您想向右移动时,您正在向左移动...
您只是在猜测何时使用什么寄存器。因此,这是一种可以帮助您摆脱因不熟悉组装而引起的混乱的方法。
尝试用C编写代码[[[使用Three address code-)—这项工作不需要花很多,大约5行!但这将帮助您了解何时使用哪个变量。 (确保您的C三个地址代码有效—在某个地方测试和调试它,如有必要,请使用在线C编译器。)
接下来,在编写任何指令之前,将所有这些C(TAC)变量分配(创建映射)到MIPS寄存器。
最后,使用变量到寄存器的映射,编写尊重/模仿这三个地址代码的汇编指令。