ebook img

program with the Pascal PDF

216 Pages·2010·7.44 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview program with the Pascal

Programming Arcana Pascal Andrew Cain March 1, 2010 2 Introduction WelcometotheProgrammingArcana1-abookaboutlearningtoprogramwiththePascalProgramming Language. This book contains a number of lessons that take you from knowing nothing, or little, about program- ming to a position where the mysteries are revealed. By the end of the material you will be able to create your own programs to perform tasks you are interested in achieving. Understanding the knowl- edge contained here will mean that you can then turn your attention to learning other languages and ways of creating programs. These lessons are divided into chapters, each of which introduces you to a programming task and the arcane knowledge that must be attained to understand how the task is accomplished. Much of your initial learning will focus on the terms that software developers use to discuss and describe their programs. Understandingthesetermsiscrucial,aclearunderstandingofthesewillenableyoutodiscuss programs and how they are created with others. 1Arcana is defined as secrets or mysteries, Wiktionary defines it as “specialized knowledge that is mysterious to the uninitiated.” -butwejustthinkitsacoolwordtodescribethemagic ofprogramming. 3 4 Program Primer What is a program? Programsaresetsofinstructionsusedtogetacomputertoperformcertainactions. Ineffect,acomputer program is just a list of instructions that tell the computer what to do, and the order in which to do it. Each instruction is very simple, but they can be executed very quickly, allowing computers to perform quite remarkable feats. So why is this building software challenging? The key to starting to understand the issues with software development comes from realising that the computer is unintelligent. The computer is a machine that can use instructions to manipulate data. It doesn’t have the ability to interpret instructions, or to make assumptions about what it should do. The computer blindly performs the instructions, regardless of their outcomes or your intentions. The challenge therefore, is to be able to craft the instructions so that there is no need for interpretation. The instructions cannot need intelligence, the computer has none! There are basically two things that we need to keep in mind when starting to learn to program. These are: 1. Computers can execute only very simple instructions 2. Computers are unintelligent, they follow instructions without interpretation What is an algorithm? Algorithm In mathematics, computing, linguistics, and related disciplines, an algo- rithm is a procedure (a finite set of well-defined instructions) for accomplishing some task which, given an initial state, will terminate in a defined end-state.? Analgorithmisasetofinstructionsthatdefinehowtoperformorcalculatesomesolutiontoaproblem. Algorithmsshouldbeexplicit,sothatthesameansweriscalculatedwhenexecutedmultipletimeswith the same input data, regardless of who or what is performing the actions. If an algorithm is a set of instructions how does it differ from a computer program? Algorithms,asdescribedabove,canbeexpressedinavarietyofdifferentways. Youcandrawalgorithms graphically with flowcharts, using English, or any number of other variations. Programs on the other hand must be expressed in such as way that it can be executed by a computer. A program is a kind of algorithm, one that can be executed by a computer. 5 Writing Programs Programs are algorithms expressed in such a way that it can be executed by a computer. There are several different ways of creating programs. Lets start by looking at the lowest level2 approach and move up from there. Machine Code Computers execute machine code. Machine code is a binary language (consisting of 1s and 0s) that is unique to each kind of processor. The instructions that it can understand are called its instruction set and only contains a few very simple instructions. Programming at this level is very slow and entirely dependent on the kind of processor that you are targeting. As a result programmers tend not to work at this level, but to use a higher level language. The following is a chunk of the machine code for a small program. While it looks like a random list of 1s and 0s to us, these files contain the code that is used to instruct the computer when the program is executed. ... 0110 0111 0111 0010 0000 0000 0110 0011 0100 1110 0101 1111 0100 0001 0101 1000 0110 0111 0111 0010 0000 0000 0111 0110 0101 1111 0101 1111 0110 1001 0101 1111 ... Listing 1: Machine Code Sample Exercise 1: Letshavealookatthecodethatexistsinsideoneoftheexecutableprogramsthatyou have on your computer. To do this we will use a program that shows you the data that exists within the files you ask it to examine. These programs will show you a hexadecimal view of the data, showing you in base 16, rather than binary (base 2). • If you have a Linux machine or Mac you can use the built in hexdump program. In this case open a Terminal window and execute "hexdump /usr/bin/hexdump" this will then list the machine code for the file that you pass in. • OnaWindowsmachineyouwillneedtodownloadahexeditorandusethattoopenanexecutable file (.exe file). Use Google to locate a suitable program and follow its instructions for usage. 2This refers to the level of abstraction. The lowest level of abstraction has the most details, which higher levels aim toabstract. 6 Assembler Code The next level of abstraction up from machine code is called Assembler Code. Here the numeric machine code instructions are given symbolic names that are, to some degree, more understandable for humans. Programs written is this language cannot be executed directly by the computer, it isn’t machinecode. AssemblercodeisconvertedtomachinecodebyaprogramcalledanAssembler,hence the name assembler code. This program reads the instructions from the assembler code and outputs the appropriate machine code. The code in Listing 2 shows an example of some assembler code. This is the assembler code that was used to generate the machine code from Listing 1. The machine code was 13,344 bytes in size, where the same program in assembler code is only 658 bytes. The assembler reads these 658 bytes, uses instructions in program libraries, and outputs machine code. .cstring LC0: .ascii "Hello\0" .text .globl _main _main: pushl %ebp movl %esp, %ebp pushl %ebx subl $20, %esp call ___i686.get_pc_thunk.bx "L000001$pb": leal LC0-"L000001$pb"(%ebx), %eax movl %eax, (%esp) call L_printf$stub addl $20, %esp popl %ebx popl %ebp ret Listing 2: Assembler Sample Fromaprogrammersperspectiveassemblercodeismucheasiertoworkwiththanmachinecode,though there are still issues with the use of assembler code. Firstly assembler code is bound to the instruction set of the CPU that you are targeting, meaning that if you want to support other kinds of CPU you will need to rewrite the program. The other main issue with assembler code is that while it is more understandable, you are still working with the primitive instructions of the CPU. Working at this level takes considerable effort to write even simple programs. 7 Programming Languages The next step in programming languages moved from machine level instructions, to something more human readable. These languages, generally called third generation programming languages, use some other program to convert their instructions into machine code. Programs written in these languages are either compiled into machine code or are executed by an interpreter. Compiler A program that converts source code into machine code that is persisted into a program. The resulting program can then be executed independent of the compiler and the source code. Interpreter Aprogramthatreadsinstructionsfromsourcecode, convertsthemtomachinecode, and executes the machine code. To execute the instructions in the source code a second time requires the code to be reinterpreted. The code that a programmer writes in these languages is called source code. Typically source code is saved into a text file with a file extension that identifies the language. For example, programs written in the C language are saved into files with a .c file extension whereas Pascal programs are saved into files with a .pas extension. #include <stdio.h> int main() { printf("Hello"); } Listing 3: C Sample The code shown in Listing 3 shows the source code for the C program that was used to generate the assembler code, and machine code shown in the previous source code listings. In order to execute this program we first need to convert the C code into machine code using the C compiler. Notice the size of the C file. Its only 50 bytes. The compiler converts this 50 bytes into the 13,344 bytes of machine code. Exercise 2: If you are using a Linux or Mac computer your system will include a C compiler. You can use this compiler to compile the source code from Listing 3. 1. Install the developer tools from the operating system installation discs, if you haven’t already done so 2. Copy the source code into a text file called Hello.c 3. Use the following instruction at the console to compile the code: gcc -o Hello Hello.c 4. Run the program using: ./Hello Programs written in a third generation programming language are much more understandable than their assembler code counterparts. It is also possible that this code can be compiled to run on different types of CPU, making it more portable. Most modern programming languages are third generation programming languages. 8 Part I Core Pascal Secrets 9

Description:
Programming Arcana. Pascal. Andrew Cain. March 1, 2010 It doesn't have the ability to interpret instructions, or to make assumptions about what it should do. WriteLn('My name is ''Fred '', what is your name?'); WriteLn .. Listing 5.2: DoubleIt and MakeZero procedure using reference parameters.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.