[英]Printing register value in Mips assembly
I have the following code and I am trying to print an int value:我有以下代码,我正在尝试打印一个 int 值:
addi $t0, $v0, 0 # add immediate: copy returned value to $t0 by adding 0
li $v0, 1 # syscall 1 (print_int)
sw $t0, 0($a0) # store word: supply argument: recorded return value from $t0
syscall # syscall: print the int
But it gives error at runtime, how could I fix it?但它在运行时出错,我该如何解决?
$a0 should contain the value to print, not the pointer to it. $a0 应该包含要打印的值,而不是指向它的指针。 Instead of addi/sw, assign $v0 to $a0.
将 $v0 分配给 $a0,而不是 addi/sw。 I'm assuming $v0 has initially the value to print;
我假设 $v0 最初具有打印价值; it's not obvious from the question.
从问题上看并不明显。
Meanwhile, $v0 should have the syscall number, 1 in this case.同时,$v0 应该有系统调用号,在本例中为 1。
Since you probably don't initialize $a0 to a valid memory address, the program tries to write to a bogus memory location and predictably crashes.由于您可能没有将 $a0 初始化为有效的内存地址,因此程序会尝试写入一个虚假的内存位置并可能会崩溃。
you can just move that value in $a0
你可以在
$a0
移动该值
li $t0,13
li $v0,1
move $a0,$t0
syscall
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.