如何使用MIPS汇编语言获取计算所需的时间?
所以现在我想计算一下计算所需的时间。我在$v0中使用30,但问题是每次打印所花费的时间,都会显示一个不相关的巨大数字。那么我如何解决这个问题呢? 谢谢你的帮助 示例代码如何使用MIPS汇编语言获取计算所需的时间?,mips,Mips,所以现在我想计算一下计算所需的时间。我在$v0中使用30,但问题是每次打印所花费的时间,都会显示一个不相关的巨大数字。那么我如何解决这个问题呢? 谢谢你的帮助 示例代码 .data inputNumber: .asciiz "Input an integr: " newLine: .asciiz "\n" done1: .asciiz "loop completed\n" .text main: #print inputNmber string li $v0, 4 la $
.data
inputNumber: .asciiz "Input an integr: "
newLine: .asciiz "\n"
done1: .asciiz "loop completed\n"
.text
main:
#print inputNmber string
li $v0, 4
la $a0, inputNumber
syscall
#read integer
li $v0, 5
syscall
#move int to another register
move $t0, $v0
# get starting time
li $v0, 30
syscall
# store it in another place
move $a0, $v0
#while loop counter
addi $t1, $zero, 1
# while loop
while:
bgt $t1, $t0, done
#print inputNmber int
li $v0, 1
move $a0, $t0
syscall
#print new line
li $v0, 4
la $a0, newLine
syscall
addi $t1, $t1, 1
j while
#Exit the program
li $v0, 10
syscall
done:
# get finishing time
li $v0, 30
syscall
# store it in another place
move $a1, $v0
#printing done1
li $v0, 4
la $a0, done1
syscall
# Subtract time values
sub $t2, $a1, $a0
#print time taken
li $v0, 1
move $a0, $t2
syscall
# exit program
li $v0, 10
syscall
首先,从提供系统时间的
syscall
返回后,将结果存储在$a0
中。但是,在循环内部,您将擦除$a0
的值:
#print inputNmber int
li $v0, 1
move $a0, $t0
此外,通过查看syscall表,您可以看到此syscall将时间值如下所示:
$a0 = low order 32 bits of system time
$a1 = high order 32 bits of system time
而不是在$v0
中。所以你应该调整你的move
指令,并考虑到这一点
注意:如果您使用的是模拟器,则此sycall与SPIM不兼容,仅与MARS兼容
系统调用的来源:您能解释一下“$a0=低阶…”部分吗?参考表中似乎没有对此做出任何解释。感谢syscall返回的时间戳是64位的值,因为MIPS寄存器只有32位宽,所以需要2来存储当前时间(返回的值以毫秒为单位)。让我们举一个例子:自历元以来的当前时间是1484002833406毫秒,十六进制为0x159857633FE。因此,在系统调用之后,
$a0
将包含0x00000159(4字节| 32高位),而$a1
将包含0x857633FE(低位32位)。如果可能,您可以展示一个如何获取时间的示例吗?我仍然无法让代码正常工作。谢谢