
Computer Programming Fundamentals: Understanding Machine Language
"Learn about computer programs, writing code, and the importance of machine language in programming. Discover the basics of programming languages, algorithms, and the significance of machine-native instructions. Explore the advantages and disadvantages of machine language for efficient coding. Start your journey into the world of computer programming today."
Uploaded on | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Computer Programs Software refers to programs that make the computer perform some task. A program is a setof instructionsthat tells thecomputerwhat todo. When you have written a program, the computer will behave exactly as you have instructed it. It will do no more or no less than what is contained in yourspecific instructions.
Writing Programs Learning to write programs requires two skills. You need to use specific terminology and punctuation that can be understood by the machine; that is, you need to learn a programming language. You need to develop a plan for solving a particular problem. This plan or algorithm is a sequence of steps that, when followed, will lead to a solution of the problem.
What Is a Computer Language? A microprocessor is designed to understand a set of commands called an instruction set All instructions must be provided to the CPU in its native language, called machine language. If eight-digit binary codes are used, there are 256 numbered instructions from 00000000 to 11111111.
Machine Language Instructions for adding two numbers would consist of a sequence of these eight-digit codes from 00000000 to 11111111. Instructions written in this form are referred to as machine language. It is the native language that the CPU speaks and understands . It is possible to write an entire program in machine language. However, this is very time consuming and difficult to read and understand.
Machine Language Advantages It is the basic terminology that can understand by machine. It can easily read by computer so execution of code is fast as compared toother language. No translator is required todo an operation. The computer can easily respond when input given to machinewords.
Machine Language Disadvantages It is not easy to write it required core knowledge about programming, i.e. machine languageprogram. It canvary with respect to the configuration of the machine. It is necessary to remember the address location where data can store. Lot much time is required to write a code because it consists of combination 0 or 1 digit only. Machinedependent language.
Programming Languages Fortunately, special languages have been developed that are more easily understood (than machine language). These special languages are called programming languages. These languages provide a way to write computer programs that are understood by both computers and people. Programming languages have their own vocabulary and rules of usage. Some languages are very technical, while others are similar to English.
Assembly Language The programming language that is most like machine language is assembly language. Assembly language uses letters and numbers to represent machine language instructions. An assembler is a program that reads the codes the programmer writes in assembly language and assembles a machine language program based on those codes. However, assembly language is still difficult to read.
Comparing Machine Language & Assembly Language For example, the machine code for adding two integers might be: 010000110011101000111101010000010010101101000010 While the assembly language code might be: LOAD A ADD B STORE C This causes the number in A to be added to the number in B, and the result is stored for later use in C.
Assembly Language Advantages Assembly language is easier to understand and saves time and effort by the software engineer. It is easier to re-correct error and modify instructions in a program. Assembly language has the same efficiency of execution as the machine level language because this is one-to-one translator between assembly language program and its corresponding machine language program. Disadvantages One of the major disadvantages is that assembly languages is machine dependent. A program written for one computer might not run in othercomputerswith different hardwareconfiguration.
Low Level Languages Machine Language and Assembly Language are both called low-level languages. In a low-level language, it is necessary for the programmer to know the instructionset of the CPU in ordertoprogram thecomputer. Each instruction in a low-level language corresponds to one or only a few microprocessorinstructions.
High Level Languages A high-level language is any programming language that uses words and symbols to make it relatively easy to read and writea computer program. In a high-level language, instructions do not necessarily correspond one-to-one with the instruction set of the CPU. One command in a high-level language may correspond to many microprocessor instructions. High level programming languages allow the specification of a problem solution in termsof human understanding language. (Eg. English)
High Level Languages Cont Many high-level languages have been developed. These include: FORTRAN, COBOL, BASIC, Logo, Pascal, C, C++, Java, and others. These languages simplify even further the terminology and symbolism necessary for directing the machine to perform various manipulationsof data.
High Level Languages Advantages Reduce the number of instructions that must be written. Allow programs to be written in a shorter amount of time than a low-level language would take. Reduce the number of errors that are made, because The instructions are easier to read. Are more portable (the programs are easier to move among computers with different microprocessors). Disadvantages High level languages are less efficient than assembly language program and require a bigger quantity of computer time for translation into machine instructions.
High Level Language Examples Consider the following programs that add two numbers together: BASIC Pascal C++ LOGO I = 3 program AddIt; int main( ) to add :I :J :K J = 2 var { MAKE I :3 K = I + J i, j, k : integer; int i, j, k; MAKE J :2 begin i = 3; MAKE K :I + :J i := 3; j = 2; end j := 2; k = i + j; k := i + j; return 0; end. }
Interpreters and Compilers Programmers writing in a high-level language enter the program s instructions into a text editor. The files saved in this format are called text files. A program written in a high-level language is called source code. The programs are translated into machine language by interpreters or compilers. The resulting machine language code is known as object code.
Scripting languages The scripting languages is a programming languages designed for integrating and putting across with other programming languages. Scripting languages are turning more popular due to the emergence of web-based applications and security feature. It gives a platform to employ to work with GUI (Graphical User Interface). Eg. Various online courses linked to various World Wide Web sites. Some of the most widely used scripting language are JavaScript, VBScript, PHP, Perl, Python, Ruby, ASP and TCL.
Scripting languages Advantages Easy to learn and use. Minimum programming knowledge or experience required. Allow complex tasks to be performed in relatively few steps. Allow simple creation and editing in a variety of text editors. Take into account the addition of dynamic and interactive activities to web pages. Disadvantages Scripting languages is that executable code can inadvertently be downloaded from a distant host to a web browser s machine, installed and run using the local browser s interpreter.
Natural Language Natural language is spoken communication that can adapt naturally by humans. Natural languages not actually constructed as artificial languages and they do not truly appear as formal lectures. No extra training needed to read it clearly. A human language, such as English, Hindi is the natural languages.
Natural Language Advantages It can use for human communication. Easy to understand by human, it directly related to feeling and emotions. It can changes as region vise i.e. differently country with different languages. No proper sequence maintained some time. Disadvantages It is not unique. Natural language cannot understand by computer machine.
A Brief Chronology Early 1950s 1957 1958 1960 1962 1964 1966 1970 1972 1975 1978 1978 1983 1984 1986 1988 1990 1990s 1995 2000 order codes (primitive assemblers) the first high-level programming language the first modern, imperative language Interactive programming; business programming the birth of OOP (SIMULA) FORTRAN ALGOL LISP, COBOL APL, SIMULA BASIC, PL/I ISWIM Prolog C Pascal, Scheme CSP FP Smalltalk-80, Ada Standard ML C++, Eiffel CLOS, Oberon, Mathematica Haskell Perl, Python, Ruby, JavaScript Java C# first modern functional language (a proposal) logic programming is born the systems programming language two teaching languages Concurrency matures Backus proposal OOP is reinvented FP becomes mainstream (?) OOP is reinvented (again) FP is reinvented Scripting languages become mainstream OOP is reinvented for the internet
Why Programming? To Develop Problem Solving Skills It is very important to develop problem solving skills. Programming is all about solving problems. Requires creativity and careful thought. Analyze the problem and break it down into manageable parts (modules, procedures, functions) It s also rewarding!
Step 1 Good Programming Habits 1. Analysis Is the computer the appropriate tool for solving this problem? Would the problem be better solved with human interaction or paper and pencil? Sometimes human judgment is preferable.
Step 2 Good Programming Habits 2. Specification of the Problem Formulate a clear and precise statement of what is to be done (clear and unambiguous). Know what data are available Know what may be assumed Know what output is desired & the form it should take Divide the problem into sub problems
Step 3 Good Programming Habits 3.1 Develop an Algorithm Algorithm: a finite sequence of effective statements that when applied to the problem, will solve it. Effective Statement: a clear unambiguous instruction that can be carried out. Algorithms should have: specific beginning and ending that is reached in a reasonable amount of time (a finite amount of time). This is done before sitting down at the computer.
Step 3 Good Programming Habits 3.2 Document the Program Programming Style Upper / Lower Case, Indenting, format Comments Descriptive Identifier Names Variables, Constants, Procedures, Functions Pre & Post Conditions For each Procedure and Function Output
Step 4 Good Programming Habits 4. Code the Program After algorithms are correct Desk check your program Without the computer, just paper and pencil 4.1 Type and Run the Program Look for errors Syntax Errors (semi colon missing, etc.) Logic Errors (divide by zero, etc.)
Step 4 Good Programming Habits 4.2 Test the Results Does it produce the correct solution? Check results with paper and pencil. Does it work for all cases? Border, Edge, Extreme Cases Revise the program if not correct. The coding process is not completed until the program has been tested thoroughly and works properly (recheck the specifications).
Step 5 Good Programming Habits 5. Interpretation The program may execute without any obvious errors. It may not produce the results which solve the problem. G.I.G.O Get what you ask for, not what you want. Recheck your program with the original specifications GIGO (garbage in, garbage out) is a concept common to computer science and mathematics: the quality of output is determined by the quality of the input. So, for example, if a mathematical equation is improperly stated, the answer is unlikely to be correct. Similarly, if incorrect data is input to a program, the output is unlikely to be informative.
C - LANGUAGE OVERVIEW C is a general purpose high level language that was originally developed by Dennis M. Ritchie to develop the Unix operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX applications programs have been written in C. C has now become a widely used professional language for various reasons. Easy to learn Structured language It produces efficient programs. It can handle low-level activities. It can be compiled on a variety of computer platforms. 40
C - PROGRAM STRUCTURE Before we study basic building blocks of the C programming language, let us look a bare minimum C program structure so that we can take it as a reference in upcoming chapters. A C program basically consists of the following parts: Preprocessor Commands Functions Variables Statements & Expressions Comments 41
C Hello World Example 1. The first line of the program #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. 2. The next line int main() is the main function where program execution begins. 3. The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program. 4. The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen. 5. The next line return 0; terminates main()function and returns the value 0. 42
Token of C/C++ The C programs are made up of different things termed as tokens. The different tokens of C are its character set keyword identifiers constants and variables data types operators 43
character set The character set of any programming language indicate the different characters the program can contain. Character set of C includes all the alphabets, digits and special symbol supported by the processor. Characters List included Alphabets (Upper case and lower case) A ,B, C ..Z a, b, c z Digits (numbers) 0, 1, 2, 3, 4 ..9 Special symbol (all those seen on a keyword, nothing besides that) < > { } ( ) [ ] , . ; : ! ? / + * = % & # @ | - _ $ ^ - (The names used for these symbol are given in the table) Other special characters Blank Space, Tab, Carriage Return (Enter Key) 44
Special symbol and their names Symbol Name of Symbol Symbol Name of Symbol , Comma & Ampersand . Dot * Asterisk ; Semicolon - Minus Sign : Colon + Plus Sign ? Question Mark < Opening Angle (Less than sign) ! Exclamation Mark > Closing Angle (Greater than sign) | Pipe ( Left Parenthesis / Forward Slash ) Right Parenthesis \ Back Slash [ Left Square Bracket Single Quotes ] Right Square Bracket Double quotes { Left Brace ~ Tilde } Right Brace _ Underscore # Hash ^ Caret $ Dollar % Percentage 45
Keywords The following list shows the reserved words in C. These reserved words may not be used as constant or variable or any other identifier names. Keywords auto else long switch break enum register typedef case extern return union char float short unsigned const for signed void continue goto sizeof volatile default if static while do int struct _Packed double 46
identifiers A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9). C does not allow punctuation characters such as @, $, and % within identifiers. C is a case sensitive Thus Manpower and manpower are two different identifiers in C. Here are some examples of acceptable identifiers: programming language. 47
Data Types In the C programming language, data types refers to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted. The types in C can be classified as follows: 48
Data Types Type Storage size Value range char 1 byte -128 to 127 or 0 to 255 unsigned char 1 byte 0 to 255 signed char 1 byte -128 to 127 -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 int 2 or 4 bytes unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295 short 2 bytes -32,768 to 32,767 unsigned short 2 bytes 0 to 65,535 long 4 bytes -2,147,483,648 to 2,147,483,647 unsigned long 4 bytes 0 to 4,294,967,295 float 4 byte 1.2E-38 to 3.4E+38 double 8 byte 2.3E-308 to 1.7E+308 long double 10 byte 3.4E-4932 to 1.1E+4932 49
Constants The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well. The constants are treated just like regular variables except that their values cannot be modified after their definition. Constants can be defined either by writing the keyword const before the data type or by using #define. ex .. const type variable = value; 50
Variables A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive. type variable_list; type variable_name = value; int i, j, k; int d = 3, f = 5; char c, ch; float f, salary; double d; 51
Escape Sequences There are certain characters in C when they are proceeded by a back slash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Here you have a list of some of such escape sequence codes: Escape sequence Meaning \\ \ character \' ' character \" " character \? ? character \a Alert or bell \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab 52
C-Operators Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators sizeof & ternary Operators Precedence in C 53
Arithmetic Operators Following table shows all the arithmetic operators supported by C language. Assume variable A holds 10 and variable B holds 20 then: Operator Description Example + Adds two operands A + B will give 30 Subtracts second operand from the first - A - B will give -10 * Multiply both operands A * B will give 200 / Divide numerator by de-numerator B / A will give 2 Modulus Operator and remainder of after an integer division % B % A will give 0 Increment operator increases integer value by one ++ A++ will give 11 Decrement operator decreases integer value by one -- A-- will give 9 54
Relational Operators Following table shows all the relational operators supported by C language. Assume variable A holds 10 and variable B holds 20 then: Operator Description Example Checks if the value of two operands is equal or not, if yes then condition becomes true. == (A == B) is not true. Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. != (A != B) is true. Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. > (A > B) is not true. Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. < (A < B) is true. Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. >= (A >= B) is not true. Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. <= (A <= B) is true. 55
Logical Operators Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0 then: Operator Description Example Called Logical AND operator. If both the operands are non zero then condition becomes true. && (A && B) is false. Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. || (A || B) is true. Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. ! !(A && B) is true. 56
Bitwise Operators The Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13 then: Operator Description Example Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 & (A | B) will give 61 which is 0011 1101 | Binary OR Operator copies a bit if it exists in either operand. Binary EXOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ^ (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. ~ Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000 << Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111 >> 57