6. Consider the following cortex-m3 assembly code 1 loop PROC 4 label MOV MOV r2
ID: 3771508 • Letter: 6
Question
6. Consider the following cortex-m3 assembly code 1 loop PROC 4 label MOV MOV r2,#0xffffffff r3 , #1 AND EOR LSLS BNE MOV BX ENDP r4,r3,r0 r2,r2,r4 r2,r2,r1 label r0,r2 10 The above code was generated by compiling C code that had the following overall form: 1 int loop(int x, int n) int result- for (int mask = ----_---; mask ; mask result Your task is to fill in the missing parts of the C code, and to answer several questions. Recall that the result of the function is returned in rO. You will find it helpful to examine the assembly code before, during, and after the loop to form a consistent mapping between the registers and the program variables (a) Which registers hold the values x, n, result, and mask? (b) What are the initial values of result and mask? (c) What is the test condition for mask? (d) What is the C expression that updates mask? (e) What is the C expression that updates result? (f) Fill in all the missing parts of the C codeExplanation / Answer
6)
A)
of course these answers are direct ones . so I hope explaination not required.
x value stored in %esi
n value stored in %ebx
result value stored in %edi
mask value stored in %edx
--------------------------------------------------------------------------------
B)
Initial values of result is -1 and mask is 1;
----------------------------------------------------------------
C)
Mask test condition is
mask should not be zero ==> mask not zero
-----------------------------------------------------------------------------
D) How mask update?
As already in first question, I explained...
Register %edx holds the value of mask. so mask gets update eevery time %edx is left shifted
to n bits in program line number 10.
"test1" imstruction in line 11 does not change value but it alters flag value.
--------------------------------------------------------------------------------
E)
int loop(int x, int n){
int result = -1;
int mask;
for(mask=1;mask!=0;mask = mask<<n)}
result ^= +(mask & x)
}
return result;
}
-------------------------------------------------------------------------------------------------------------------------------------------
8)
a)
struct {
char * a; ---------> 1
short b; ---------> 2
double c; ---------> 8
char d; ----------> 1
float e; ----------> 4
char f; ----------> 1
int g; -----------> 4
}foo;
As you see the sizes of each variable in structure.
so initital offset = 0;
starts with char (1) so offset = 1
next short(2) so it has move one place ahead, so offset = 2
nex double ... since offset is 2 , it has to move 6 places ahead --> offset = 8
next char(1) so it has to move left to again 7 places.. offset--> 15
nest float(3) so so Total offset = 18
next char(1) so Total offset = 21
next int(4) so total offset = 24
---------------------------------------------------------------------------------------
b) byte offset of all fields in structure?
struct {
char * a; ---------> 1
short b; ---------> 2
double c; ---------> 8
char d; ----------> 1
float e; ----------> 4
char f; ----------> 1
int g; -----------> 4
}foo;
*** Total = 1+2+8+1+4+1+4 ==> 21
-----------------------------------------------------------------
c)
So for every instruction we have to place 8 bits ... if char 1 bit data byte + 7 bits padding bytes
struct {
char * a; ---------> 1 padding 7
short b; ---------> 2 padding 6
double c; ---------> 8 padding 0
char d; ----------> 1 padding 7
float e; ----------> 4 padding 4
char f; ----------> 1 padding 7
int g; -----------> 4 padding 4
}foo;
-------------------------------------------------------------------------------------------
d)
To minimise memory wastage ...
declaring of variables in structure , modify this way
struct {
char * a; ---------> 1
char f; ----------> 1
char d; ----------> 1
short b; ---------> 2
float e; ----------> 4
int g; -----------> 4
double c; ---------> 8
}foo;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.