[英]Venus RISC-V how to loop, compare, and print?
I am trying to loop through an array and if the number is larger than X then print.我正在尝试遍历一个数组,如果数字大于 X,则打印。
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
Thank you!谢谢!
There are some issues with the code you have posted.您发布的代码存在一些问题。
I am trying to loop through an array and if the number is larger than X then print.
我正在尝试遍历一个数组,如果数字大于 X,则打印。
I can't find any X symbol in your code.我在您的代码中找不到任何 X 符号。 Do you mean
K
?你是说
K
吗?
la t0 K #set t0 to K
The comment is wrong.评论是错误的。 You load the address of K into
t0
.您将K的地址加载到
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
, ie load it with the lw
or lwu
instruction.如果要将存储在该地址的值放入
t0
,则必须将该地址加载到另一个寄存器中并将该地址解引用到t0
,即使用lw
或lwu
指令加载它。
addi a1 t0 0 #add t0 deallocated to a1 addi a2 t1 0 #add t1 deallocated to a2
What do you mean with 'deallocated to'? “解除分配给”是什么意思? You copy
t0
to a1
and t1
to a2
.您将
t0
复制到a1
并将t1
复制到a2
。 The same could be archieved with the mv
pseudo instruction.同样可以用
mv
伪指令存档。 However, those 2 lines are superfluous as you immediately overwrite the a1
and a2
registers in the following lines.但是,当您立即覆盖以下行中的
a1
和a2
寄存器时,这两行是多余的。
addi a0 x0 1 # print_int ecall addi a1 t1 0 # add t1 value to print ecall
You could use the li
/ mv
pseudo instructions here.您可以在此处使用
li
/ mv
伪指令。 This unconditionally prints the value of t1
- which is an address.这无条件地打印
t1
的值 - 这是一个地址。 If you want to print an actual array element you would have to load it using the address stored in t1
- cf.如果要打印实际的数组元素,则必须使用存储在
t1
的地址加载它 - 参见。 the lw
/ lwu
instructions. lw
/ lwu
指令。
addi t1, t1, 4 # Increments t1 to move to the next element jal x0, loop
With that you unconditionally jump to the head of the loop (with pseudo instruction: j loop
), ie that means that you read over the end of your array and never quit the loop.有了它,您无条件地跳到循环的开头(使用伪指令:
j loop
),即这意味着您阅读了数组的末尾并且永远不会退出循环。 To fix this you have to use a conditional branch instruction such as bnez
.要解决此问题,您必须使用条件分支指令,例如
bnez
。 Meaning that you eg 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.或者在最后一个数组元素之后的地址设置一个寄存器并分支直到
t1
等于它。
addi a1 x0 0 # random exit 0 addi a0 x0 17 # print_int ecall ecall
It's anything but random.这绝不是随机的。 The comment is incorrect, you are calling the Venus exit2 syscall, not print_int.
评论不正确,您正在调用 Venus exit2 系统调用,而不是 print_int。 Besides, Venus also provides an exit syscall (
10
) that doesn't require an argument.此外,Venus 还提供了一个不需要参数的退出系统调用 (
10
)。
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.您的代码中完全缺少的是您实际尝试比较数字然后根据该比较打印它们的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.