logo

UTK Notes


Structures and Memory Practice

Question 1

Write the instruction necessary to allocate 24 bytes from the stack:

addi sp, sp, -24

Question 2

Given the following code:

1
2
3
4
5
6
7
8
9
10
struct MyStruct {
     int i;
     long j;
     short k;
};
int main() {
    MyStruct ms[20];
    
    long value = ms[10].j;
}

What byte offset would you use to load ms[10].j?

248

Question 3

What is the largest addressable unit in RISC-V (in bits)?

64

Question 4

What is the smallest addressable unit in RISC-V (in bits)?

8

Question 5

Given the following C++ code:

1
2
3
int get_value(const unsigned short *data) {
    return data[12];
}

Write the assembly instruction that loads data[12] into the proper register to return as shown in the C++ code above:

lhu a0, 24(a0)

Question 6

Given the following code:

1
2
3
4
5
6
7
8
9
10
struct MyStruct {
     int i;
     long j;
     short k;
};
int main() {
    MyStruct ms[20];
    
    long value = ms[3].k;
}

What byte offset would you use to load ms[3].k?

88

Question 7

All of the stack space allocated to a specific function, such as int main, defines a range from top to bottom. This range is better known as a/an stack frame

Question 8

Given the following structure,

1
2
3
4
5
struct MyStruct {
    char i;
    int  j;
    long k;
};

What is the offset of int j?

4

Question 9

Given the following code:

1
2
3
4
5
6
7
8
9
struct MyStruct {
    int i;
    int j;
    int k;
};
int main() {
    MyStruct ms[200];
    int value = ms[94].k;
}

Assuming ms is in memory at 79,361 (base 10), what memory address would be loaded for ms[94].k? Give your answer in base 10.

80,497

Question 10

Which of the following is the invariant used to determine the offset of a field?

offset % size == 0

Question 11

Given the following code:

1
2
3
int main(int argc, char *argv[]) {
     return argv[30];
}

What is the byte offset of argv[30]?

IDK MIGHT BE 8?

Question 12

Given the following code:

1
2
3
int val = 0xcafefeed;
unsigned char *p = (unsigned char *)&val;
printf("%x", *p);

What is printed to the screen?

ed

Question 13

Architectures that store the least-significant byte first in multibyte words are said to be which type of machine?

little-endian