Reputation: 71
I'm currently learning about basic assembly language via RISC-V, and I'm slightly confused on valid ways to loop through an array.
If we're given a0 as a pointer to an integer array, would this suffice to move to the next element?
If I would want to modify the actual value of the element at the location as well, can I simply use sw?
I also know using slli/srli also allows you to shift, can anyone explain that concept?
Thank you!
Upvotes: 3
Views: 12789
Reputation: 39047
The basic pattern for traversing an array (with n elements) is this:
Y = X + n * 4
) with shift and add instructionslw
to load the word from the address that is present in X) and do something with that array elementadd
instructionRegarding modification: You modify the loaded array element in a register and then you can store the new register value back into your array with sw
.
Upvotes: 7