辅导data编程、讲解C++程序、c/c++语言编程调试 解析Java程序|讲解数据库SQL
- 首页 >> Java编程 1 [5 marks] Memory and Numbers. Consider the execution of the following code on a little-endian processor.
Assume that the address of i is 0x1000 and that the compiler allocates the three global variables contiguously, in
the order they appear, wasting no memory between them other than what is required to ensure that they are properly
aligned (and assuming that char’s are signed, and int’s and pointers are 4-bytes long).
1a For every byte of memory whose value you can determine from the information given above, give its value in
hex on the line labeled with its address. For addresses whose value you can not determine, check the Unknown
box.
0x1000: Unknown or Value ____________
0x1001: Unknown or Value ____________
0x1002: Unknown or Value ____________
0x1003: Unknown or Value ____________
0x1004: Unknown or Value ____________
0x1005: Unknown or Value ____________
0x1006: Unknown or Value ____________
0x1007: Unknown or Value ____________
0x1008: Unknown or Value ____________
0x1009: Unknown or Value ____________
0x100a: Unknown or Value ____________
0x100b: Unknown or Value ____________
2
2 [10 marks] Global Variables and Arrays. Answer the following questions about global variables.
int i;
int a[5];
int *b;
2a Give assembly code for the C statement: i = a[2].
2b Give assembly code for the C statement: b[0] = a[b[i]];.
2c How many memory reads are required to execute the statement? Fill in a single multiple choice option below.
b[a[3]] = a[3] + a[i];
0 1 2 3 4 5 6 7 8
3
3 [10 marks] Structs and Instance Variables. Answer the following questions about the global variables a and b,
declared as follows. Assume that int’s and pointers are 4-bytes long.
struct A {
char x;
int y[3];
struct B* z;
};
struct A* a;
struct B b;
struct B {
struct A j;
int k;
};
Treat each sub-question separately (i.e., do not use values computed in prior sub-questions). Comments are not
required, but they will probably help you.
3a Give assembly code for the C statement: b.k = a->z[2].k;.
3b How many memory reads are required to execute the statement? Fill in a single multiple choice option below.
b.k = a->z->j.z->j.y[2];
0 1 2 3 4 5 6 7 8
4
4 [10 marks] Static Control Flow. Answer the following questions on static control flow.
4a Give assembly code for the following. Assume x and y are global integer variables.
if (x > 0 && x < 10) {
y = x;
} else {
y = 0;
}
4b Consider a procedure with the following signature: int foo(int a, int b);
Give assembly code for the following.
x = foo(x, y);
5
5 [10 marks] C Pointers. Consider the following global variable declarations.
int a[4] = (int[4]){5, 4, 3, 2};
int b[3] = (int[3]){6, 7, 8};
int* c;
Assume that the address of a is 0x1000, the address of b is 0x2000, and the address of c is 0x3000. Now, consider
the the execution of the following additional code.
c = &a[3];
b[*c] = a[1];
c = (b+1);
*c = *c + 1;
*c = *c + 1;
a[3] = c[1];
Indicate the value of each of the following expressions (i.e., the values of a, b, and c) below by checking Unchanged
if the execution does not change the value or by entering its new value in the space provided. Assuming that int’s
are 4-bytes long. Answer these questions about the value of these variables following the execution of this code.
a[0]: Unchanged or Changed to Value ____________
a[1]: Unchanged or Changed to Value ____________
a[2]: Unchanged or Changed to Value ____________
a[3]: Unchanged or Changed to Value ____________
b[0]: Unchanged or Changed to Value ____________
b[1]: Unchanged or Changed to Value ____________
b[2]: Unchanged or Changed to Value ____________
c: Unchanged or Changed to Value ____________
6
6 [12 marks] Dynamic Allocation. Consider each of the following pieces of C code to determine whether it contains
(or may contain) a memory-related problem. Label each of the following code snippets as one of the following:
A: There is no memory leak or dangling pointer; nothing needs to be changed with malloc or free.
B: There is no memory leak or dangling pointer, but the code would be improved by moving malloc or free.
C: There is a possible memory leak that is best resolved by adding, removing or moving malloc or free.
D: There is a possible memory leak that is best resolved by adding reference counting.
E: There is a possible dangling pointer that is best resolved by adding, removing or moving malloc or free.
F: There is a possible dangling pointer that is best resolved by adding reference counting.
You can assume that the starting point for each snippet of code is a call to foo, and that copy is in a different module.
Do not fix any bugs; for each part, fill in a single multiple choice bubble based off of the options above. Most of the
code snippets are very similar. Changes from previous versions and/or key things to look for in bold font.
6a int* copy(int s) {
int* d = malloc(sizeof(int));
*d = s;
return d;
}
void foo (int s) {
int* d = copy(s);
printf("value is %d", *d);
free(d);
}
A B C D E F
6b int* copy(int s) {
int* d = malloc(sizeof(int));
*d = s;
free(d);
return d;
}
void foo (int s) {
int* d = copy(s);
printf("value is %d", *d);
}
A B C D E F
6c void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int d = 0;
copy(s, &d);
printf("value is %d", d);
}
A B C D E F
7
6d void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
free(d);
printf("value is %d", *d);
}
A B C D E F
6e void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
printf("value is %d", *d);
process(d);
free(d);
}
A B C D E F
6f void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
printf("value is %d", *d);
process(*d);
free(d);
}
A B C D E F
8
7 [8 marks] Reference Counting. Consider the following code that is implemented in three independent modules
(and a main module) that share dynamically allocated objects that should be managed using reference counting. The
call to rc_malloc has been added for you; recall that rc_malloc sets the allocated object’s reference count to 1.
7a What does this program print when it executes?
7b Add calls to rc_keep_ref and rc_free_ref to correct implement reference counting for this program
(all modules).
7c Assuming this program implements reference counting correctly, give the reference counts (number of pointers
currently pointing to) the following two objects when printf is called from main?
*p:
*q:
7d Add code at point TODO so that the program is free of memory leaks and dangling pointers. Your code
may call any of the procedures shown here as well as rc_free_ref. It may not directly access the global
variable b_values (note that this variable is not listed in the “header file contents” section of the code and
so it would not be in scope in main if the modules were implemented is separate files).
9
/***********
* Module a
*/
int* a_create(int i) {
int* value = rc_malloc(sizeof(int));
*value = i;
return value;
}
/**********
* Module b
*/
#define B_SIZE 4
int* b_values[B_SIZE];
void b_init() {
for (int i=0; ib_values[i] = NULL;
}
void b_put(int index, int* value) {
if (index>=0 && indexb_values[index] = value;
}
}
int* b_get(int index) {
return b_values[index];
}
10
/********
* Header file content for the three modules - this is all that main can access
*/
int* a_create(int i);
void b_init();
void b_put(int index, int* value);
int* b_get(int index);
/*******
* main
*/
int main(int arc, char** argv) {
b_init();
b_put(0, a_create(10));
b_put(1, a_create(20));
b_put(2, b_get(0));
b_put(3, b_get(2));
int* p = b_get(1);
rc_keep_ref(p);
int* q = b_get(3);
rc_keep_ref(q);
printf("%d %d", *p, *q));
rc_free_ref(p);
rc_free_ref(q);
//TODO
}
11
8 [15 marks] Reading Assembly. Comment the following assembly code and then translate it into C. Assume that
the caller prologue was completed as shown in lecture, and that register 0 is used to return a value. Use the back of the
preceding page for extra space if you need it.
foo: deca r5 #
st r6, (r5) #
ld $0, r0 #
ld $0, r1 #
ld 4(r5), r2 #
ld 8(r5), r3 #
ld 12(r5), r4 #
L0: mov r3, r6 #
not r6
inc r6 #
add r1, r6 #
beq r6, L3 #
ld (r2, r1, 4), r6 #
not r6
inc r6 #
add r4, r6 #
beq r6, L1 #
br L2 #
L1: inc r0 #
L2: inc r1 #
br L0 #
L3: ld (r5), r6 #
inca r5 #
j (r6) #
8a Translate into C:
8b Explain what the code does in one sentence.
12
[This page has been left intentionally blank. If you write any answer you want graded on this page, YOU MUST
indicate in the answer area for that question that you have work on this page, and indicate on this page what question
you are answering.]
13
These two tables describe the SM213 ISA. The first gives a template for instruction machine and assembly language
and describes instruction semantics. It uses ’s’ and ’d’ to refer to source and destination register numbers and ’p’
and ’i’ to refer to compressed-offset and index values. Each character of the machine template corresponds to a 4-bit,
hexit. Offsets in assembly use ’o’ while machine code stores this as ’p’ such that ’o’ is either 2 or 4 times ’p’ as
indicated in the semantics column. The second table gives an example of each instruction.
Operation Machine Language Semantics / RTL Assembly
load immediate 0d-- vvvvvvvv r[d] vvvvvvvv ld $vvvvvvvv,rd
load base+offset 1psd r[d] m[(o = p ⇥ 4) + r[s]] ld o(rs),rd
load indexed 2bid r[d] m[r[b] + r[i] ⇥ 4] ld (rb,ri,4),rd
store base+offset 3spd m[(o = p ⇥ 4) + r[d]] r[s] st rs,o(rd)
store indexed 4sdi m[r[b] + r[i] ⇥ 4] r[s] st rs,(rb,ri,4)
halt F000 (stop execution) halt
nop FF00 (do nothing) nop
rr move 60sd r[d] r[s] mov rs, rd
add 61sd r[d] r[d] + r[s] add rs, rd
and 62sd r[d] r[d] & r[s] and rs, rd
inc 63-d r[d] r[d]+1 inc rd
inc addr 64-d r[d] r[d]+4 inca rd
dec 65-d r[d] r[d]
1 dec rd
dec addr 66-d r[d] r[d]
4 deca rd
not 67-d r[d] !r[d] not rd
shift 7dss r[d] r[d] << ss shl ss, rd
(if ss is negative) shr -ss, rd
branch 8-pp pc (a = pc + pp ⇥ 2) br a
branch if equal 9rpp if r[r] == 0 : pc (a = pc + pp ⇥ 2) beq rr, a
branch if greater Arpp if r[r] > 0 : pc (a = pc + pp ⇥ 2) bgt rr, a
jump B--- aaaaaaaa pc a j a
get program counter 6Fpd r[d] pc + (o = 2 ⇥ p) gpc $o, rd
jump indirect Cdpp pc r[d]+(o = 2 ⇥ pp) j o(rd)
jump double ind, b+off Cdpp pc m[(o = 4 ⇥ pp) + r[d]] j *o(rd)
jump double ind, index Edi- pc m[4 ⇥ r[i] + r[d]] j *(rd,ri,4)
Operation Machine Language Example Assembly Language Example
load immediate 0100 00001000 ld $0x1000,r1
load base+offset 1123 ld 4(r2),r3
load indexed 2123 ld (r1,r2,4),r3
store base+offset 3123 st r1,8(r3)
store indexed 4123 st r1,(r2,r3,4)
halt f000 halt
nop ff00 nop
rr move 6012 mov r1, r2
add 6112 add r1, r2
and 6212 and r1, r2
inc 6301 inc r1
inc addr 6401 inca r1
dec 6501 dec r1
dec addr 6601 deca r1
not 6701 not r1
shift 7102 shl $2, r1
71fe shr $2, r1
branch 1000: 8003 br 0x1008
branch if equal 1000: 9103 beq r1, 0x1008
branch if greater 1000: a103 bgt r1, 0x1008
jump b000 00001000 j 0x1000
get program counter 6f31 gpc $6, r1
jump indirect c104 j 8(r1)
jump double ind, b+off d102 j *8(r1)
jump double ind, index e120 j *(r1,r2,4)
14
Assume that the address of i is 0x1000 and that the compiler allocates the three global variables contiguously, in
the order they appear, wasting no memory between them other than what is required to ensure that they are properly
aligned (and assuming that char’s are signed, and int’s and pointers are 4-bytes long).
1a For every byte of memory whose value you can determine from the information given above, give its value in
hex on the line labeled with its address. For addresses whose value you can not determine, check the Unknown
box.
0x1000: Unknown or Value ____________
0x1001: Unknown or Value ____________
0x1002: Unknown or Value ____________
0x1003: Unknown or Value ____________
0x1004: Unknown or Value ____________
0x1005: Unknown or Value ____________
0x1006: Unknown or Value ____________
0x1007: Unknown or Value ____________
0x1008: Unknown or Value ____________
0x1009: Unknown or Value ____________
0x100a: Unknown or Value ____________
0x100b: Unknown or Value ____________
2
2 [10 marks] Global Variables and Arrays. Answer the following questions about global variables.
int i;
int a[5];
int *b;
2a Give assembly code for the C statement: i = a[2].
2b Give assembly code for the C statement: b[0] = a[b[i]];.
2c How many memory reads are required to execute the statement? Fill in a single multiple choice option below.
b[a[3]] = a[3] + a[i];
0 1 2 3 4 5 6 7 8
3
3 [10 marks] Structs and Instance Variables. Answer the following questions about the global variables a and b,
declared as follows. Assume that int’s and pointers are 4-bytes long.
struct A {
char x;
int y[3];
struct B* z;
};
struct A* a;
struct B b;
struct B {
struct A j;
int k;
};
Treat each sub-question separately (i.e., do not use values computed in prior sub-questions). Comments are not
required, but they will probably help you.
3a Give assembly code for the C statement: b.k = a->z[2].k;.
3b How many memory reads are required to execute the statement? Fill in a single multiple choice option below.
b.k = a->z->j.z->j.y[2];
0 1 2 3 4 5 6 7 8
4
4 [10 marks] Static Control Flow. Answer the following questions on static control flow.
4a Give assembly code for the following. Assume x and y are global integer variables.
if (x > 0 && x < 10) {
y = x;
} else {
y = 0;
}
4b Consider a procedure with the following signature: int foo(int a, int b);
Give assembly code for the following.
x = foo(x, y);
5
5 [10 marks] C Pointers. Consider the following global variable declarations.
int a[4] = (int[4]){5, 4, 3, 2};
int b[3] = (int[3]){6, 7, 8};
int* c;
Assume that the address of a is 0x1000, the address of b is 0x2000, and the address of c is 0x3000. Now, consider
the the execution of the following additional code.
c = &a[3];
b[*c] = a[1];
c = (b+1);
*c = *c + 1;
*c = *c + 1;
a[3] = c[1];
Indicate the value of each of the following expressions (i.e., the values of a, b, and c) below by checking Unchanged
if the execution does not change the value or by entering its new value in the space provided. Assuming that int’s
are 4-bytes long. Answer these questions about the value of these variables following the execution of this code.
a[0]: Unchanged or Changed to Value ____________
a[1]: Unchanged or Changed to Value ____________
a[2]: Unchanged or Changed to Value ____________
a[3]: Unchanged or Changed to Value ____________
b[0]: Unchanged or Changed to Value ____________
b[1]: Unchanged or Changed to Value ____________
b[2]: Unchanged or Changed to Value ____________
c: Unchanged or Changed to Value ____________
6
6 [12 marks] Dynamic Allocation. Consider each of the following pieces of C code to determine whether it contains
(or may contain) a memory-related problem. Label each of the following code snippets as one of the following:
A: There is no memory leak or dangling pointer; nothing needs to be changed with malloc or free.
B: There is no memory leak or dangling pointer, but the code would be improved by moving malloc or free.
C: There is a possible memory leak that is best resolved by adding, removing or moving malloc or free.
D: There is a possible memory leak that is best resolved by adding reference counting.
E: There is a possible dangling pointer that is best resolved by adding, removing or moving malloc or free.
F: There is a possible dangling pointer that is best resolved by adding reference counting.
You can assume that the starting point for each snippet of code is a call to foo, and that copy is in a different module.
Do not fix any bugs; for each part, fill in a single multiple choice bubble based off of the options above. Most of the
code snippets are very similar. Changes from previous versions and/or key things to look for in bold font.
6a int* copy(int s) {
int* d = malloc(sizeof(int));
*d = s;
return d;
}
void foo (int s) {
int* d = copy(s);
printf("value is %d", *d);
free(d);
}
A B C D E F
6b int* copy(int s) {
int* d = malloc(sizeof(int));
*d = s;
free(d);
return d;
}
void foo (int s) {
int* d = copy(s);
printf("value is %d", *d);
}
A B C D E F
6c void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int d = 0;
copy(s, &d);
printf("value is %d", d);
}
A B C D E F
7
6d void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
free(d);
printf("value is %d", *d);
}
A B C D E F
6e void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
printf("value is %d", *d);
process(d);
free(d);
}
A B C D E F
6f void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
printf("value is %d", *d);
process(*d);
free(d);
}
A B C D E F
8
7 [8 marks] Reference Counting. Consider the following code that is implemented in three independent modules
(and a main module) that share dynamically allocated objects that should be managed using reference counting. The
call to rc_malloc has been added for you; recall that rc_malloc sets the allocated object’s reference count to 1.
7a What does this program print when it executes?
7b Add calls to rc_keep_ref and rc_free_ref to correct implement reference counting for this program
(all modules).
7c Assuming this program implements reference counting correctly, give the reference counts (number of pointers
currently pointing to) the following two objects when printf is called from main?
*p:
*q:
7d Add code at point TODO so that the program is free of memory leaks and dangling pointers. Your code
may call any of the procedures shown here as well as rc_free_ref. It may not directly access the global
variable b_values (note that this variable is not listed in the “header file contents” section of the code and
so it would not be in scope in main if the modules were implemented is separate files).
9
/***********
* Module a
*/
int* a_create(int i) {
int* value = rc_malloc(sizeof(int));
*value = i;
return value;
}
/**********
* Module b
*/
#define B_SIZE 4
int* b_values[B_SIZE];
void b_init() {
for (int i=0; i
}
void b_put(int index, int* value) {
if (index>=0 && index
}
}
int* b_get(int index) {
return b_values[index];
}
10
/********
* Header file content for the three modules - this is all that main can access
*/
int* a_create(int i);
void b_init();
void b_put(int index, int* value);
int* b_get(int index);
/*******
* main
*/
int main(int arc, char** argv) {
b_init();
b_put(0, a_create(10));
b_put(1, a_create(20));
b_put(2, b_get(0));
b_put(3, b_get(2));
int* p = b_get(1);
rc_keep_ref(p);
int* q = b_get(3);
rc_keep_ref(q);
printf("%d %d", *p, *q));
rc_free_ref(p);
rc_free_ref(q);
//TODO
}
11
8 [15 marks] Reading Assembly. Comment the following assembly code and then translate it into C. Assume that
the caller prologue was completed as shown in lecture, and that register 0 is used to return a value. Use the back of the
preceding page for extra space if you need it.
foo: deca r5 #
st r6, (r5) #
ld $0, r0 #
ld $0, r1 #
ld 4(r5), r2 #
ld 8(r5), r3 #
ld 12(r5), r4 #
L0: mov r3, r6 #
not r6
inc r6 #
add r1, r6 #
beq r6, L3 #
ld (r2, r1, 4), r6 #
not r6
inc r6 #
add r4, r6 #
beq r6, L1 #
br L2 #
L1: inc r0 #
L2: inc r1 #
br L0 #
L3: ld (r5), r6 #
inca r5 #
j (r6) #
8a Translate into C:
8b Explain what the code does in one sentence.
12
[This page has been left intentionally blank. If you write any answer you want graded on this page, YOU MUST
indicate in the answer area for that question that you have work on this page, and indicate on this page what question
you are answering.]
13
These two tables describe the SM213 ISA. The first gives a template for instruction machine and assembly language
and describes instruction semantics. It uses ’s’ and ’d’ to refer to source and destination register numbers and ’p’
and ’i’ to refer to compressed-offset and index values. Each character of the machine template corresponds to a 4-bit,
hexit. Offsets in assembly use ’o’ while machine code stores this as ’p’ such that ’o’ is either 2 or 4 times ’p’ as
indicated in the semantics column. The second table gives an example of each instruction.
Operation Machine Language Semantics / RTL Assembly
load immediate 0d-- vvvvvvvv r[d] vvvvvvvv ld $vvvvvvvv,rd
load base+offset 1psd r[d] m[(o = p ⇥ 4) + r[s]] ld o(rs),rd
load indexed 2bid r[d] m[r[b] + r[i] ⇥ 4] ld (rb,ri,4),rd
store base+offset 3spd m[(o = p ⇥ 4) + r[d]] r[s] st rs,o(rd)
store indexed 4sdi m[r[b] + r[i] ⇥ 4] r[s] st rs,(rb,ri,4)
halt F000 (stop execution) halt
nop FF00 (do nothing) nop
rr move 60sd r[d] r[s] mov rs, rd
add 61sd r[d] r[d] + r[s] add rs, rd
and 62sd r[d] r[d] & r[s] and rs, rd
inc 63-d r[d] r[d]+1 inc rd
inc addr 64-d r[d] r[d]+4 inca rd
dec 65-d r[d] r[d]
1 dec rd
dec addr 66-d r[d] r[d]
4 deca rd
not 67-d r[d] !r[d] not rd
shift 7dss r[d] r[d] << ss shl ss, rd
(if ss is negative) shr -ss, rd
branch 8-pp pc (a = pc + pp ⇥ 2) br a
branch if equal 9rpp if r[r] == 0 : pc (a = pc + pp ⇥ 2) beq rr, a
branch if greater Arpp if r[r] > 0 : pc (a = pc + pp ⇥ 2) bgt rr, a
jump B--- aaaaaaaa pc a j a
get program counter 6Fpd r[d] pc + (o = 2 ⇥ p) gpc $o, rd
jump indirect Cdpp pc r[d]+(o = 2 ⇥ pp) j o(rd)
jump double ind, b+off Cdpp pc m[(o = 4 ⇥ pp) + r[d]] j *o(rd)
jump double ind, index Edi- pc m[4 ⇥ r[i] + r[d]] j *(rd,ri,4)
Operation Machine Language Example Assembly Language Example
load immediate 0100 00001000 ld $0x1000,r1
load base+offset 1123 ld 4(r2),r3
load indexed 2123 ld (r1,r2,4),r3
store base+offset 3123 st r1,8(r3)
store indexed 4123 st r1,(r2,r3,4)
halt f000 halt
nop ff00 nop
rr move 6012 mov r1, r2
add 6112 add r1, r2
and 6212 and r1, r2
inc 6301 inc r1
inc addr 6401 inca r1
dec 6501 dec r1
dec addr 6601 deca r1
not 6701 not r1
shift 7102 shl $2, r1
71fe shr $2, r1
branch 1000: 8003 br 0x1008
branch if equal 1000: 9103 beq r1, 0x1008
branch if greater 1000: a103 bgt r1, 0x1008
jump b000 00001000 j 0x1000
get program counter 6f31 gpc $6, r1
jump indirect c104 j 8(r1)
jump double ind, b+off d102 j *8(r1)
jump double ind, index e120 j *(r1,r2,4)
14