[SOLVED] CS计算机代考程序代写 assembly CPSC 213 Midterm 2019W Term 1 (Oct 21, 2019) – Solution 1. Numbers and Memory [8 marks]

30 $

File Name: CS计算机代考程序代写_assembly_CPSC_213_Midterm_2019W_Term_1_(Oct_21,_2019)_–_Solution_1._Numbers_and_Memory_[8_marks].zip
File Size: 1205.76 KB

SKU: 1811045281 Category: Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Or Upload Your Assignment Here:


CPSC 213 Midterm 2019W Term 1 (Oct 21, 2019) – Solution 1. Numbers and Memory [8 marks]
a) Considerthefollowingcoderunningonalittle-endianmachinewheretheaddressofiis0x1000. int i = 0x1234;
What is the hex value of the byte at address 0x1000? 0x34
b) Considerthefollowingcodewheretheaddressofsis0x1000.
struct SS {
char c;
int i;
char d[2]; };
struct SS s;
What is the value of the expression &s.i ?
0x1004
What is the value of the expression sizeof(s) ? 12
c) Consider the following code
int i = 0x56789a;
char c = (char) i;
int j = c;
What is the hex value of c ? 0x9a
What is the hex value of j ?
 0xffffff9a
1 of 11

2. Global Variables and Arrays [9 marks]
Consider the following code where the value of the variable i is already loaded into r0 and where the assembly- codelabelsa,b,andcrepresenttheaddressofthevariablesa,b,andc,respectively. Treateachsubquestion separately; do not use values computed in prior subquestions. No comments needed.
int* a;
int b[2];
int c;
a) Give assembly code for the statement c = *a;
ld $a, r1 ld(r1),r1 ld(r1),r1 ld $c, r2 str1,(r2)
# r1 = &a #r1=a #r1=*a # r2 = &c #c=*a;
b) Give assembly code for the statement a = b;
ld $b, r1 # r1 = b ld $a, r2 # r2 = &a str1,(r2) #a=b
c) Give assembly code for the statement b[i] = *(a + i) + i;
ld $a, r1
ld (r1), r1 ld(r1,r0,4),r2 add r0, r2
ld $b, r3 str2,(r3,r0,4)
# r1 = &a
# r1 = a #r2=*(a+i)=a[i] # r2 = *(a + i) + i
# r3 = b #b[i]=*(a+1)+i
2 of 11

3. Structs and Instance Variables [9 marks]
Consider the following code, where f is a global variable, assuming whatever assembly labels make sense, and
where the size of a pointer is 8-bytes. Use r0 for the variable i. No comments needed.
struct X {
int* a;
intb[4];
struct Y c;
}
struct X* f;
struct Y {
int d;
struct X* e; }
a) Give assembly code for i = f->c.e->a[1]; (i.e., store the result in r0)
ld $f, r0 # r0 = &f
ld(r0),r0 #r0=f
ld 32(r0), r0 # r0 = f->c.e
ld (r0), r0 # r0 = f->c.e->a
ld 4(r0), r0 # r0 = i’= f->c.e->a[1]
b) Give assembly code for f->b[1] = i; (i.e., just use r0 for i)
ld $f, r1 # r1 = &f ld(r1),r1 #r1=f
st r0, 12(r1) # f->b[1] = i’
c) What is the minimum number of memory references required to compute the value of the following expression (not including fetching the instructions themselves from memory)?
f->c.e->c.e[0].a[0]
01234567
3 of 11

4. Static Control Flow [7 marks] XXXX THIS TOPIC NOT COVERED on 2020 T1 MIDTERM Assumingx,yandzareglobalvariablesthathavealreadybeendeclaredandinitialized. Nocommentsneeded.
a) Giveassemblycodefortheloop(youmustimplementaloop)
for (int x=0; x y #gotodoneifx’>=y
# last two lines optional (since x in declared in for statement) ld $x r1 # r1 = &x
str0,(r1) #x=x’
b) Giveassemblycodefortheprocedurecall(ignoreanythinghavingtodowiththestack).
foo();
c) Give assembly code for the return statement (ignore anything having to do with the stack).
return;
gpc $6, r6
j foo
j (r6)
4 of 11

