ExamSaathi learning library · Government exams · IBPS RRB (CRP RRBs XV), Office Assistant and Officer Scale I main examinations, Computer Knowledge: a test named in the notice, which gives no topic list · Computer knowledge

Computer languages and translators: assemblers, compilers and interpreters

How instructions written by people become the bits a CPU carries out: machine, assembly and high-level languages; the assembler, compiler and interpreter, compared side by side; the linker and loader; syntax, run-time and logical errors; and the generations of programming languages.

Status: original pilot lesson. Teacher review is pending. It is not a complete course or official exam material.

You will learn to

Before you start

Three earlier ExamSaathi lessons lead into this one.

1. "Operating systems: functions, types, and Unix and Linux basics": system software runs the computer itself, application software does your tasks (a mobile banking app, for instance) and utilities are small helper programs that look after the machine.

2. "Number systems for computers: binary, octal, hexadecimal and memory units": a computer holds everything as bits, the binary digits 0 and 1.

3. "Computer organisation: CPU, buses, input and output devices and ports": the CPU is the part that carries out instructions.

This lesson joins these ideas. People write programs in languages they can read, but the CPU can carry out only instructions made of bits. You will learn the three levels of language (machine, assembly and high-level), the three language translators named in the CBSE Class XI Computer Science (083) syllabus (assembler, compiler and interpreter), what the linker and the loader do, the three kinds of program error, and how books number the generations of programming languages.

Programs, machine language and assembly language

A program is a set of step-by-step instructions that tells a computer what to do; the seat-booking software at a railway reservation counter is one. A programming language is a fixed set of words, symbols and grammar rules for writing such instructions.

Machine language is the CPU's own language. Each instruction is a group of bits (0s and 1s) that the CPU carries out directly, without translation. Every processor family has its own instruction set (the instructions it understands), so machine code made for one family generally will not run on another. Writing long rows of bits by hand is slow and easy to get wrong.

Assembly language makes this easier by giving each instruction a short memory aid called a mnemonic, such as MOV for move or ADD for add. On one processor family, the bits 10110000 01100001 tell the CPU to put the number 97 into a register named AL (a register is a tiny storage place inside the CPU). In assembly the same instruction is written MOV AL, 61h, where 61h is 97 written in hexadecimal. One assembly statement usually stands for one machine instruction, so assembly stays tied to one processor family. The CPU cannot read mnemonics, so a translator called an assembler turns them into machine code.

Machine language and assembly language are called low-level languages because they stay close to the hardware.

High-level languages and language generations

High-level languages use words and symbols close to English and arithmetic, such as balance = balance + deposit, instead of registers and memory addresses. One such statement may become several machine instructions. Examples are Fortran, COBOL, Java and Python (taught in CBSE Class XI). Such a language is not tied to one processor family, so the same source can run on any machine that has a suitable translator: it is largely portable. The CPU still cannot carry out high-level code directly; a compiler or an interpreter must handle it first. Low and high are relative terms: assembly is a step above machine code but still low-level.

Many books number these levels as language generations:

1. First generation: machine language.

2. Second generation: assembly language.

3. Third generation: high-level languages such as COBOL and Python.

4. Fourth generation: very high-level languages, often built for one field such as databases or reports; SQL, used to ask a database for data, is one example.

Some books add a fifth generation, used mainly in artificial intelligence research, in which you state the conditions an answer must meet. The labels were applied loosely and partly after the fact, and books group the later generations differently; treat the list as approximate. These are not the generations of computers, which lent the name.

Assembler, compiler and interpreter

The text a programmer types is called source code. A language translator converts it into instructions the CPU can carry out, and the CBSE Class XI syllabus names three.

The assembler translates assembly language into machine code.

In the usual textbook picture of a compiler that works ahead of time, it translates a high-level program, or one source file of it, before the compiled code runs, and it can report several errors during one compilation. It does not run the program. It produces object code (machine code still to be joined with other pieces) or an executable file, a finished program that runs again and again without the compiler.

