Why is the upper and lower bound not resetting properly after giving L or H, so the computer guesses are off. MIPS code

时间:2018-09-18 19:56:40

标签: mips

.data
prompt : .asciiz "Enter the secret number : "
prompt2 : .asciiz "\nComputer guess is : "
higherLowerOrCorrect : .asciiz "\nNumber is higher (h) lower (l) or correct/exit (x) : "
.text

li $v0,4
la $a0,prompt #it will print prompt for year
syscall

li $v0,5
syscall #wait for user input
move $t2,$v0

li $t7,100 #higher bound
li $t6,0 #lower bound
li $t5,0 #stores guess

loop :


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

add $a0,$a0,$t6
move $t5,$a0

li $v0,4
la $a0,prompt2 #it will print prompt for year
syscall

move $a0,$t5
li $v0, 1 #1 print integer
syscall

li $v0, 4
la $a0, higherLowerOrCorrect
syscall

li $v0, 12 #GET CHARACTER
syscall

beq $v0,'l',setHigherBound #IF Y DO THE LOOP
beq $v0,'h',setLowerBound #IF Y DO THE LOOP
beq $v0,'x',exit #IF Y DO THE LOOP


setHigherBound:
move $t7,$t5
j loop
setLowerBound:
add $t7,$t7,$t6
sub $t7, $t7,$t5
move $t6,$t5
j loop

exit:

output:

1 个答案:

答案 0 :(得分:0)

您的setHigherBound分支未考虑当前下限可能为非零。而不是move $t7,$t5应该是sub $t7,$t5,$t6