How to convert high-level language to machine language

Over the years, computer languages have been evolved from Low-Level to High-Level Languages. In the earliest days of computers, only Binary Language was used to write programs. The computer languages are classified as follows:

How to convert high-level language to machine language
How to convert high-level language to machine language

reference

Machine Language (low level language)

Low-Level language is the only language which can be understood by the computer. Low-level language is also known as Machine Language. The machine language contains only two symbols 1 & 0. All the instructions of machine language are written in the form of binary numbers 1's & 0's. A computer can directly understand the machine language.

Middle-level language is a computer language in which the instructions are created using symbols such as letters, digits and special characters. Assembly language is an example of middle-level language. In assembly language, we use predefined words called mnemonics. Binary code instructions in low-level language are replaced with mnemonics and operands in middle-level language. But the computer cannot understand mnemonics, so we use a translator called Assembler to translate mnemonics into machine language.

Assembler is a translator which takes assembly code as input and produces machine code as output. That means, the computer cannot understand middle-level language, so it needs to be translated into a low-level language to make it understandable by the computer. Assembler is used to translate middle-level language into low-level language.

g++ -S main.cpp -o main.s

High Level Language

High-level language is a computer language which can be understood by the users. The high-level language is very similar to human languages and has a set of grammar rules that are used to make instructions more easily. Every high-level language has a set of predefined words known as Keywords and a set of rules known as Syntax to create instructions. The high-level language is easier to understand for the users but the computer can not understand it. High-level language needs to be converted into the low-level language to make it understandable by the computer. We use Compiler or interpreter to convert high-level language to low-level language.

Languages like FORTRAN,C, C++, JAVA, Python, etc., are examples of high-level languages. All these programming languages use human-understandable language like English to write program instructions. These instructions are converted to low-level language by the compiler or interperter so that it can be understood by the computer.

g++ main.cpp

  show understanding of the need for both high-level and low-level languages

•  show understanding of the need for compilers when translating programs written in a high-level language

•  show understanding of the use of interpreters with high-level language programs

•  show understanding of the need for assemblers when translating programs written in assembly language

How to convert high-level language to machine language

The only language understood by computers is binary, also known as machine code

This is because computers are electronic devices that can only tell the difference between the on and off states of an electric circuit. 

The numbers 1 and 0 are used by humans to represent these on/off values. That’s why things written in binary look like this: 01010010010001010101011001001001010100110100010100100001

Machine code:

  •  Instructions for a computer to follow must therefore be written in machine code
  • This was done by early computer programmers
  • Each sort of computer is different so they all need different binary instructions to perform the same task – different computers have different machine code

Programming languages can be classified into low-level and high-level languages

Low-level languages

Assembly:

 To make it easier to program computers a programming language was invented. It was called ‘Assembly‘ and was made up of a small set of command words called mnemonics which programmers typed instead of binary Examples of mnemonics are “MOV”, “ADD” and “PUSH”

Computers could not understand Assembly so it had to be converted to machine code by an ‘assembler‘before it could be run

  • Low-level languages are more similar to machine code than they are to languages spoken by humans
  • They are not very developed and just offer quicker ways to write binary
  • This means they give the programmer close control over the computer because their instructions are very specific
  • Unfortunately they are hard to learn
  • Since the machine code for each computer is different, programs translated to machine code on one computer will not work on a different one: Low level languages are not very ‘portable’

High-level languages

High level programming languages are more developed than low-level languages so are closer to human spoken language

  • Some examples of high level languages are: C#, Visual Basic, C, C++, JavaScript, Objective C, BASIC and Pascal (the fact that they all have the letter C in their name is a coincidence. Some do not.)
  • High-level programming languages are easier for humans to write, read and maintain
  • They support a wide range of data types
  • They allow the programmer to think about how to solve the problem and not how to communicate with the computer. This is called abstraction

Converting to Machine Code

 Translators:

  • Just like low-level languages, high-level languages must be converted to machine code before a computer can understand and run them
  • This is done using a ‘translator‘
  • Different translators convert the same high level code into machine code for different computers
  • High level code ready to be translated into machine code is called ‘source code’

There are two different types of translator: Compilers and Interpreters

Compilers:

  • Compilers convert (or ‘compile’) the source code to machine code all at once
  • This is then stored as an executable file which the computer can run (for example, something ending with the ‘.exe’ file extension)
  • Errors in the source code can be spotted as the program is compiling and reported to the programmer

Interpreters:

  • Interpreters convert the code as it is running
  • They take a line of source code at a time and convert it to machine code (which the computer runs straight away)
  • This is repeated until the end of the program
  • No executable file is created
  • If the interpreter comes across an error in the source code the only things it can do is to report the error to the person trying to use the program (or it may just refuse to continue running)

Show understanding of the need for both high-level and low-level languages

Computers don’t understand high level languages because they only understand binary (‘machine code’). 

Humans struggle to understand exactly what a program does when it is in binary only.

High-level languages are more accessible to programmers.

High-level languages will work on different types of computers.

Low-level programming allows for hardware to be controlled directly.

Low-level programming will only work with the processor it is designed for (machine-dependent).

understand the need for compilers when translating programs written in a high-level language 

Translates the entire program from source (i.e. high-level language) to object code / machine code.

Produces an executable file (i.e. In binary / machine code)

Source code remains hidden so cannot be modified by customer

Compiled once only so doesn’t need a translator

Compilers use a lot of computer resources: It has to be loaded in the computer’s memory at the same time as the source code and there has to be sufficient memory to hold the object code

Difficult to pin-point errors its source in the original program.

Show understanding of the use of interpreters with high-level language programs

Interpreters translate each instruction is taken in turn and translated to machine code. The instruction is then executed before the next instruction is translated.

Error messages are output as soon as an error is encountered so easy to debug

Useful for prototypes as program will run even when part of it has errors.

Execution of a program is slow compared to that of a compiled program.

Instructions inside a loop have to be translated each time the loop is entered.

Understand the need for assemblers when translating programs written in assembly language

Assemblers translate assembly language to machine code / binary / object code.