1

In this youtube video, the instructor explained some basic code in RISC-V assembly, but i didn't understand why in the first line, he is shifting i by 3. Why do we have to multiply it by 8??

I feel like it has something to do with the fact that LOAD IMMEDIATE operations can load values up to 12 bits only, but I couldn't make the connection.

enter image description here

Shinobi San
  • 111
  • 1
  • 1
    What is save? Is it by any chance an array of 8 byte values? in which case the shift is about converting the index (which increments by 1 per array item), into an address offset (which increments by the size of the array item per item) – user1937198 Feb 27 '23 at 13:05

1 Answers1

2

The ld instruction…

Loads a 64-bit value from memory into register rd for RV64I

It's then safe to say that the type of save[i] is 8 bytes large, something like this:

uint64_t save[10] = { 1, 2, 3, ... };
...
while (save[i] == k) i += 1;

In C, save[i] addresses the object so the compiler must adjust for its size.

pipe
  • 13,748
  • 5
  • 42
  • 72
  • 2
    `ld` is "load doubleword", aka "load 64 bits from memory", so `save` is *definitely* an array of 64-bit values. – Jonathan S. Feb 27 '23 at 13:40
  • @JonathanS. Thanks, I know nothing about RISC-V but I found a reference and added a link to it. – pipe Feb 27 '23 at 13:57