ebook img

Software Development for Engineers. with C, Pascal, C++, Assembly Language, Visual Basic, HTML, JavaScript and Java PDF

640 Pages·1997·14.082 MB·
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 Software Development for Engineers. with C, Pascal, C++, Assembly Language, Visual Basic, HTML, JavaScript and Java

Specialization in software development is becoming a thing of the past. Previously many software developers specialized on software languages such as FORTRAN, C and Pascal. This was mainly because these languages allowed access to all the required functionality. In modem times with the move towards graphical user interface programming a developer must choose not only the required software language(s) but also the required set of development tools for a specific purpose. Typical decision might be to: (cid:12)9 Minimize development time; (cid:12)9 Create a usable interface (such as DOS, or Microsoft Windows or X- Windows, and so on); (cid:12)9 Operate within critical timings (such as the use of fast code, or that DOS programs generally operate faster than Microsoft Windows programs, or that compiled programs generally work faster than interpreted programs); (cid:12)9 Integrate with other software or systems (such as the integration with previous written software, different operating systems or with precompiled libraries); (cid:12)9 Maintain the long-term development of the program (typical questions might be: will there be updates to the development tools; will the development company still be around in a few years?, and so on). Typical modem development languages are C/C++, Visual Basic, Ada (especially in military applications), Java and Delphi. This book introduces C/C++ which can be used in C/C++ and Java development applications. Pascal is useful in developing Delphi and Ada applications. Visual Basic is used to write Microsoft Windows applications, and 80X86 Assembly Lan- guage programming is useful in writing extremely fast sections of code and in appreciating the operation of the PC. The main objective of the text is to provide a single source of reference and learning material for most of the main technical programming lan- guages. It can be used by undergraduates through a course of study from first year to final and from introductory tutorial work to advanced user inter- faces and project work. It can also be used by professional developers with a knowledge of one or more of the software development language who wish to learn some, or all, of the others, or how these languages can be used in 'real-life' applications. xiv Preface The text splits into nine main sections" Part A: Pascal/C programming- gives an introduction to structured soft- ware development using Pascal and C. Part "B C++ programming - gives an introduction to object-oriented de- sign with C++. Part C: 80x86 Assembly Language programs- gives an introduction to Assembly Language programming and PC architecture. Part "D Visual Basic programming- gives an introduction to the devel- opment of graphical user interfaces for Microsoft Windows. Part E: HTML and Java programs - show how to develop WWW-based pages and gives an introduction to Java. Part F: DOS. Part G: Windows .3 Part H: Windows 95. Part :I UNIX. The text uses C and Pascal to provide a basic grounding in software devel- opment. These are used to show structured software development concepts, such as repetition, decision making and modular development. The more advanced concepts of object-oriented design is introduced with the C++ de- velopment. The Visual Basic section contains program examples which can be used to develop graphical user interface programs. Many software development job advertisements now specify the re- quirement for a mixture of software languages on possibly several different operating systems. Software development has thus evolved to the point where it is possible to integrate different software tools to produce the re- quired system. The user interface of a program might be developed using a graphical programming language such as Visual Basic and various special- ized modules within the program could be developed in C/C++. In summary, in a changing employment market: 'it is essential to become multi-skilled in different areas and applications .' Author email" w. buchanan@napier, ac. uk WWW page: http://www.eece.napier.ac.uk/~bill b Source code: http://www.eece.napier.ac.uk/~bill b/soft.html Introduction 1.1 Introduction Software development has grown over the years from simple BASIC pro- grams written on small hobby computers to large software systems that con- trol factories. Many applications that at one time used dedicated hardware are now implemented using software and programmable hardware. This shift in emphasis has meant that, as a percentage, an increasing amount of time is spent on software and less on hardware development. Electrical, electronic and software engineers require a great deal of flexibility in their approach to system development. They must have an un- derstanding of all levels of abstraction of the system, whether it be hard- ware, software or firmware. The system itself could range from a small 4-bit central heating controller to a large industrial control system. In the devel- opment of any system the engineer must understand the system specification from its interface requirements, its timing requirements, its electrical charac- teristics, and so on. The software that runs on a system must be flexible in its structure as the developer could require to interrogate memory addresses for their contents or to model a part of the system as an algorithm. For this purpose the pro- gramming languages C and Pascal are excellent in that they allow a high- level of abstraction (such as algorithm specification) and allow low-level operations (such as operations on binary digits). They have a wide range of applications, from commerce and business to industry and research, which is a distinct advantage as many software languages have facilities that make them useful only in a particular environment. For example, in the past, busi- ness and commercial applications used COBOL extensively, whereas engi- neering and science used FORTRAN. 1.2 Hardware, software and firmware A system consists of hardware, software and firmware, all of which inter- connect. Hardware is 'the bits that can be touched', that is, the components, the screws and nuts, the case, the electrical wires, and so on. Software is the programs that run on programmable hardware and change their operation depending on the inputs to the system. These inputs could be taken from a 4 Introduction keyboard, interface hardware or from an external device. The program itself cannot exist without some form of programmable hardware such as a micro- processor or controller. Firmware is a hardware device that is programmed using software. Typical firmware devices are EEPROMs (Electrically Eras- able Read Only Memories), and interface devices that are programmed using registers. In most applications, dedicated hardware is faster than hardware that is rtmning software, although systems running software programs tend to be easier to modify and require less development time. 1.3 Basic computer architecture The main elements of a basic computer system are a central processing unit (or microprocessor), memory, and input/output (I/O) interfacing circuitry. These are interconnected by three main buses: the address bus; the control bus; and the data bus, as illustrated in Figure 1.1. External devices such as a keyboard, display, disk drives, and so on, can connect directly onto the data, address and control buses, or connect through I/O interfacing circuitry. Memory normally consists of RAM (random access memory) and ROM (read only memory). ROM stores permanent binary information, whereas RAM is a non-permanent memory and loses its contents when the power is taken away. RAM memory is used to run application programs and to store information temporarily. The microprocessor is the main controller of the computer. It fetches bi- nary instructions (known as machine code) from memory, it then decodes these into a series of simple actions and carries out the actions in a sequence of steps. These steps are synchronized by a system clock. The microprocessor accesses a memory location by putting its address on the address bus. The contents at this address are placed on the data bus and the microprocessor reads the data from the data bus. To store data in mem- ory the microprocessor places the data on the data bus. The address of the location in memory is then put on the address bus and the data is then read from the data bus into the required memory address location. Figure 1.1 Block margaid of a elpmis retupmoc metsys Compiling, linking and producing an executable program 5 1.4 Compiling, linking and producing an executable program A microprocessor only understands binary information and operates on a series of binary commands known as machine code. It is extremely difficult to write large programs in machine code, so that high-level languages are used instead. A low-level language is one which is similar to machine code and normally involves the usage of keyword macros to replace machine code instructions. A high-level language has a syntax that is almost like written English and thus makes a program easy to read and to modify. In most programs the actual operation of the hardware is invisible to the pro- grammer. A compiler changes the high-level language into machine code. High- level languages include C, BASIC, COBOL, FORTRAN and Pascal; an ex- ample of a low-level language is 80386 Assembly Language. Figure 1.2 shows the sequence of events that occur to generate an execu- table program from a C or Pascal source code file (the filenames used in this example relate to a PC-based system). An editor creates and modifies the source code file; a compiler then converts this source code into a form which the microprocessor can understand, that is, machine code. The file produced by the compiler is named an object code file code (note that Turbo Pascal does not produce an object code file). This file cannot be executed as it does not have all the required information to run the program. The final stage of the process is linking, which involves adding extra machine code into the program so that it can use devices such as a keyboard, a monitor, and so on. A linker links the object code file with other object code files and with libraries to produce an executable program. These libraries contain other object code modules that are compiled source code. Library and other object code Editor Compiler l Linker (converts source (create and (adds extra code into modify code) information) machine code) I 1 Source code: I Object code: I Executable file: I I FILE.C 1 ~ FILE.OBJ ! FILE.EXE I FILE.PAS I I FILE.EXE I I ' I r I I I Errors/warnings erugiF 2.1 Edit, elipmoc dna link sessecorp If compilation or linking steps generate errors or wamings then the source code must be modified to eliminate them and the process of compila- tion/linking begins again. Warnings in the compile/link process do not stop the compiler or linker from producing an output, but errors will. All errors in 6 Introduction the compilation or linking stage must be eliminated, whereas it is only ad- visable to eliminate warnings. 1.5 C compilation Borland C++ Version 3.0 is an integrated development package available for PC-based systems. It contains an editor, compiler, linker and debugger (used to test programs). The editor creates and modifies source code files and is initiated by running BC. EXE. Figure 3.1 shows a main screen with a source code elif PROGI 1. .C Figure 1.4 shows the compile menu options within this package. A source code file is compiled by selecting Compile to OBJ. If there are no errors an object code file is produced (in this case PROG_I. OBJ). This is linked using Link EXE file (producing the file PROG_I. EXE). A compile and link process can also be initiated using the Make EXE file option. Programs are run from the Run menu option. Figure 3.1 Borland ++C noisreV 0.3 main neercs Figure 4.1 Borland ++C Version 0.3 elipmoc menu snoitpo Pascal compilation 7 1.6 Pascal compilation Turbo Pascal Version 5.0 is an integrated development package available for PC-based systems. It contains an editor, compiler, linker and debugger (used to test programs). The editor creates and modifies source code files and is initiated by running TURBO. EXE. Figure 1.5 shows a main screen with a source code file P ROGI i. PAS. Figure 1.6 shows the compile menu options within this package. A source code file is compiled by selecting Compile. If there are no errors then an executable program is produced. If the destination is given as Mem- ory then it does not save the executable file to the disk but runs it from memory. If the destination is to the Dis k then an executable file will be produced (producing the file PROG 1. EXE). The destination can be toggled by pressing the ENTER key while the line cursor is on the Destination option. A program is run from the Run menu option. Figure 5.1 Turbo lacsaP Version 0.5 main neercs Figure 6.1 Turbo lacsaP noisreV 0.5 elipmoc menu snoitpo 8 Introduction 1.7 Introduction to C This section gives a brief introduction to ANSI-C. 1.7.1 Pre-processor The pre-processor acts on programs before the compiler. It uses commands that have a number-sign symbol ('#') as the first non-blank character on a line. Figure 7.1 shows its main uses, which are" including special files (header files) and defining various macros (or symbolic tokens). The #include directive includes a header file and #define defines macros. By placing these directives near the top of a source code file then all parts of the program have access to the information contained in them. Library and other object code Replace macros ~cSource I with #define directive Compiler rekniL (converts source Pre-processor (adds extra code into v information) machine code) I Include files with #include directive Figure 1.7 Operations 11o the program to produce an executable file For example, the pre-processor directive: #include "main. h" includes the header file main.h. The inverted commas inform the pre- processor that this file will be found in the current working directory, while the directive #include <stdio.h> includes the file stdio.h found in the default include directory. This directory is normally set-up automatically by the system. For example, Turbo C Ver- sion 2.0 stores its header files, by default, in the directory \TC\ INCLUDE and Borland C uses \B(cid:14)9 Typically, header files on a Unix system are stored in the/usr/include directory. To summarize, inverted commas (" ") inform the pre-processor to search for the specified header file in the current directory (or the directory speci- fied in the pathname). The chevron characters (<>) inform the pre- processor to search in the default include directory. It is not advisable to Introduction to C 9 include any other file apart from header files. These have a '.h' file exten- sion (although this is not obligatory). Standard header files are used in con- junction with fimctions contained in libraries. They do not contain program code, but have information relating to functions. A given set of functions, such as maths or I/O, has a header file associated with it. Table 1.1 gives typical header files and their functionality. A macro replaces every occurrence of a certain token with another specified token. The following examples show substitutions using the # de f i n e directive. #define PI 3.14 #define BEGIN { #define END } #define sqr(X) ((X) * (x)) #define SPEED OF LIGHT 3e8 Typically, as a matter of programming style, the definitions of constants, such as n, are given in uppercase characters. Table 1.1 Typical header files Header file Comment ctype, h character classification and conversion math.h maths functions stddef h defines several common data types and macros stdio.h Input/Output (I/O) routines, such as input from keyboard, output to display and file handling (stdio is a contraction of standard input/output) stdlib.h miscellaneous routines string.h string manipulation functions time.h time functions 1.7.2 Structure Normally programs are split into a number of sub-tasks named functions. These are clearly distinctive pieces of code that perform particular opera- tions. The main function (main()) is the basic routine for controlling the flow of the program and calls other sub-functions. C Program 1.1 is a simple program which uses the puts () function to display the text "Essence of Software". The puts() function is a standard function used to output text to the display; the header file associ- ated with it is stdio.h. This header file is included using the #include directive. The statement terminator (;) is used to end a line of code (or statement) and braces ({}) show the beginning ({) and end (}) of a block of code. Comments are inserted in the program between a start comment identifier (/*) and an end identifier (*/). 01 Introduction OProgram 1.1 /* Simple program */ #include <stdio.h> int main (void) { puts ("Essence of Software") ; return(O); All C programs have a main () function which defines the entry point into the program and, by means of calling functions, controls general program flow. tI can be located anywhere in the source code program, but is normally placed near the top of the file it is located in (making it easier to find). The i nt keyword preceding ma in ( ) defines that the program returns a value to the operating system (or calling program). In this case, the return value is 0 (re turn (0)). Normally, a non-zero return value is used when the program has exited due to an error; the actual value of this gives an indication of why the program has exited. The void within the parenthesis of main() de- fines that there is no communication between the program and the operating system (that is, no values are passed into the program). Figure 1.8 shows the basic structure of a C program. I /* This is a comment */ ' i oomment int main (void) ( in t ~ VarX, var2; va ria ble i float var3, var4; declaration statement ; statement ; return ( 0 ) ; main function erugiF margorp erutcurts 1.8 C 1.7.3 Data types Variables within a program can be stored as either numbers or characters. For example, the resistance of a copper wire would be stored as a number (a real value) and the name of a component (such as, "RI") would be stored as characters. Table 1.2 gives the four basic data types which define the format of variables. There are three basic extensions for the four types; these are: short long unsigned

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.