2
Syllabus What is a Program? What is a Computer? Holding Data Programming Languages Machine Language Sample Compiler Linker Loader Debugger Quick Excursion to C++ While Loops in C++

3
Acknowledgments Numerous graphs and pictures used here have been copied as-is from ECE 103 teaching material developed by professor Phillip Wong at PSU

4
What is a Program A computer program is a sequence of executable machine instructions, reading information (input), computing new data, and generating output, according to the steps of an algorithm An algorithm is a finite sequence of instructions, reading information (input), computing new data, and generating output The two are almost synonymous, except that the program executes (AKA runs) on a computer, the algorithm is just an abstraction in our mind or written on paper The algorithm coded in a programming language becomes your program

5
What is a Computer A computer is a physical device that can read input, compute, and produce corresponding output It “understands” a small set of machine instructions, which it can execute, one at a time (for a uni-processor) It is possible, but very tedious, to write a program in such machine instructions An Assembler eases this tediousness by allowing users to write abstract data and symbolic instructions, which the assembler then translates into machine code A compiler (similar: an interpreter) reads higher-level programs and maps them into assembly code; or sometimes into machine code directly

8
Holding Data A computer holds a good amount of data in memory The size of such a memory (AKA primary storage) is defined by the architecture’s address range E.g. on a 32-bit architecture, memory can be as large as 2 32 different addressable units Often such units are bytes, i.e. 8-bit addressable, contiguous units; other architectures use 60-bit words Large amounts of data are stored on secondary storage devices, such as rotating disks or SSDs Access to data in memory is slow, about times slower than executing one instruction Access to data on disk is even 10,000s of times slower

9
Holding Data Getting data from memory into the processor (usually a machine register) is called a load operation Moving data from a register to memory is called a store operation Such transport proceeds on a bus; the width of the bus and its speed are critical for the overall execution speed of the machine and thus of your programs

10
Programming Languages Even writing assembly source code is highly tedious Since the 1960s, higher-level programming languages have been developed Some of these are machine-independent; others highly machine-dependent (AKA architecture- dependent) A machine-independent language allows writing of source programs (code) that can be executed on different machines, provided a compiler is available on each This is referred to a portability of source programs, quite a desirable property!

14
Compiler Compiler is a special-purpose system program that reads source programs, written in the source language and translates them into machine language Mapping into machine language (AKA object code) sometimes involves an intermediate step: Creating assembly source first, and then using the assembler to generate machine code A compiler generally understands just one input language; exceptions are some C++ compilers that also read C source Compilers emit error messages when certain violations are detected

16
Compiler preprocessor → handles preprocessor directives and expands macro definitions compiler → takes preprocessed source code files and translates them to intermediate code; for beginners it is convenient to view the other system programs as part of the compiler assembler → takes intermediate code files and translates them to binary object code linker → resolves references among the object files and the libraries. It puts all the parts together to create the final executable file

18
Linker Often programs are composed of multiple source programs For example, some projects are too large to have a single programmer develop all code in sequence Also system function, such as input, output, heap acquisitions etc. are provided in the PDE, and do need to be coded by the programmer All such elements are linked together into a single, executable object program That is the work of the system’s linker For C on Unix the link step is frequently hidden, i.e. not visible to the programmer

19
Loader When a program has been linked, it is still not executable Instead, it is just a binary file, residing on some disk, as an object file To run such object code, it must be loaded into memory and be granted processor execution time That is the purpose of the system laoder

22
Quick Excursion to C++ You now know C++ arrays The type of an array element may be any legal C++ type, including array, in which case you declare multi- dimensional arrays To manipulate arrays, generally you need loops Loops are syntactic constructs that allow the programmer to do steps repetitively We also call this: iteratively Loops iterate over a so called iteration space C++ has while, for, and do loops Here we discuss while loops

23
While Loop in C++ Formally, there are no while statements, just expressions in C++, we ignore this language politics for the moment A while statement consists of 3 parts: 1. Reserved keyword while 2. A parenthesized expression, interpreted as boolean 3. And 1 statement (formally an expression) Example: while( i < MAX_SIZE ) { process_vector( i++ ): } // end while The single statement may, of course, be a compound statement

24
While Loop in C++ A while statement executes, as long as the boolean expression in the pair of ( and ) yields true When the expression is false, the while statement ends, and the operation after the while is executed next Usually, some operation in the body of the while –inside the single statement, which may be a compound statement-- must ensure that the boolean condition is false eventually Else you constructed an infinite loop Note that the boolean condition could be false at the start, in which case the wile is empty, i.e. executed 0 times