How does lw know how to load word sizes?

47 views
Skip to first unread message

Ryan Tio

unread,
Sep 22, 2020, 10:14:43 PM9/22/20
to cmpt-295-sfu
# Lecture Question
Week : 3
Slide deck name: Control-Flow
Question: For the array example, I was wondering how we dealt with arrays that maybe contained a complex type or a struct, how do we know that loading a single word size into the register will be enough to contain the whole object, or will it always be a pointer to the object?

Ryan Tio

unread,
Sep 22, 2020, 10:16:25 PM9/22/20
to cmpt-295-sfu
I mean something like 
struct names {
...
};

struct names arr[10]; 

and we wanted to load say arr[5] into the registry using something like 
lw x12, 0(x14)
How does lw know that the object will fit into the single word size?

Arrvindh Shriraman

unread,
Sep 23, 2020, 12:27:18 AM9/23/20
to cmpt-295-sfu
Short answer: you can't. A single C statement will get lowered to multiple assembly instructions by the compiler
Two reasons
- Registers are only 1 word wide (see Registers video)
- You do not have access to those types of load instructions. 


You can only operate on one word at a time.
See here for instrance. 

Check out the copy function. I am saying record[0] = record[1], which means internally I need multiple lw and sw to copy from one location in memory to another one word at a time

Modify the code and see how that leads to different instruction sequences. 


(Small correction: It is not registry. It is either registers or register file).

-----------------------------------

If you have not tried godbolt before then my videos should include a short description.

This video also includes more details. (https://www.youtube.com/watch?v=4_HL3PH4wDg)

- Make sure you select the RISC-V compiler shown in my link if you reload the page.
  if you select a different compiler then the assembly for a different processor will be generated
- Make sure you set the command line arg (like I have done) to -O1. This makes the code more readable. 
Reply all
Reply to author
Forward
0 new messages