In the simple textbook picture, the interpreter translates one statement, runs it, then moves on. It stops at the first faulty statement, so the lines before it have already run.

Side by side:

1. Works on: compiler, a whole program or source file before its code runs; interpreter, one statement at a time.

2. Errors: compiler, several may be reported together while compiling; interpreter, stops at the first one.

3. Leaves behind: compiler, an object or executable file; interpreter, no such file.

4. Needed on every run: compiler, no; interpreter, yes.

5. Speed: compiled code tends to run faster; an interpreter lets you test a change straight away.

Many real systems mix the two: Java, for example, is usually compiled into bytecode, an in-between code that is then interpreted, or compiled again into machine code, while the program runs.

From source code to a running program, and errors

For a conventional ahead-of-time build that produces object code, one usual path has five steps:

1. You write the source code and save it.

2. The compiler translates it into object code.

3. The linker joins your object code, and any ready-made library code supplied with the language, into one executable file.

4. The loader copies the executable file from storage into main memory (RAM).

5. The CPU carries out the machine instructions.

Things can go wrong along the way, and the CBSE syllabus names three kinds of error.

A syntax error breaks the grammar rules of the language, such as a bracket opened but never closed. The translator reports it, and the program cannot run properly until you fix it.

A run-time error arises while the program is running: the grammar is fine, but an operation fails, such as dividing by zero. If the program does not handle the error, it normally stops and shows an error message.

A logical error makes a program that can run give a wrong result or behave wrongly, because the steps themselves are wrong, such as adding five days' sales and dividing by 4. Often no error message appears.

Debugging means finding and removing these errors, often called bugs; the CBSE syllabus lists testing and debugging among the steps of problem-solving. Test with inputs whose answers you already know, read every message, and fix the cause; a tool called a debugger helps you examine a program while it runs.

Common mistakes

1. Thinking a compiler runs the program. Fix: a compiler only translates. It leaves an object or executable file; after linking, the loader puts the executable file in memory and the CPU runs it, with no compiler needed.

2. Thinking an interpreter leaves an object file. Fix: in the textbook picture an interpreter translates and runs one statement at a time and keeps no machine-code file, which is why it is needed every time the program runs.

3. Calling assembly a high-level language. Fix: assembly uses readable mnemonics, but its instructions usually match the machine instructions of one processor family closely, so it is a low-level, second-generation language.

4. Saying high-level code runs on the processor without translation. Fix: on ordinary computers the CPU carries out only its own machine code, so high-level code must first go through a compiler or an interpreter.

5. Calling the linker or the loader a language translator. Fix: the three translators are the assembler, the compiler and the interpreter; the linker joins pieces of machine code and the loader copies the result into memory.

6. Thinking that no error message means no error. Fix: a logical error may give a wrong answer without any error message, so check results against cases you have worked out by hand.

A routine for translator and language questions

1. Read the description and mark the clue words: bits, mnemonics, English-like statements, whole program, one statement at a time, file left behind, needed on every run.

2. Place the language. Only 0s and 1s means machine language (first generation); mnemonics such as MOV mean assembly (second); English-like statements mean a high-level language (third); a database query language such as SQL is usually put in the fourth.

3. Name the translator. Assembly into machine code: assembler. A program or source file translated before its code runs, several errors reported together, a file left behind: compiler. One statement at a time, a stop at the first error, needed on every run: interpreter.

4. For order questions, write the chain: source code, compiler, object code, linker, executable file, loader, main memory, CPU.

5. For error questions, ask in turn: was the grammar rejected (syntax error), did an operation fail while the program was running (run-time error), or did the program run but give a wrong result or behave wrongly (logical error)?

6. Before you choose, test each option against the traps: a compiler translates but does not run the program, an interpreter leaves no object file, assembly is low-level, and the linker and loader are not translators.

