我知道问题是我正在阅读测试的地址而不是内容的地址,但是我很难弄清楚如何进行测试。 我在火星上汇编
我在下面修复了错误。但是,在我们达到一些风格上的观点之前...
我总是为好评论而鼓掌。您的是每个说明的明确措辞。这是因为您刚刚开始并试图理解每个指令以及它的作用。 但是,好的评论应表现出意图(即)算法在做什么,而不仅仅是模仿指令。指令是“如何”和评论是“什么/为什么”。 so,在我可以诊断和修复您的程序之前,我做的第一件事就是将评论稍微减少一点。另外,我喜欢遵循80列规则,特别是对于ASM
syscall
如果有一条线,真的不需要评论。
在此简化了您的代码,并用注释错误并修复了:
这里是编写程序的一种略微较小的方法:
.data
# make a 4 byte (32 bit) space in memory for a word with address insert_into
# in unused memory store an integer
test: .word 4
Input: .asciiz "\Please Enter a Positive Integer: "
Triangular_Output: .asciiz " is a Triangular Number."
Not_Triangular_Output: .asciiz " is not a Triangular Number: "
.text
main:
la $a0,Input # address of string to print
li $v0,4 # syscall for print string
syscall
# NOTE/BUG: syscall 5 does _not_ need $a0 to be preset and it returns the
# read value in $v0
la $a0,test # get address of test
li $v0,5 # syscall getting an integer from the user
syscall
# here are two ways to save off the value:
move $t0,$v0 # save to a register that won't be clobbered
sw $v0,test # save to memory location
# NOTE/BUG: we do _not_ want the _address_ of test, but rather its
# _contents_ (i.e.) use "lw" instead of "la"
la $a0,test # get address of test
lw $a0,test # get value of test
li $v0,1 # syscall for print integer
syscall
la $a0,Triangular_Output
li $v0,4 # syscall for print string
syscall
li $v0,10 # syscall for program exit
syscall