[英]MIPS 32 showing wrong values and repeat print statements
我在QtSpim中使用MIPS 32汇编,大纲是取三个输入数字,找到两个最大的并显示两个数字以及它们的和,然后显示最大和最小,最后问是否用户想要继续循环成功。 有谁知道为什么我的输出打印语句会像这样翻倍,其中一些没有显示值或错误的值?
编辑:我尝试使用一些跳跃来继续,但似乎不喜欢那样,有什么建议吗? 我认为有些行没有跳过,因为 continue 在下面,所以它们不会被跳过:
largestt0:
li $v0, 1 # print int
add $a1, $t0, $0 # move t0 into a0
syscall # outputs value
j continue
largestt6:
li $v0, 1 # print int
add $a1, $t6, $0 # move t6 into a0
syscall # outputs value
j continue
largestt7:
li $v0, 1 # print int
move $a1, $t7 # move t7 into a0
syscall # outputs value
j continue
这是完整的代码:
################################### Data Segment ###################################
.data
.globl welcome
.globl input
.globl endprogram
.globl sum
.globl continue
.globl largestsum
.globl smallnum
.globl largenum
welcome: .asciiz "\n\nWelcome to the largest 2 sum program\n" #string to print
input: .asciiz "\n\nType in your integer please\n" #string to print
endprogram: .asciiz "\n\nThat was the largest 2sum program!\n" #string to print
sum: .asciiz "\n\n**** Here is your sum ****\n" #string to print
continue: .asciiz "\n\n=== Want to continue? 0 for No ===\n" #string to print
largestsum: .asciiz "\n\nHere is the sum of the following two largest integers input:" #string to print
smallnum: .ascii "\n\nHere is the smallest of the three input integers: " #string to print
largenum: .ascii "\n\nHere is the largest of the three input integers: " #string to print
firstnum: .ascii "\n\nHere is the first largest of the three integers: " #string to print
secondnum: .ascii "\n\nHere is the second of the three integers: " #string to print
sumnum: .ascii "\n\nHere is the sum of the two largest input integers: " #string to print
################################### Code Segment ###################################
.text
.globl main
main:
li $t4, 0 # t4 used with slt checks
li $t3, 1 # check if equal to 1
li $t2, 0 # user input
li $t0, 0 # init t0 to 0
li $t1, 0 # takes in sums of large nums
li $v0, 4 # print_str (sys call 4)
la $a0, welcome # takes the address of
# string as an arg
syscall # output intro message
mainloop:
########### num 1 #####################
li $v0, 4 # print_str (sys call 4)
la $a0, input # takes the address of
# string as an arg
syscall # output intro message
li $v0, 5 # sys code to read integer
syscall # input, $v0 = val to read
add $t0, $v0, $0 # $t0 = input1 int, don't use move
########### num 2 #####################
li $v0, 4 # print_str (sys call 4)
la $a0, input # takes the address of
# string as an arg
syscall # output intro message
li $v0, 5 # sys code to read integer
syscall # input, $v0 = val to read
add $t6, $v0, $0 # $t6 = input2 int, don't use move
########### num 3 #####################
li $v0, 4 # print_str (sys call 4)
la $a0, input # takes the address of
# string as an arg
syscall # output intro message
li $v0, 5 # sys code to read integer
syscall # input, $v0 = val to read
add $t7, $v0, $0 # $t3 = input2 int, don't use move
########### check for smallest, add two largest ###########
########### t0, t6, t7
slt $t4, $t0, $t6 # t4 = 1 if num1 is less than num2
slt $t8, $t0, $t7 # t8 = 1 if num1 is less than num3
beq $t4, $t8, addt6t7 # if t4 = t8 add the other two
slt $t4, $t6, $t0 # t4 = 1 if num2 is less than num1
slt $t8, $t6, $t7 # t8 = 1 if num2 is less than num3
beq $t4, $t8, addt0t7 # if t4 = t8 add the other two
slt $t4, $t7, $t0 # t4 = 1 if num3 is less than num1
slt $t8, $t7, $t6 # t8 = 1 if num3 is less than num2
beq $t4, $t8, addt0t6 # if t4 = t8 add the other two
################# Add t6 + t7 #################
addt6t7:
li $v0, 4 # print_str (sys call 4)
la $a0, largestsum # takes the address of
# string as an arg
syscall # output intro message
li $v0, 4 # print_str (sys call 4)
la $a0, firstnum # takes the address of
# string as an arg
syscall # output intro message
li $v0, 1 # print int
add $a0, $t6, $0 # move t6 into a0
#
syscall # outputs value
li $v0, 4 # print_str (sys call 4)
la $a0, secondnum # takes the address of
# string as an arg
syscall # output intro message
li $v0, 1 # print int
add $a0, $t7, $0 # move t7 into a0
#
syscall # outputs value
add $t1, $t6, $t7 #add two largest into temp t9
li $v0, 4 # print_str (sys call 4)
la $a0, sumnum # takes the address of
# string as an arg
syscall # output intro message
li $v0, 1 # print int
add $a0, $t1, $0 # move t1 into a0
#
syscall # outputs value
#### New Part, add to each ####
li $v0, 4 # print_str (sys call 4)
la $a0, smallnum # takes the address of
# string as an arg
syscall # output intro message
li $v0, 1 # print int
move $a0, $t7 # move t7 into a0
syscall # outputs value value
li $v0, 4 # print_str (sys call 4)
la $a0, largenum # takes the address of
# string as an arg
syscall # output intro message
slt $t4, $t6, $t7 # t4 = 1 if num2 is less than num3
slt $t8, $t7, $t6 # t8 = 1 if num3 is less than num2
beq $t4, $t3, largestt7 # if t4 = 1 then largest is t7, branch t7
beq $t8, $t3, largestt6 # if t4 = 1 then largest is t6, branch t6
################# Add t0 + t7 #################
addt0t7:
li $v0, 4 # print_str (sys call 4)
la $a0, largestsum # takes the address of
# string as an arg
syscall # output intro message
li $v0, 1 # print int
add $a0, $t0, $0 # move t0 into a0
#
syscall # outputs value value
li $v0, 1 # print int
add $a0, $t7, $0 # move t7 into a0
#
syscall # outputs value value
add $t1, $t0, $t7 #add two largest
li $v0, 1 # print int
add $a0, $t1, $0 # move t1 into a0
#
syscall # outputs value value
#### New Part, add to each ####
li $v0, 4 # print_str (sys call 4)
la $a0, smallnum # takes the address of
# string as an arg
syscall # output intro message
li $v0, 1 # print int
add $a0, $t7, $0 # move t7 into a0
#
syscall # outputs value value
li $v0, 4 # print_str (sys call 4)
la $a0, largenum # takes the address of
# string as an arg
syscall # output intro message
slt $t4, $t0, $t7 # t4 = 1 if num1 is less than num3
slt $t8, $t7, $t0 # t8 = 1 if num3 is less than num1
beq $t4, $t3, largestt7 # if t4 = 1 then largest is t7, branch t7
beq $t8, $t3, largestt0 # if t4 = 1 then largest is t0, branch t0
################# Add t0 + t6 #################
addt0t6:
li $v0, 4 # print_str (sys call 4)
la $a0, largestsum # takes the address of
# string as an arg
syscall # output intro message
li $v0, 1 # print int
add $a0, $t0, $0 # move t0 into a0
#
syscall # outputs value
li $v0, 1 # print int
add $a0, $t6, $0 # move t6 into a0
#
syscall # outputs value
add $t1, $t0, $t6 #add two largest into temp t9
li $v0, 1 # print int
add $a0, $t1, $0 # move t1 into a0
#
syscall # outputs value
#### New Part, add to each ####
li $v0, 4 # print_str (sys call 4)
la $a0, smallnum # takes the address of
# string as an arg
syscall # output intro message
add $a0, $t7, $0 # move t7 into a0
li $v0, 1 # print int
syscall # outputs value
li $v0, 4 # print_str (sys call 4)
la $a0, largenum # takes the address of
# string as an arg
syscall # output intro message
slt $t4, $t0, $t6 # t4 = 1 if num1 is less than num2
slt $t8, $t6, $t0 # t8 = 1 if num2 is less than num1
beq $t4, $t3, largestt0 # if t4 = 1 then largest is t6, branch t6
beq $t8, $3, largestt6 # if t4 = 1 then largest is t0, branch t0
################# Each Largest Branch #################
largestt0:
li $v0, 1 # print int
add $a0, $t0, $0 # move t0 into a0
syscall # outputs value
largestt6:
li $v0, 1 # print int
add $a0, $t6, $0 # move t6 into a0
syscall # outputs value
largestt7:
li $v0, 1 # print int
move $a0, $t7 # move t7 into a0
syscall # outputs value
################# Continue? #################
li $v0, 4 # print_str (sys call 4)
la $a0, continue # takes the address of
# string as an arg
syscall # output intro message
li $v0, 5 # sys code to read integer
syscall # input, $v0 = val to read
add $t2, $v0, $0 # $t2 = input int
beq $t2, $0, exit # if t0 == 0 end loop
li $v0, 4 # print_str (sys call 4)
la $a0, welcome # takes the address of
# string as an arg
syscall # output intro message
j mainloop # jump back to the top main
exit:
li $v0, 4 # print_str (sys call 4)
la $a0, endprogram # takes the address of
# string as an arg
syscall # output intro message
li $v0, 10
syscall
我还尝试将 $a0 更改为不同的寄存器,如 $a1、$a2 和 $a3,但这也不起作用:
我需要.asciiz
而不是.ascii
,我错误地认为它以换行符而不是 null 结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.