Question: #include #define NOINLINE _ _ attribute _ _ ( ( noinline ) ) static NOINLINE int function 1 ( int x , int y )

#include
#define NOINLINE __attribute__((noinline))
static NOINLINE int function1(int x, int y)
{
int i;
int sum;
int values[10];
sum =0;
for (i =0; i <10; i++){
values[i]=10+ i +2* x * y;
sum += values[i];
}
return (sum);
}
static int NOINLINE function2(int *values, int valuesLen)
{
int i;
int sum1;
int sum2;
int v;
sum1=0;
sum2=0;
for (i =0; i < valuesLen; i++){
v = values[i];
if (v >0)
sum1+= v;
else
sum2+= v;
}
return (sum1+ sum2);
}
static NOINLINE int function3(int x)
{
int y;
y = x /20;
return (y);
}
static NOINLINE int function4(int a, int b, int c, int d)
{
int r;
if (a > b)
r = b;
else if (a > c)
r =4* a;
else if (a > d)
r =8* a;
else
r =-1;
return (r);
}
static NOINLINE unsigned int function5(unsigned int x)
{
unsigned int x1;
unsigned int x2;
unsigned int x3;
x1= x *16;
x2= x /8;
x3= x %8;
return (x1+ x2+ x3);
}
int main(int argc, char **argv)
{
int i;
int j;
int k;
int values[10];
i =5;
j =8;
k = function1(i, j);
printf("function1: i =%d, j =%d, k =%d
", i, j, k);
for (i =0; i <10; i++){
values[i]= i;
}
k = function2(values,10);
printf("function2: k =%d
", k);
k = function3(100);
printf("function3: k =%d
", k);
k = function4(1,2,3,4);
printf("function4: k =%d
", k);
k = function5(100);
printf("function5: k =%d
", k);
return (0);
}
CODESEG
public strlen
strlen proc \
buf:ptr byte
OPTION PROLOGUE:NONE, EPILOGUE:NONE
.FPO (0,1,0,0,0,0)
string equ [esp +4]
mov ecx,string ; ecx -> string
test ecx,3 ; test if string is aligned on 32 bits
je short main_loop
str_misaligned:
; simple byte loop until string is aligned
mov al,byte ptr [ecx]
add ecx,1
test al,al
je short byte_3
test ecx,3
jne short str_misaligned
add eax,dword ptr 0 ; 5 byte nop to align label below
align 16 ; should be redundant
main_loop:
mov eax,dword ptr [ecx] ; read 4 bytes
mov edx,7efefeffh
add edx,eax
xor eax,-1
xor eax,edx
add ecx,4
test eax,81010100h
je short main_loop
; found zero byte in the loop
mov eax,[ecx -4]
test al,al ; is it byte 0
je short byte_0
test ah,ah ; is it byte 1
je short byte_1
test eax,00ff0000h ; is it byte 2
je short byte_2
test eax,0ff000000h ; is it byte 3
je short byte_3
jmp short main_loop ; taken if bits 24-30 are clear and bit
; 31 is set
byte_3:
lea eax,[ecx -1]
mov ecx,string
sub eax,ecx
ret
byte_2:
lea eax,[ecx -2]
mov ecx,string
sub eax,ecx
ret
byte_1:
lea eax,[ecx -3]
mov ecx,string
sub eax,ecx
ret
byte_0:
lea eax,[ecx -4]
mov ecx,string
sub eax,ecx
ret
strlen endp
end
1. Describe the stack at line 25(return (sum);). The bottom of the stack is the first row below. The top of the stack will be the last item. The first few items are provided. You will probably need to add some additional rows to the table below.
Description Value
First argument to main argv
Second argument to main argc
Return address of main Callers return address
C runtime value of bp Callers bp register
Local variable i in main [bp-12]5
2. In detail explain the code generated for line 105(k = function1(i, j);). For this and all further questions in which you are asked to explain the code generated in detail, I expect you to copy the generated code and add a comment for each line. For example:
006e C745FC00 mov DWORD PTR -4[ebp],0 ; initialize i
You can also refer to Week9-CAssembly-g.asm for examples of what you should submit.
3. In detail explain the code generated for function2(line 29 through 49).
4. In detail explain the code generated for function5(line 80 through 93).
The following questions should be answered using the optimized code.
5. In detail explain the code generated for function1(line 12 through 27). Why does this work?
6. How are function calls optimized? For example, see lines 114 and 117.
The following questions should be answered using by comparing the unoptimized code and the optimized code.
7. Compare the code generated by function4 in the unoptimized and optimized versions. Explain the optimizations.
Bonus 10 points
In detail explain the code generated for function3. This a hard. Try your best. Why is the compiler doing

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!