struct U {
int* x;
int y[3];
};
struct T {
struct U f;
struct S *g;
};
struct S {
int a;
int b[3];
struct T c;
};
struct S* s; // assume s stores the value 4000 (base 10)
Given the C structs and variable declaration above, when running on a 64-bit architecture where pointers are 8-bytes long, determine, if possible, the address of each of the following. Assume that the value of the variable s is 4000, match up the C expression question with the letter of the correct answer giving the address or cannot be determined. Note: all addresses are given in decimal, so the offset 12 from 4000 is decimal 4012.
s->a: 4000
s->c.f.y[1]: 4000 + 4 + 3(4) + 8 + 4 = 4028
s->c.f.x: 4000 + 4 + 3(4) = 4016
s->c.g->c.f.y[0]: g is a ptr so it's offsets can't be determined
How many memory reads (i.e., instructions that load from memory) are required to compute the value of the following expressions. Again match up the correct letters.
Note that &s->c.f.y[2] == &(s->c.f.y[2]).
&s->a: &s - 1
s->c.f.y[2]: s, y[2] - 2
s->c.g->b[1]: s, g, b[1] - 3
s->c.g.b[1]: Invalid
struct U {
int* x;
int y[3];
};
struct T {
struct U f;
struct S *g;
};
struct S {
int a;
int b[3];
struct T c;
};
struct S* s; // assume s stores the value 3000 (base 10)
How many memory reads (i.e., instructions that load from memory) are required to compute the value of the following expressions. Again match up the correct letters.
Note that &s->c.f.y[2] is equivalent to &(s->c.f.y[2]).
s->b[1]: &s, b[1] - 2
&s->c.f.y[0]: &s - 1
s->c.g->b[1]: &s, &g, b[1]
s->c.g.f: Invalid (g is a pointer)
Assume the following C structs and global variables are declared. Recall that, unlike the previous questions, sm213 is a 32-bit machine and so pointers are only 4 bytes long.
struct S {
int* a;
int b[4];
struct T c;
struct S* d;
};
struct T {
int x[4];
int* y;
};
struct S* s;
Which of the following pieces of SM213 assembly code (if any) is equivalent to this C statement?
s->c.x[0] = 0;
ld $0, r0
ld $s, r1
ld 20(r1), r2
st r0, 20(r2)
ld $0, r0
ld $s, r1
ld (r1), r2
st r0, 20(r2)
ld $0, r0
ld $s, r1
st r0, 20(r1)
ld $0, r0
ld $s, r1
ld (r1), r2
ld 20(r2), r3
st r0, (r3)
Assume the following C structs and global variables are declared.
Since this question asks you to write sm213 assembly code, and sm213 is a 32-bit machine, pointers are 4 bytes long, not 8 as would normally be the case with just C code.
struct A {
int x;
int* y;
struct B* b_struct;
};
struct B {
int i;
int j[2];
};
struct A* a;
struct B b;
int v0;
Translate these snippets of C code into SM213 assembly. Note that each question should be considered individually; code in one section has no effect on the other sections.
a->y = b.j;
Into SM213 Assembly code
ld $a, r0
ld (r0), r1
ld $b, r2
ld $4, r3
add r3, r2
st r2, 4(r1)
v0 = a->y[3];
Into SM213 Assembly code
ld $v0, r0
ld $a, r1
ld (r1), r2
ld 4(r2), r3
ld 12(r3), r4
st r4, (r0)
Consider the following struct and global-variable declarations.
// Option A
struct S {
int x;
int* y;
struct T* t;
};
// Option B
struct S {
int* x;
int** y;
int* z;
};
// Option C
struct S {
struct T* t_ptr;
struct T t;
};
struct T {
int a[6];
struct S* arr;
}
struct S s;
struct T* t;
int v0, v1;
int* arr;
void* v_ptr;
Each question below lists a sequence of asm instructions that might be an appropriate storing of a value involving one (or more) of these structs. Indicate which ones. The code must be exactly correct in order to match a struct. Assume that the code that is compiled into those instructions did so without warnings (e.g. no "creative" implicit type casting took place). You can also assume that assembly labels for global variables have already been defined correctly.
Keep in mind that SM213 is a 32-bit system, so pointers are four bytes long.
ld $s, r0
ld $arr, r1
inca r0
st r0, (r1)
Select all possible options that apply.
ld $s, r0
ld 4(r0), r0
ld $v0, r1
st r0, (r1)
ld (r0), r0
ld $arr, r2
st r0, (r2)
Select all possible options that apply.
Consider the following C type and global variable declarations.
struct U {
int n;
struct B l;
int m;
};
struct B {
int y;
struct U *c[3];
};
struct U *t;
struct B j;
int p;
Convert the statement below to an appropriate SM213 equivalent code. As usual in SM213, assume that all pointers are 32-bits long.
t->l.y = j.c[p]->m;
ld $t, r0
ld (r0), r0
ld $j, r1
inca r1
ld $p, r2
ld (r2), r2
ld (r1, r2, 4), r3
ld 20(r3), r4
st r4, 4(r0)