金星RISC-V如何循环,比较和打印? [英] Venus RISC-V how to loop, compare, and print?
问题描述
我正在尝试遍历数组,如果数字大于X,则打印.
I am trying to loop through an array and if the number is larger than X then print.
我试图在线查找教程,但是我只是停留在为什么它不起作用/没有输出任何东西的问题上.我的评论有点解释我的尝试.
I've tried to find tutorials online but I'm just stuck on why it is not working/outputting anything. My comments kind of explain what i tried to do.
.data
arrayOfNums:
.word 0
.word 1
.word 122
.word 1112
.word 4294967295
.word 22
.word 234234
.word 23332
.word 42
.word 23423
K: .word 2237
.text
.globl main
main:
#### *** vv My problem starts here vv *** ####
la t0 K #set t0 to K
la t1 arrayOfNums #set t1 pointer to array
addi a1 t0 0 #add t0 deallocated to a1
addi a2 t1 0 #add t1 deallocated to a2
loop:
addi a0 x0 1 # print_int ecall
addi a1 t1 0 # add t1 value to print
ecall
addi t1, t1, 4 # Increments t1 to move to the next element
jal x0, loop
exit:
####Exit using environmental calls####
addi a1 x0 0 # random exit 0
addi a0 x0 17 # print_int ecall
ecall
谢谢!
推荐答案
您发布的代码存在一些问题.
There are some issues with the code you have posted.
我正在尝试遍历数组,如果数字大于X,则打印.
I am trying to loop through an array and if the number is larger than X then print.
我在您的代码中找不到任何X符号.你是说K
吗?
I can't find any X symbol in your code. Do you mean K
?
la t0 K #set t0 to K
该评论是错误的.您将K的地址加载到t0
中.如果要将存储在该地址的值保存到t0
中,则必须将该地址加载到另一个寄存器中,然后将该地址取消引用到t0
中,即用lw
或lwu
指令加载它./p>
The comment is wrong. You load the address of K into t0
. If you want to get the value that is stored at that address into t0
you have to load the address into another register and dereference that address into t0
, i.e. load it with the lw
or lwu
instruction.
addi a1 t0 0 #add t0 deallocated to a1
addi a2 t1 0 #add t1 deallocated to a2
分配给"是什么意思?您将t0
复制到a1
,并且将t1
复制到a2
.可以使用mv
伪指令归档相同的内容.但是,当您立即覆盖下面几行中的a1
和a2
寄存器时,这两行是多余的.
What do you mean with 'deallocated to'? You copy t0
to a1
and t1
to a2
. The same could be archieved with the mv
pseudo instruction. However, those 2 lines are superfluous as you immediately overwrite the a1
and a2
registers in the following lines.
addi a0 x0 1 # print_int ecall
addi a1 t1 0 # add t1 value to print
ecall
您可以在此处使用li
/mv
伪指令.这将无条件打印t1
的值-这是一个地址.如果要打印实际的数组元素,则必须使用t1
-cf中存储的地址加载它. lw
/lwu
说明.
You could use the li
/mv
pseudo instructions here. This unconditionally prints the value of t1
- which is an address. If you want to print an actual array element you would have to load it using the address stored in t1
- cf. the lw
/lwu
instructions.
addi t1, t1, 4 # Increments t1 to move to the next element
jal x0, loop
这样一来,您将无条件跳到循环的开头(使用伪指令:j loop
),即,这意味着您读取了数组的末尾,并且永不退出循环.要解决此问题,您必须使用条件分支指令,例如bnez
.意思是你将一个寄存器(作为计数器)设置为数组大小,并将其递减至零.或者将寄存器设置为最后一个数组元素之后的地址并分支,直到t1
等于它.
With that you unconditionally jump to the head of the loop (with pseudo instruction: j loop
), i.e. that means that you read over the end of your array and never quit the loop. To fix this you have to use a conditional branch instruction such as bnez
. Meaning that you e.g. set a register (as counter) to the array size and decrement it until zero. Or set a register to the address after the last array element and branch until t1
is equal to it.
addi a1 x0 0 # random exit 0
addi a0 x0 17 # print_int ecall
ecall
除了随机之外什么都没有.注释不正确,您正在调用Venus exit2系统调用,而不是print_int.此外,Venus还提供了不需要参数的退出系统调用(10
).
It's anything but random. The comment is incorrect, you are calling the Venus exit2 syscall, not print_int. Besides, Venus also provides an exit syscall (10
) that doesn't require an argument.
您的代码中完全缺少的是实际尝试比较数字,然后根据该比较结果打印它们的位置.
What is completely missing from your code is a location where you are actually trying to compare numbers and then print them based on that comparison.
这篇关于金星RISC-V如何循环,比较和打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!