High and Low Level Languages, translators

  • 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

Objectives

Programming Languages and machine code

  • Computer, in particular CPU, takes instruction in the form of machine code
  • Machine code uses the instruction set of a particular computer architecture
  • Machine code is the lowest level in terms of computer language

Characteristic of machine code

  • Machine code is basically a bunch of binary numbers, which each value may represents a different instruction or operation to a computer
  • Usually machine code is "displayed" in hexadecimal values, but still very difficult to understand:

Left: some machine code displayed in hexadecimal

Computer architecture

  • Different types of computer may have different computer (CPU) architecture
    • e.g. Desktop computer nowadays are called Intel x64 Architecture, while iOS/Android devices are called ARM Architecture
  • Machine code is designed for one particular CPU architecture, and most of the time are not compatible with other
    • so a desktop program cannot run directly on smartphone

Translator

  • Since machine code is, if not impossible, very difficult to understand (it's computer language, not human!), there's some need of translator software
  • Translator software will convert higher level languages into machine code, which allows the computer the understand and execute

Low level? High level?

  • When we say a language is lower level, that means it is closer to how a computer works
  • On the other hand, the higher level a language, the language is usually closer to how human thinks

Low level languages

Higher level languages

Lowest

Machine Code / Language

Assembly
Language

Java

C++

Python

Javascript

C

More platform dependent

Less platform dependent

Platform specific

Assembly Language

  • Assembly is an low level language, somehow closest to machine code
  • Assembly language is platform / architecture specific, contains mnemonic code that can translate to machine code directly (almost 1-1 translation, like how Cantonese can translate to Mandarin)

Example of some assembly code and machine code

More characteristic about assembly

  • You may found that assembly is hard to understand, but compare to machine code, at least it is better
  • Assembly language are used to write code specific to hardware, e.g. device driver
  • Usually assembly codes are very fast compare to most higher level languages (since it is closer to what the computer can natively understand)
  • Assembly language is translated into machine code using programs called Assembler

High level languages

  • Python, Java, Visual Basic are some examples of higher level programming languages
  • Compare to Assembly language, they are more human readable, closer to the way of human logic and human languages

High level language, example

A statement in high level language:

a = a + 1

Same code, written in assembly language:

LDS r1, #0001
INC r1
STS r1, #0001

Even though the assembly code looks very clumsy, it is exactly what the computer will do to increase a number in memory, inside the CPU. 

High level translators

  • There are mainly two types of translators for high level languages:
    • Compiler
    • Interpreter

Compiler

  • Translates high level language to machine code
  • Results in (direct) executable machine code
    • e.g. Windows .exe files are executable machine code files
  • It translates whole program codes into executable, and once it is done, the program can be executed independently (without compiler)
  • If there's syntax error in the code, compiler will try to record all the error found and produce a summary of errors to the programmer to fix. 

Interpreter

  • Interpreter also translates high level language to machine instructions
  • Instead of translating the whole program codes, it only works line by line. 
    • Translates one line, and then execute, and then next line. 
  • If syntax error found on one line, interpreter will stop immediately and report to the programmer
  • No independent executable file is created. The program written in interpreter language requires the interpreter program to execute. 

Compiler vs interpreter

  • Consider you have a recipe, written in an language that you don't know (e.g. Russian?)
  • Compiler is, you find a friend to translate the whole recipe and written down. 
  • Interpreter is like you have a friend that reads Russian, and tell you each step of what to do on the recipe. 
  • So, consider the following questions:
    • When do you need the translator (a friend)?
    • Which one is faster in executing the program? Compiler or interpreter? 
    • If error is found, which one gives more instant feedback? 

Drawbacks of Interpreter language

  • Slower execution of code
  • Protection of source code

Research and discuss

  1. We have mentioned that Python, like many interpreter languages, executes slower than compiled languages. But according to the video, interpreter language like Python or Javascript are getting more and more popular over time compare to those compiled ones like C / Java. Even those computation intensive application like AI, for instance, Tensorflow, one of the most famous Deep Learning framework by Google, includes Python version. Why so many developers choose Python?

Types of error

  • There are in general, three types of programming errors:
    • Syntax
    • Run-time
    • Logic

Syntax Error

  • Syntax error can be (most of them) detected by a translator during translation
  • They are grammatically wrong statements, which simply the computer won't understand them
  • e.g. (python)
    • A + 1 = A 
    • whlie a > 1: (wrong spelling)

Run-time error

  • Run-time error appeared when program is executed
  • E.g. 
    • A = int(input("enter a number"))
      ..the user inputted 0 (zero)
      ..
      print( 15 / A)
  • The above codes are perfectly valid, in terms of syntax, thus the translators won't report anything
  • But once the program is executed, there will be a division by zero error
  • To avoid run-time error, we need data validation

Logic Error

  • Logic error is an error that the program cannot perform what it is expected to do
  • Translators are not able to pickup logic error
  • Programmer can found by performing test on data, using the techniques you've learnt:
    • Dry-running
    • Trace table
    • Tests
    • etc. 
Made with Slides.com