5. C Pointers [8 marks]
Give the values of the listed variables following the execution of this block of code.
int i = 1;
int j = 10;
int* p = &i
int* q = &j
*p = *p + *q;
q = p;
*p = *p + *q;
*q = *q + *p;
p = (int*) 0x1000;
int k = &p[3] – &p[1];
q = p + 2;
What is the value of
What is the value of
What is the value of
What is the value of
i ?
j ?
k ?
q ?
44
10
2
0x1008
5 of 11

6. Dynamic Allocation [10 marks]
Consider each of the following code blocks where add, doAdd, and doSomethingElse are in three different modules of a large program. For each block indicate whether the code has a memory leak or dangling pointer and which technique (if any) is the single best way (only one) to fix the bug or improve the code (even if it doesn’t have a bug). Select none if the code does not need to be improved or if the needed change isn’t listed.
a)
int* add(int *a, int *b, n) {
int* c = malloc(n * sizeof(int));
for (int i = 0; i s->i);
rc_free_ref(a->s);
free(a);
}
a = malloc(sizeof (struct A)); a->s = s; rc_keep_ref(a->s->i); rc_keep_ref(a->s);
}
c = c + 1;
*s->i = c;
}
8 of 11

//////////
// Module B
int bar() {
struct S* s = rc_malloc(sizeof(struct S)); s->i = rc_malloc(sizeof(int));
foo(s);
int t = *s->i;
rc_free_ref(s->i);
rc_free_ref(s);
return t;
}
//////////
// Shared between models A and B
struct S {
int* i;
};
//////////
// Module C
int main() {
int s = 0;
for (int i=0; i<100; i++)s = s + bar();printf(“%d
“, s);}9 of 11 8. Reading Assembly Code [10 marks]a) Add high-level, C-like comments to the following assembly program. Be sure to clearly indicate the location of high-level structure such as loops, if-statements, reading from or writing to variables or arrays etc. Partial marks on this question will be determined by the amount of high-level structure you identify. ld$a, r0 ld(r0), r0 ld$b, r1 ld(r1), r1 not r1 inc r1 ld$0, r2L0:mov r2, r3 add r1, r3 beq r3, L4 mov r2, r3inc r3L1: mov r3, r4add r1, r4beq r4, L3ld (r0, r2, 4), r5 ld(r0,r3,4),r6 mov r5, r7not r7inc r7add r6, r7bgt r7, L2st r6, (r0, r2, 4) st r5, (r0, r3, 4)L2:inc r3 br L1L3:inc r2 br L0L4: halt
#r0=&a#r0=a#r1=&b #r1=b=b’ #r1=~b’#r1=-b’ #r2=i’=0 #r3=i’#r3=i’ -b’# goto L4 if i’ == b’ #r3=i’#r3=j’ =i’+1 #r4=j’ #r4=j’ -b’ <= top of outer for loop (i’)<= top of inner for loop (j’)# goto L3 if#r5=a[i’]#r6=a[j’]#r7=a[i’]# a7 = ~a[i’]# a7 = -a[i’]#a7=a[j’]-a[i’]# goto L2 if a[j’] > a[i’]
# a[i’] = a[j’]’ if a[j’] <= a[i’] <= THEN: swap two # a[j’] = a[i’]’ if a[j’] <= a[i’] <= array elements # j’++# goto top of inner loop# i’++# goto top of outer loop <= IF statement<= end of inner loop<= end of outer loopj’==b’10 of 11<= load two values from array b) Give C code that does what this assembly code does. For full credit, your code must be straight-forward C, like you would write if you were writing this from scratch and not just translating it from the assembly code. for (int i=0; i

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] CS计算机代考程序代写 assembly CPSC 213 Midterm 2019W Term 1 (Oct 21, 2019) – Solution 1. Numbers and Memory [8 marks]
30 $