首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
辅导data编程、讲解C++程序、c/c++语言编程调试 解析Java程序|讲解数据库SQL
项目预算:
开发周期:
发布时间:
要求地区:
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; i
b_values[i] = NULL;
}
void b_put(int index, int* value) {
if (index>=0 && index
b_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
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代做ceng0013 design of a pro...
2024-11-13
代做mech4880 refrigeration a...
2024-11-13
代做mcd1350: media studies a...
2024-11-13
代写fint b338f (autumn 2024)...
2024-11-13
代做engd3000 design of tunab...
2024-11-13
代做n1611 financial economet...
2024-11-13
代做econ 2331: economic and ...
2024-11-13
代做cs770/870 assignment 8代...
2024-11-13
代写amath 481/581 autumn qua...
2024-11-13
代做ccc8013 the process of s...
2024-11-13
代写csit040 – modern comput...
2024-11-13
代写econ 2070: introduc2on t...
2024-11-13
代写cct260, project 2 person...
2024-11-13
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!