【问题标题】:Assembly IF statements, multiple conditions triggering [duplicate]汇编 IF 语句,多个条件触发 [重复]
【发布时间】:2021-11-11 18:37:46
【问题描述】:

我正在为一项任务开发“Craps”(一种骰子游戏),但我对代码中的一件事感到困惑。我试图弄清楚为什么当 beq if 语句触发时,win 提示和 lost 提示都会触发。在掷骰子的情况下,如果滚动玩家掷出 7 或 11,他们就赢了,并且不知何故,获胜条件也会触发代码底部的失败条件提示。

感谢您的帮助

.text
main:

li $v0, 4                               # Load system call code 4 into register $v0; System call code 4 represents print_string.
la $a0, heading                         # Load address of string to print.
syscall                                 # Perform system call.
        
# Prompt for the integer to enter
li $v0, 4
la $a0, prompt
syscall

# Read the integer and save it in $s0
li $v0, 5
syscall
move $s0, $v0

# Output the text
li $v0, 4
la $a0, output
syscall

# Output the value
li $v0, 1
move $a0, $s0
syscall





li $t0, 3 # t0 is a constant 10
li $t5, 0 # t1 is our counter (i)
loop:
beq $t5, $s0, end # if t1 == 10 we are done





##################body of loop
    # Output the text
li $v0, 4
la $a0, rng
syscall


.text
    li $a1, 6  #Here you set $a1 to the max bound.
    li $v0, 42  #generates the random number.
    syscall

    add $a0, $a0, 1  #Here you add the lowest bound
    li $v0, 1   #1 print integer

    syscall
   
    move $t1, $a0
   
       # Output the text
li $v0, 4
la $a0, registerMoved
syscall

# Output the value
li $v0, 1
move $a0, $t1
syscall

    # Output the text
li $v0, 4
la $a0, rng
syscall

#generation of second random number
    li $a1, 6  #Here you set $a1 to the max bound.
    li $v0, 42  #generates the random number.
    syscall

    add $a0, $a0, 1  #Here you add the lowest bound
    move $t2, $a0

    syscall
   
# Output the value
li $v0, 1
move $a0, $t2
syscall
   
       # Output the text
li $v0, 4
la $a0, registerMoved
syscall

# Output the value
li $v0, 1
move $a0, $t2
syscall

add $t3, $t1, $t2

       # Output the text
li $v0, 4
la $a0, combined
syscall

li $v0, 1
move $a0, $t3
syscall

li $t1, 7

 beq $t1, $a0, WIN  # if(input==7 output win)
li $t2, 11






###### end of body of loop



addi $t5, $t5, 1 # add 1 to t1
j loop # jump back to the top
end:


 # Exit the program
li $v0, 10
syscall

.text

WIN: 

   # Output the text
li $v0, 4
la $a0, winString
syscall


END_WIN:

.text
LOSE: 
   # Output the text
li $v0, 4
la $a0, loseString
syscall


END_LOSE:
    
.data
heading: .asciiz "Welcome to the game of craps\n"
registerMoved: .asciiz "\nThe register was copied to another address: "
prompt: .asciiz "Enter a number of rolls: "
die_roll: .asciiz "The dice roll is: "
win: .asciiz "You have won"
loss: .asciiz "You have lost"
output: .asciiz "\n You entered for the number of rolls :"
rng: .asciiz "\nThe number generated was: "
combined: .asciiz "\nThe combined value is: "
winString: .asciiz "The rolling player wins "
loseString: .asciiz "The rolling player loses "```

【问题讨论】:

    标签: assembly mips


    【解决方案1】:

    标签不会改变执行流程。插入一个名为END_WIN: 的标签绝不会导致函数或块“结束”;它只是将当前地址与一个从未使用过的名为END_WIN 的符号相关联。在WIN 之后的系统调用之后,执行落入LOSE,我怀疑这不是你想要的。

    如果你想执行到别的地方,你需要显式地写一个跳转指令。

    【讨论】:

    • 如果方法包含在标签中,为什么两个方法都被触发?我认为 : 在一个字符串之后使它成为一个可以调用和单独调用的方法,而不是两个方法
    • 不,标签不是方法。汇编不是高级语言,没有像“方法”这样的 HLL 概念。汇编程序只是一系列指令,它们按顺序执行,直到其中一条指令成为分支指令。而标签实际上只是一个标签 - 代码中特定位置的人类可读名称,如果需要,您可以跳转到。
    • 您的帮助。如何制作仅显示文本 A 的条件和显示文本 B 的单独条件(如果 7 或 11 玩家获胜,否则玩家输了,实际规则稍微复杂一些,但我正在尝试掌握条件语法跨度>
    • @nbulgarides:我们已经有一些其他的副本用不同的词来解释它,比如MIPS conditional printing all cases / If else in MIPS / MIPS if-else with printing trouble。花点时间理解其中一个或多个示例应该会更清楚。
    • @nbulgarides:在代码中的每一点,如果你不想继续下一条指令,你必须考虑你想去哪里,然后跳到那里。在这种情况下,听起来像是在输出win消息后,您想退出程序。您已经在end 标签处编写了执行此操作的代码,因此您可以在syscall 之后简单地写j end
    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 2016-12-13
    • 2013-12-17
    • 1970-01-01
    • 2014-04-12
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多