SlideShare a Scribd company logo
20145-5SumII_CSC407_assign1.html
CSC 407: Computer Systems II: 2015 Summer II, Assignment
#1
Last Modified 2015 July 21Purpose:
To go over issues related to how the compiler and the linker
serve you, the programmer.
Computing
Please ssh into ctilinux1.cstcis.cti.depaul.edu, or use your own
Linux machine.
Compiler optimization (45 Points)
Consider the following program.
/* q1.c
*/
#include <stdlib.h>
#include <stdio.h>
#define unsigned int uint
#define LENGTH ((uint) 512*64)
int initializeArray(uint len,
int* intArray
)
{
uint i;
for (i = 0; i < len; i++)
intArray[i] = (rand() % 64);
}
uint countAdjacent (int maxIndex,
int* intArray,
int direction
)
{
uint i;
uint sum = 0;
for (i = 0; i < maxIndex; i++)
if ( ( intArray[i] == (intArray[i+1] + direction) ) &&
( intArray[i] == (intArray[i+2] + 2*direction) )
)
sum++;
return(sum);
}
uint funkyFunction (uint len,
int* intArray
)
{
uint i;
uint sum = 0;
for (i = 0; i < len-1; i++)
if ( (i % 8) == 0x3 )
sum += 7*countAdjacent(len-2,intArray,+1);
else
sum += 17*countAdjacent(len-2,intArray,-1);
return(sum);
}
int main ()
{
int* intArray = (int*)calloc(LENGTH,sizeof(int));
initializeArray(LENGTH,intArray);
printf("funkyFunction() ==
%dn",funkyFunction(LENGTH,intArray));
free(intArray);
return(EXIT_SUCCESS);
}
(8 Points) Compile it for profiling but with no extra
optimization with:
$ gcc -o q1None -pg q1.c # Compiles q1.c to write q1None to
make profile info
$ ./q1None # Runs q1None
$ gprof q1None # Gives profile info on q1None
Be sure to scroll all the way to the top of gprof output!
What are the number of self seconds taken by:
FunctionSelf
secondsinitializeBigArray()__________countAdjaceent()______
____funkyFunction()__________
(8 Points)
How did it do the operation (i % 8) == 0x3?
Was it done as a modulus (the same as an expensive
division, but returns the remainder instead of the quotient) or
something else?
Show the assembly language for this C code
using gdb to dissassemble
funkyFunction() of q1None.
Hint: do:
$ gdb q1None
. . .
(gdb) disass funkyFunction
Dump of assembler code for function funkyFunction:
. . .
and then look for the code that sets up the calls to
countAdjacent().
The (i % 8) == 0x3 test is done before either
countAdjacent() call.
(8 Points) Compile it for profiling but with optimization
with:
$ gcc -o q1Compiler -O1 -pg q1.c # Compiles q1.c to write
q1Compiler to make profile info
$ ./q1Compiler # Runs q1Compiler
$ gprof q1Compiler # Gives profile info on
q1Compiler
What are the number of self seconds taken by:
FunctionSelf
secondsinitializeBigArray()__________countAdjacent()_______
___funkyFunction()__________(8 Points) Use gdb to
dissassemble countAdjacent() of both q1None and q1Compiler.
Don't try to understand all the code but in general how
did the optimizer make q1Compiler's countAdjacent() faster
than q1None's?
Give a specific example by comparing both assembly
codes.
(8 Points) One optimization the compiler may not have
made would be do the two countAdjacent() calls once each
before the loop in funkyFunction(), put them in variables, and
then use those variables in the loop.
Re-write the code this way:
. . .
uint i;
uint sum = 0;
uint countUp = 7*countAdjacent(len-2,intArray,+1);
uint countDn = 17*countAdjacent(len-2,intArray,-1);
for (i = 0; i < len-1; i++)
if ( (i % 8) == 0x3 )
sum += countUp;
else
sum += countDn;
Compile it for profiling again with optimization with:
$ gcc -o q1Programmer -O1 -pg q1.c # Compiles q1.c to write
q1Programmer to make profile info
$ ./q1Programmer # Runs q1Programmer
$ gprof q1Programmer # Gives profile info on
q1Programmer
What are the number of self seconds taken by:
FunctionSelf
secondsinitializeBigArray()__________countAdjacent()_______
___funkyFunction()__________(5 Points) Which optimizations
ought to be done by the compiler?
Which optimizations ought to be done by the
programmer?
Linker operation (55 Points total)
The program file p1.c below compiles under Linux to create
an object file p1.o.
It is to be linked with another file p2.o to create a running
program, whole.
/* p1.c
*/
#include <stdlib.h>
#include <stdio.h>
//
// Declarations go here:
//
// PURPOSE: To hold the length of the array pointed to by
'intArray'.
extern int length;
// PURPOSE: To hold the array of integers.
extern int* intArray;
// PURPOSE: To hold the maximum value that may place in
array 'intArray'.
extern int maxRandVal;
// PURPOSE: To:
// (1) have user enter 'length' (by calling 'enterValue()'),
// (2) have user enter 'maxRandVal' (by calling
'enterValue()'),
// (3) define 'intArray' (say 'intArray
=(int*)calloc(length,sizeof(int))')
// (4) fill 'intArray' with random numbers between 0-
'maxRandVal'
// (use expression '(rand() % (maxRandVal+1))')
// No parameters. No return value.
extern void createArray ();
// PURPOSE: To print the values in 'intArray[]'. No
parameters. No return
// value.
extern void printArray ();
//
// Function and variables go here:
//
// PURPOSE: To hold the minimum value for 'length'.
int minArrayLen = 1;
// PURPOSE: To hold the maximum value for 'length'.
int maxArrayLen = 256;
// PURPOSE: To hold the maximum value for 'maxRandVal'.
int maxMaxRandVal = 256;
// PURPOSE: To hold the length of C-string 'line'.
#define MAX_LINE 256
// PURPOSE: To hold the a C-string.
char line[MAX_LINE];
// PURPOSE: To have the user enter the variable pointed to by
'valuePtr',
// which must be between 'min' and 'max', and which is
described by
// '*descriptionPtr'. No parameters. No return value.
void enterValue (const char* descriptionPtr,
int min,
int max,
int* valuePtr
)
{
do
{
printf("Please enter the %s (%d-%d):
",descriptionPtr,min,max);
fgets(line,MAX_LINE,stdin);
*valuePtr = strtol(line,NULL,10);
}
while ( (*valuePtr < min) || (*valuePtr > max) );
}
// PURPOSE: To free 'intArray'. No parameters. No return
value.
//
void freeArray ()
{
free(intArray);
}
// PURPOSE: To define the array, print an array, and free the
array. No
// Ignores parameters. Returns 'EXIT_SUCCESS' to OS.
int main ()
{
createArray();
printArray();
freeArray();
return(EXIT_SUCCESS);
}
Sample output:[[email protected] Assign1]$ gcc -c p1.c
[[email protected] Assign1]$ gcc -c p2.c
[[email protected] Assign1]$ gcc -o whole p1.o p2.o
[[email protected] Assign1]$ ./whole
Please enter the array's length (1-256): 0
Please enter the array's length (1-256): 257
Please enter the array's length (1-256): 16
Please enter the maximum random value (1-256): 0
Please enter the maximum random value (1-256): 257
Please enter the maximum random value (1-256): 4
The array is:
intArray[0] = 3
intArray[1] = 1
intArray[2] = 2
intArray[3] = 0
intArray[4] = 3
intArray[5] = 0
intArray[6] = 1
intArray[7] = 2
intArray[8] = 4
intArray[9] = 1
intArray[10] = 2
intArray[11] = 2
intArray[12] = 0
intArray[13] = 4
intArray[14] = 3
intArray[15] = 1
[[email protected] Assign1]$
(30 Points) Write a program file p2.c that would compile to
p2.o, and would link with p1.o to create a legal running
program whole.
Important: Include your p2.c with your submission!
HINTS:
What does p1.c need that it does not define?Be sure to
extern any necessary functions with the same parameters as they
are declared.
What does p1.c define that p2.c would need?
Be sure to extern both such variable and function
symbols.
Create the individual object files and the complete
executable with:
linux> gcc -c p1.c # creates p1.o
linux> gcc -c p2.c # creates p2.o
linux> gcc p1.o p2.o -o whole # creates whole
linux> ./whole # runs whole
(5 Points) Use objdump (not gdb!) to disassemble both main()
and createArray() in whole.
Find the call to createArray() in main().
Show the math of how the number in that call instruction
is used to compute the address where createArray() actually is.
(5 Points) Which segment (that is: .text, .rodata, .data or
.bss) of p1.o has string constant
used in enterValue()'s printf() call?
Show this with objdump.
(5 Points) Can you find the memory for enterValue()'s
variable min using objdump?
If you can, use objdump to show where it is.
If you can't, tell why not.
(5 Points) Which segment of p2.o has the function
printArray()?
Show this with objdump.
(5 Points) It is rather inelegant for both p1.c and
p2.c to have the code:
// PURPOSE: To hold the length of C-string 'line'.
#define MAX_LINE 256
because if we decide to make a change we have to
remember to change all .c files.
Suggest a more elegant solution, one that C encourages.

More Related Content

Similar to 20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx

C introduction by piyushkumar
C introduction by piyushkumarC introduction by piyushkumar
C introduction by piyushkumar
piyush Kumar Sharma
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
Mahmoud Samir Fayed
 
C tutorials
C tutorialsC tutorials
C tutorials
Amit Kapoor
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
Mahmoud Samir Fayed
 
JVM code reading -- C2
JVM code reading -- C2JVM code reading -- C2
JVM code reading -- C2
ytoshima
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
JavvajiVenkat
 
The Ring programming language version 1.5.4 book - Part 82 of 185
The Ring programming language version 1.5.4 book - Part 82 of 185The Ring programming language version 1.5.4 book - Part 82 of 185
The Ring programming language version 1.5.4 book - Part 82 of 185
Mahmoud Samir Fayed
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
PVS-Studio
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
mallik3000
 
The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.2 book - Part 59 of 84The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.2 book - Part 59 of 84
Mahmoud Samir Fayed
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Vivek Singh
 
Programming in C
Programming in CProgramming in C
Programming in C
Nishant Munjal
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
GCC RTL and Machine Description
GCC RTL and Machine DescriptionGCC RTL and Machine Description
GCC RTL and Machine Description
Priyatham Bollimpalli
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
javaTpoint s
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
CharuJain396881
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212
Mahmoud Samir Fayed
 

Similar to 20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx (20)

C introduction by piyushkumar
C introduction by piyushkumarC introduction by piyushkumar
C introduction by piyushkumar
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
C tutorials
C tutorialsC tutorials
C tutorials
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
 
JVM code reading -- C2
JVM code reading -- C2JVM code reading -- C2
JVM code reading -- C2
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
The Ring programming language version 1.5.4 book - Part 82 of 185
The Ring programming language version 1.5.4 book - Part 82 of 185The Ring programming language version 1.5.4 book - Part 82 of 185
The Ring programming language version 1.5.4 book - Part 82 of 185
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.2 book - Part 59 of 84The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.2 book - Part 59 of 84
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Programming in C
Programming in CProgramming in C
Programming in C
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
GCC RTL and Machine Description
GCC RTL and Machine DescriptionGCC RTL and Machine Description
GCC RTL and Machine Description
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Functions
FunctionsFunctions
Functions
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212
 

More from eugeniadean34240

I need a 7 pg research essay on the following   Select a real o.docx
I need a 7 pg research essay on the following   Select a real o.docxI need a 7 pg research essay on the following   Select a real o.docx
I need a 7 pg research essay on the following   Select a real o.docx
eugeniadean34240
 
I need a 4-5 APA formatted paper with references that is clearly wri.docx
I need a 4-5 APA formatted paper with references that is clearly wri.docxI need a 4-5 APA formatted paper with references that is clearly wri.docx
I need a 4-5 APA formatted paper with references that is clearly wri.docx
eugeniadean34240
 
I need a 3 page research paper on Title  Addictive being youn.docx
I need a 3 page research paper on Title  Addictive being youn.docxI need a 3 page research paper on Title  Addictive being youn.docx
I need a 3 page research paper on Title  Addictive being youn.docx
eugeniadean34240
 
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docxI need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
eugeniadean34240
 
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docxI need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
eugeniadean34240
 
I need a 200-word paper that answers the following questions:D.docx
I need a 200-word paper that answers the following questions:D.docxI need a 200-word paper that answers the following questions:D.docx
I need a 200-word paper that answers the following questions:D.docx
eugeniadean34240
 
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docxi need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
eugeniadean34240
 
I need a 1 page professional bio. My cover letter and resume i.docx
I need a 1 page professional bio. My cover letter and resume i.docxI need a 1 page professional bio. My cover letter and resume i.docx
I need a 1 page professional bio. My cover letter and resume i.docx
eugeniadean34240
 
I need 100 words response for this two discussion forum1 discu.docx
I need 100 words response for this two discussion forum1 discu.docxI need 100 words response for this two discussion forum1 discu.docx
I need 100 words response for this two discussion forum1 discu.docx
eugeniadean34240
 
I need 200 words response for each discussion post.Guided Respon.docx
I need 200 words response for each discussion post.Guided Respon.docxI need 200 words response for each discussion post.Guided Respon.docx
I need 200 words response for each discussion post.Guided Respon.docx
eugeniadean34240
 
I need 3 pages discussion for an intersection (Attached image).docx
I need 3 pages discussion for an intersection (Attached image).docxI need 3 pages discussion for an intersection (Attached image).docx
I need 3 pages discussion for an intersection (Attached image).docx
eugeniadean34240
 
I need 1page write up on Hypothesis & Methods Proposal,Due on .docx
I need 1page write up on Hypothesis & Methods Proposal,Due on .docxI need 1page write up on Hypothesis & Methods Proposal,Due on .docx
I need 1page write up on Hypothesis & Methods Proposal,Due on .docx
eugeniadean34240
 
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docxI need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
eugeniadean34240
 
I need 120 words for each question. Please ensure to post individual.docx
I need 120 words for each question. Please ensure to post individual.docxI need 120 words for each question. Please ensure to post individual.docx
I need 120 words for each question. Please ensure to post individual.docx
eugeniadean34240
 
I need 10-12 slides Presentation with detailed speaker notes. Instru.docx
I need 10-12 slides Presentation with detailed speaker notes. Instru.docxI need 10-12 slides Presentation with detailed speaker notes. Instru.docx
I need 10-12 slides Presentation with detailed speaker notes. Instru.docx
eugeniadean34240
 
I N N O V A T I O N N E T W O R K , I N C . www.innone.docx
I N N O V A T I O N  N E T W O R K ,  I N C .   www.innone.docxI N N O V A T I O N  N E T W O R K ,  I N C .   www.innone.docx
I N N O V A T I O N N E T W O R K , I N C . www.innone.docx
eugeniadean34240
 
I like to tie my learning to Biblical Principles. On Virtuous Le.docx
I like to tie my learning to Biblical Principles. On Virtuous Le.docxI like to tie my learning to Biblical Principles. On Virtuous Le.docx
I like to tie my learning to Biblical Principles. On Virtuous Le.docx
eugeniadean34240
 
I just want one paragraph.!!C.W.Mills described ‘sociological im.docx
I just want one paragraph.!!C.W.Mills described ‘sociological im.docxI just want one paragraph.!!C.W.Mills described ‘sociological im.docx
I just want one paragraph.!!C.W.Mills described ‘sociological im.docx
eugeniadean34240
 
i just need serious help answering the question. I have answered mos.docx
i just need serious help answering the question. I have answered mos.docxi just need serious help answering the question. I have answered mos.docx
i just need serious help answering the question. I have answered mos.docx
eugeniadean34240
 
I Headnotes and indexes are copyrighted and may not be duplica.docx
I Headnotes and indexes are copyrighted and may not be duplica.docxI Headnotes and indexes are copyrighted and may not be duplica.docx
I Headnotes and indexes are copyrighted and may not be duplica.docx
eugeniadean34240
 

More from eugeniadean34240 (20)

I need a 7 pg research essay on the following   Select a real o.docx
I need a 7 pg research essay on the following   Select a real o.docxI need a 7 pg research essay on the following   Select a real o.docx
I need a 7 pg research essay on the following   Select a real o.docx
 
I need a 4-5 APA formatted paper with references that is clearly wri.docx
I need a 4-5 APA formatted paper with references that is clearly wri.docxI need a 4-5 APA formatted paper with references that is clearly wri.docx
I need a 4-5 APA formatted paper with references that is clearly wri.docx
 
I need a 3 page research paper on Title  Addictive being youn.docx
I need a 3 page research paper on Title  Addictive being youn.docxI need a 3 page research paper on Title  Addictive being youn.docx
I need a 3 page research paper on Title  Addictive being youn.docx
 
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docxI need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
 
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docxI need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
 
I need a 200-word paper that answers the following questions:D.docx
I need a 200-word paper that answers the following questions:D.docxI need a 200-word paper that answers the following questions:D.docx
I need a 200-word paper that answers the following questions:D.docx
 
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docxi need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
 
I need a 1 page professional bio. My cover letter and resume i.docx
I need a 1 page professional bio. My cover letter and resume i.docxI need a 1 page professional bio. My cover letter and resume i.docx
I need a 1 page professional bio. My cover letter and resume i.docx
 
I need 100 words response for this two discussion forum1 discu.docx
I need 100 words response for this two discussion forum1 discu.docxI need 100 words response for this two discussion forum1 discu.docx
I need 100 words response for this two discussion forum1 discu.docx
 
I need 200 words response for each discussion post.Guided Respon.docx
I need 200 words response for each discussion post.Guided Respon.docxI need 200 words response for each discussion post.Guided Respon.docx
I need 200 words response for each discussion post.Guided Respon.docx
 
I need 3 pages discussion for an intersection (Attached image).docx
I need 3 pages discussion for an intersection (Attached image).docxI need 3 pages discussion for an intersection (Attached image).docx
I need 3 pages discussion for an intersection (Attached image).docx
 
I need 1page write up on Hypothesis & Methods Proposal,Due on .docx
I need 1page write up on Hypothesis & Methods Proposal,Due on .docxI need 1page write up on Hypothesis & Methods Proposal,Due on .docx
I need 1page write up on Hypothesis & Methods Proposal,Due on .docx
 
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docxI need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
 
I need 120 words for each question. Please ensure to post individual.docx
I need 120 words for each question. Please ensure to post individual.docxI need 120 words for each question. Please ensure to post individual.docx
I need 120 words for each question. Please ensure to post individual.docx
 
I need 10-12 slides Presentation with detailed speaker notes. Instru.docx
I need 10-12 slides Presentation with detailed speaker notes. Instru.docxI need 10-12 slides Presentation with detailed speaker notes. Instru.docx
I need 10-12 slides Presentation with detailed speaker notes. Instru.docx
 
I N N O V A T I O N N E T W O R K , I N C . www.innone.docx
I N N O V A T I O N  N E T W O R K ,  I N C .   www.innone.docxI N N O V A T I O N  N E T W O R K ,  I N C .   www.innone.docx
I N N O V A T I O N N E T W O R K , I N C . www.innone.docx
 
I like to tie my learning to Biblical Principles. On Virtuous Le.docx
I like to tie my learning to Biblical Principles. On Virtuous Le.docxI like to tie my learning to Biblical Principles. On Virtuous Le.docx
I like to tie my learning to Biblical Principles. On Virtuous Le.docx
 
I just want one paragraph.!!C.W.Mills described ‘sociological im.docx
I just want one paragraph.!!C.W.Mills described ‘sociological im.docxI just want one paragraph.!!C.W.Mills described ‘sociological im.docx
I just want one paragraph.!!C.W.Mills described ‘sociological im.docx
 
i just need serious help answering the question. I have answered mos.docx
i just need serious help answering the question. I have answered mos.docxi just need serious help answering the question. I have answered mos.docx
i just need serious help answering the question. I have answered mos.docx
 
I Headnotes and indexes are copyrighted and may not be duplica.docx
I Headnotes and indexes are copyrighted and may not be duplica.docxI Headnotes and indexes are copyrighted and may not be duplica.docx
I Headnotes and indexes are copyrighted and may not be duplica.docx
 

Recently uploaded

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 

Recently uploaded (20)

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 

20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx

  • 1. 20145-5SumII_CSC407_assign1.html CSC 407: Computer Systems II: 2015 Summer II, Assignment #1 Last Modified 2015 July 21Purpose: To go over issues related to how the compiler and the linker serve you, the programmer. Computing Please ssh into ctilinux1.cstcis.cti.depaul.edu, or use your own Linux machine. Compiler optimization (45 Points) Consider the following program. /* q1.c */ #include <stdlib.h> #include <stdio.h> #define unsigned int uint #define LENGTH ((uint) 512*64) int initializeArray(uint len, int* intArray ) { uint i;
  • 2. for (i = 0; i < len; i++) intArray[i] = (rand() % 64); } uint countAdjacent (int maxIndex, int* intArray, int direction ) { uint i; uint sum = 0; for (i = 0; i < maxIndex; i++) if ( ( intArray[i] == (intArray[i+1] + direction) ) && ( intArray[i] == (intArray[i+2] + 2*direction) ) ) sum++; return(sum); } uint funkyFunction (uint len, int* intArray ) { uint i; uint sum = 0; for (i = 0; i < len-1; i++) if ( (i % 8) == 0x3 ) sum += 7*countAdjacent(len-2,intArray,+1); else
  • 3. sum += 17*countAdjacent(len-2,intArray,-1); return(sum); } int main () { int* intArray = (int*)calloc(LENGTH,sizeof(int)); initializeArray(LENGTH,intArray); printf("funkyFunction() == %dn",funkyFunction(LENGTH,intArray)); free(intArray); return(EXIT_SUCCESS); } (8 Points) Compile it for profiling but with no extra optimization with: $ gcc -o q1None -pg q1.c # Compiles q1.c to write q1None to make profile info $ ./q1None # Runs q1None $ gprof q1None # Gives profile info on q1None Be sure to scroll all the way to the top of gprof output! What are the number of self seconds taken by: FunctionSelf secondsinitializeBigArray()__________countAdjaceent()______ ____funkyFunction()__________ (8 Points) How did it do the operation (i % 8) == 0x3? Was it done as a modulus (the same as an expensive division, but returns the remainder instead of the quotient) or something else? Show the assembly language for this C code
  • 4. using gdb to dissassemble funkyFunction() of q1None. Hint: do: $ gdb q1None . . . (gdb) disass funkyFunction Dump of assembler code for function funkyFunction: . . . and then look for the code that sets up the calls to countAdjacent(). The (i % 8) == 0x3 test is done before either countAdjacent() call. (8 Points) Compile it for profiling but with optimization with: $ gcc -o q1Compiler -O1 -pg q1.c # Compiles q1.c to write q1Compiler to make profile info $ ./q1Compiler # Runs q1Compiler $ gprof q1Compiler # Gives profile info on q1Compiler What are the number of self seconds taken by: FunctionSelf secondsinitializeBigArray()__________countAdjacent()_______ ___funkyFunction()__________(8 Points) Use gdb to dissassemble countAdjacent() of both q1None and q1Compiler. Don't try to understand all the code but in general how
  • 5. did the optimizer make q1Compiler's countAdjacent() faster than q1None's? Give a specific example by comparing both assembly codes. (8 Points) One optimization the compiler may not have made would be do the two countAdjacent() calls once each before the loop in funkyFunction(), put them in variables, and then use those variables in the loop. Re-write the code this way: . . . uint i; uint sum = 0; uint countUp = 7*countAdjacent(len-2,intArray,+1); uint countDn = 17*countAdjacent(len-2,intArray,-1); for (i = 0; i < len-1; i++) if ( (i % 8) == 0x3 ) sum += countUp; else sum += countDn; Compile it for profiling again with optimization with: $ gcc -o q1Programmer -O1 -pg q1.c # Compiles q1.c to write q1Programmer to make profile info $ ./q1Programmer # Runs q1Programmer $ gprof q1Programmer # Gives profile info on q1Programmer What are the number of self seconds taken by: FunctionSelf secondsinitializeBigArray()__________countAdjacent()_______ ___funkyFunction()__________(5 Points) Which optimizations ought to be done by the compiler?
  • 6. Which optimizations ought to be done by the programmer? Linker operation (55 Points total) The program file p1.c below compiles under Linux to create an object file p1.o. It is to be linked with another file p2.o to create a running program, whole. /* p1.c */ #include <stdlib.h> #include <stdio.h> // // Declarations go here: // // PURPOSE: To hold the length of the array pointed to by 'intArray'. extern int length; // PURPOSE: To hold the array of integers. extern int* intArray; // PURPOSE: To hold the maximum value that may place in array 'intArray'. extern int maxRandVal;
  • 7. // PURPOSE: To: // (1) have user enter 'length' (by calling 'enterValue()'), // (2) have user enter 'maxRandVal' (by calling 'enterValue()'), // (3) define 'intArray' (say 'intArray =(int*)calloc(length,sizeof(int))') // (4) fill 'intArray' with random numbers between 0- 'maxRandVal' // (use expression '(rand() % (maxRandVal+1))') // No parameters. No return value. extern void createArray (); // PURPOSE: To print the values in 'intArray[]'. No parameters. No return // value. extern void printArray (); // // Function and variables go here: // // PURPOSE: To hold the minimum value for 'length'. int minArrayLen = 1; // PURPOSE: To hold the maximum value for 'length'. int maxArrayLen = 256; // PURPOSE: To hold the maximum value for 'maxRandVal'. int maxMaxRandVal = 256; // PURPOSE: To hold the length of C-string 'line'.
  • 8. #define MAX_LINE 256 // PURPOSE: To hold the a C-string. char line[MAX_LINE]; // PURPOSE: To have the user enter the variable pointed to by 'valuePtr', // which must be between 'min' and 'max', and which is described by // '*descriptionPtr'. No parameters. No return value. void enterValue (const char* descriptionPtr, int min, int max, int* valuePtr ) { do { printf("Please enter the %s (%d-%d): ",descriptionPtr,min,max); fgets(line,MAX_LINE,stdin); *valuePtr = strtol(line,NULL,10); } while ( (*valuePtr < min) || (*valuePtr > max) ); } // PURPOSE: To free 'intArray'. No parameters. No return value. // void freeArray () {
  • 9. free(intArray); } // PURPOSE: To define the array, print an array, and free the array. No // Ignores parameters. Returns 'EXIT_SUCCESS' to OS. int main () { createArray(); printArray(); freeArray(); return(EXIT_SUCCESS); } Sample output:[[email protected] Assign1]$ gcc -c p1.c [[email protected] Assign1]$ gcc -c p2.c [[email protected] Assign1]$ gcc -o whole p1.o p2.o [[email protected] Assign1]$ ./whole Please enter the array's length (1-256): 0 Please enter the array's length (1-256): 257 Please enter the array's length (1-256): 16 Please enter the maximum random value (1-256): 0 Please enter the maximum random value (1-256): 257 Please enter the maximum random value (1-256): 4 The array is: intArray[0] = 3 intArray[1] = 1 intArray[2] = 2 intArray[3] = 0 intArray[4] = 3 intArray[5] = 0 intArray[6] = 1 intArray[7] = 2 intArray[8] = 4 intArray[9] = 1 intArray[10] = 2
  • 10. intArray[11] = 2 intArray[12] = 0 intArray[13] = 4 intArray[14] = 3 intArray[15] = 1 [[email protected] Assign1]$ (30 Points) Write a program file p2.c that would compile to p2.o, and would link with p1.o to create a legal running program whole. Important: Include your p2.c with your submission! HINTS: What does p1.c need that it does not define?Be sure to extern any necessary functions with the same parameters as they are declared. What does p1.c define that p2.c would need? Be sure to extern both such variable and function symbols. Create the individual object files and the complete executable with: linux> gcc -c p1.c # creates p1.o linux> gcc -c p2.c # creates p2.o linux> gcc p1.o p2.o -o whole # creates whole linux> ./whole # runs whole (5 Points) Use objdump (not gdb!) to disassemble both main() and createArray() in whole. Find the call to createArray() in main(). Show the math of how the number in that call instruction is used to compute the address where createArray() actually is.
  • 11. (5 Points) Which segment (that is: .text, .rodata, .data or .bss) of p1.o has string constant used in enterValue()'s printf() call? Show this with objdump. (5 Points) Can you find the memory for enterValue()'s variable min using objdump? If you can, use objdump to show where it is. If you can't, tell why not. (5 Points) Which segment of p2.o has the function printArray()? Show this with objdump. (5 Points) It is rather inelegant for both p1.c and p2.c to have the code: // PURPOSE: To hold the length of C-string 'line'. #define MAX_LINE 256 because if we decide to make a change we have to remember to change all .c files. Suggest a more elegant solution, one that C encourages.