问题
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
Thank you!
回答1:
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.
I can't find any X symbol in your code. Do you mean K
?
la t0 K #set t0 to K
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
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
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
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
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.
来源:https://stackoverflow.com/questions/60087133/venus-risc-v-how-to-loop-compare-and-print