computer science coding

 computer science coding 


1.3. Basic Program Structure


Programs start out as source code, a collection of instructions usually written in a highlevel programming language. A source file containing source code is nothing more than


Depiction of Computer Memory. Each address refers to a byte, but differenttypes of data (integers, floating-point numbers, characters) may requiredifferent amounts of memory. Memory addresses and some data is representedin hexadecimal.

a plain text file that can be edited by any text editor. However, many developers andprogrammers utilize modern Integrated Development Environment (IDE) that provide atext editor with code highlighting: various elements are displayed in different colors tomake the code more readable and elements can be easily identified. Mistakes such asunclosed comments or curly brackets can be readily apparent with such editors. IDEs canalso provide automated compile/build features and other tools that make the developmentprocess easier and faster.


Some languages are compiled languages meaning that a source file must be translatedinto machine code that a processor can understand and execute. This is actually amultistep process. A compiler may first preprocess the source file(s) and perform somepre-compiler operations. It may then transform the source code into another languagesuch as an assembly language, a lower-level more machine-like language. Ultimately, thecompiler transforms the source code into object code, a binary format that the machinecan understand. 

To produce an executable file that can actually be run, a linker may then take the objectcode and link in any other necessary objects or precompiled library code necessary toproduce a final program. Finally, an executable file (still just a bunch of binary code) isproduced. 


Once an executable file has been produced we can run the program. When a program isexecuted, a request is sent to the operating system to load and run the program. Theoperating system loads the executable file into memory and may setup additional memoryfor its variables as well as its call stack (memory to enable the program to make functioncalls). Once loaded and setup, the operating system begins executing the instructions atthe program’s entry point. 


In many languages, a program’s entry point is defined by a main function or method.A program may contain many functions and pieces of code, but this special function isdefined as the one that gets invoked when a program starts. Without a main function,the code may still be useful: libraries contain many useful functions and procedures sothat you don’t have to write a program from scratch. However, these functions are notintended to be run by themselves. Instead, they are written so that other programs canuse them. A program becomes executable only when a main entry point is provided. 


This compile-link-execute process is roughly depicted in Code Sample 1.2. An example ofa simple C program can be found in Code Sample 1.1 along with the resulting assemblycode produced by a compiler in Figure 1.2 and the final machine code represented inhexadecimal in Code Sample 1.3.


In contrast, some languages are interpreted, not compiled. The source code is containedin a file usually referred to as a script. Rather than being run directly by an operatingsystem, the operating system loads and execute another program called an interpreter.The interpreter then loads the script, parses, and execute its instructions. Interpreted 



 

1. Introduction


languages may still have a predefined main function, but in general, a script startsexecuting starting with the first instruction in the script file. Adhering to the syntaxrules is still important, but since interpreted languages are not compiled, syntax errorsbecome runtime errors. A program may run fine until its first syntax error at whichpoint it fails.


There are other ways of compiling and running programs. Java for example represents acompromise between compiled and interpreted languages. Java source code is compiledinto Java bytecode which is not actually machine code that the operating system andhardware can run directly. Instead, it is compiled code for a Java Virtual Machine (JVM).This allows a developer to write highly portable code, compile it once and it is runnableon any JVM on any system (write-once, compile-once, run-anywhere).
 

In general, interpreted languages are slower than compiled languages because they arebeing run through another program (the interpreter) instead of being executed directlyby the processor. Modern tools have been introduced to solve this problem. Just In Time(JIT) compilers have been developed that take scripts that are not usually compiled,and compile them to a native machine code format which has the potential to run muchfaster than when interpreted. Modern web browsers typically do this for JavaScript code(Google Chrome’s V8 JavaScript engine for example). 


Another related technology are transpilers. Transpilers are source-to-source compilers.They don’t produce assembly or machine code, instead they translate code in onehigh-level programming language to another high-level programming language. Thisis sometimes done to ensure that scripting languages like JavaScript are backwardscompatible with previous versions of the language. Transpilers can also be used totranslate one language into the same language but with different aspects (such as parallelor synchronized code) automatically added. They can also be used to translate olderlanguages such as Pascal to more modern languages as a first step in updating a legacysystem. 



Post a Comment

0 Comments