Worked examples

  1. Three students in a school computer lab describe the translator they used. Asha: "I wrote my program in a high-level language. The tool read my whole program first, listed two errors with their line numbers, and after I fixed them it produced a file that now runs on a compatible computer without that tool." Bilal: "It ran my first two statements and showed their results, then stopped at the third with an error message. I need it every time I run my program." Charu: "I wrote lines such as MOV AL, 61h and it turned them into machine code." Name the translator each student used.

    1. Step 1: Start with Charu. MOV AL, 61h is a mnemonic, so her program is in assembly language, and the translator that turns assembly into machine code is the assembler.
    2. Step 2: Asha's program is in a high-level language, and her tool translated it before any of it ran and reported two errors together. That matches the textbook picture of a compiler.
    3. Step 3: Asha's tool also left behind a file that runs without it. Leaving an executable file that runs without the translator is a compiler's mark, so this confirms the answer.
    4. Step 4: Bilal's tool translated and ran one statement at a time, which is why two results appeared before it stopped at the first error, and it is needed on every run. That is an interpreter.

    Answer: Asha used a compiler, Bilal used an interpreter and Charu used an assembler.

  2. Using a compiled language, a programmer writes a program that works out interest on a bank's fixed deposits. Put these steps in order. A: the loader copies the executable file into main memory. B: the programmer types and saves the source code. C: the CPU carries out the machine instructions. D: the linker joins the object code with ready-made library code into one executable file. E: the compiler translates the source code into object code.

    1. Step 1: Nothing can be translated before it is written, so B comes first: the source code is typed and saved.
    2. Step 2: The compiler works on that source code, so E comes next and produces object code.
    3. Step 3: Object code may still need other pieces such as library code, so D follows: the linker makes one executable file.
    4. Step 4: A file sitting in storage cannot run until it is in main memory, so A comes next: the loader copies it there.
    5. Step 5: Only now can C happen: the CPU carries out the machine instructions.

    Answer: B, E, D, A, C: write the source code, compile, link, load, run.

  3. A program in a panchayat office reads the water charges collected in five wards and the number of wards, and should print the total divided by the number of wards as the average per ward. Testing shows three problems. 1. One line opens a bracket but never closes it, and the translator rejects it. 2. When the clerk types 0 as the number of wards, the program stops halfway with a message about division by zero. 3. For ₹800, ₹700, ₹900, ₹600 and ₹1,000 with 5 wards it prints ₹600, with no message, because the line that adds the charges leaves out the fifth ward. Classify each error.

    1. Step 1: Problem 1 breaks the grammar rules of the language and is caught by the translator, so it is a syntax error.
    2. Step 2: Problem 2 has correct grammar, but an operation fails while the program is running: dividing by zero. The program does not handle it, so it stops with a message. That is a run-time error.
    3. Step 3: Problem 3 runs to the end without any message, yet the answer is wrong. The correct average is ₹4,000 ÷ 5 = ₹800, but adding only four wards gives ₹3,000 ÷ 5 = ₹600. A program that runs but gives a wrong answer because its steps are wrong has a logical error.
    4. Step 4: To find a logical error, debug by testing: work out the expected answer by hand, compare it with the output, then trace the steps until you reach the line that leaves out the fifth ward.

    Answer: 1 is a syntax error, 2 is a run-time error and 3 is a logical error, which testing against a hand-worked answer reveals.

Practice checks

  1. Which translator converts a program written in assembly language into machine code?
  2. A translator checks a whole program written in a high-level language before any of its compiled code runs, reports the errors it finds, and leaves a file that later runs without the translator. Which translator is it?
  3. Which statement about an interpreter, in the usual textbook description, is correct?
  4. A bank's simple interest program should print ₹1,000 for ₹10,000 at 5 per cent a year for 2 years. It runs to the end with no error message but prints ₹10,000. What kind of error is this?
  5. In the usual grouping of programming language generations, which pairing is correct?

Sources checked