Introduction

Programming Languages and Translators

High-Level Languages

  • Definition: Programming languages that are closer to human languages and abstract away most of the hardware details.
  • Purpose: Easier to read, write, and maintain; used for developing complex applications.
  • Examples: Python, Java, C++

Here's an example of a simple Python program that calculates the sum of numbers from 1 to 10. Even if you don't know the language it is likely you could work out what was happening here.

# High-Level Language: Python

total = 0
for i in range(1, 11):
    total = total + i

print("The sum of numbers from 1 to 10 is: "+str(total))

Low-Level Languages

  • Definition: Programming languages that are closer to machine language and provide little or no abstraction from a computer's instruction set architecture.
  • Purpose: Offer greater control over hardware; used for system programming and performance-critical applications.
  • Examples: Assembly language, Machine code

Here's the same functionality as before implemented in Little Man Computer (LMC) assembly language. Notice how much more difficult it is to understand what is happening here.

# Low-Level Language: LMC

        INP         # Input value (not needed for sum 1 to 10, placeholder)
        STA COUNT   # Store input in COUNT
        LDA ZERO    # Initialize TOTAL to 0
        STA TOTAL   # Store 0 in TOTAL
        LDA ZERO
        STA SUM
        LDA COUNT
        BRZ END     # If COUNT is 0, end the program
LOOP    LDA TOTAL   # Load TOTAL
        ADD COUNT   # Add COUNT to TOTAL
        STA TOTAL   # Store result in TOTAL
        LDA COUNT   # Load COUNT
        SUB ONE     # Subtract 1 from COUNT
        STA COUNT   # Store result in COUNT
        BRP LOOP    # If COUNT is positive, repeat loop
END     LDA TOTAL   # Load TOTAL
        OUT         # Output TOTAL
        HLT         # Halt program

COUNT   DAT 10      # Initialize COUNT to 10
TOTAL   DAT 0       # Initialize TOTAL to 0
ZERO    DAT 0       # Constant 0
ONE     DAT 1       # Constant 1

Mnemonics for Low-Level Languages

The above code is a good example of the various mnemonics in low-level languages. Mnemonics are symbolic names used to represent machine language instructions. These abbreviations help programmers write code that is easier to understand and remember compared to raw binary or hexadecimal instructions.

One line of assembly language is the same as one line of machine code. This means that coding in a low level language can be more closely tied to the systems architecure resulting in more effecient running software. However, the complexity of coding this way means that much of that effciency might be lost due to poorly design code.

Note

You don't need to be able to code in LMC for GCSE, you do for A level.

However, a little understanding of the LMC can help you understand how the processor works and in particular its registers.

Examples of Mnemonics in LMC (Little Man Computer)

  • ADD: Adds the value from a specified address to the accumulator.
  • SUB: Subtracts the value at a specified address from the accumulator.
  • STA: Stores the value in the accumulator to a specified address.
  • LDA: Loads the value from a specified address into the accumulator.
  • INP: Takes an input and stores it in the accumulator.
  • OUT: Outputs the value in the accumulator.
  • BRZ: Branches to a specified address if the accumulator is zero.
  • BRP: Branches to a specified address if the accumulator is positive.
  • HLT: Halts the program.

Using mnemonics makes it easier to read and debug low-level code, bridging the gap between human-readable code and machine instructions.

LMC Simulator

Differences Between High-Level and Low-Level Languages

  • Abstraction: High-level languages provide more abstraction from hardware than low-level languages.
  • Ease of Use: High-level languages are easier to learn and use, while low-level languages require detailed knowledge of the hardware.
  • Performance: Low-level languages can be more efficient and faster, but high-level languages are generally slower due to abstraction layers.

Translators

You can't just run a high level language on a computer. To run the code you need to translate it first into the correct type of code (machine code) for that computer to understand.

Translator

Software that converts code written in one programming language into another language. To convert high-level code into machine code that a computer's processor can execute.

Types of Translators

Compiler

A compiler is a type of translator that some languages will use. This will translate the whole program before it can be run. If there are any errors in the code then the compilation fails and the program won't work at all.

Compiler

A translator that converts the entire source code of a program into machine code before execution.

  • Converts code all at once
  • Produces an executable file
  • Performs extensive error checking

Benefits:

  • Faster execution time, since the code is already translated.
  • Can distribute the software without the source code

Drawbacks:

  • Longer initial compilation time
  • Harder to debug
  • Code won't run at all if it has syntax errors

Interpreter

Python is good way to understand how an interpreted language works. When you run python code each line is translated line by line and runs. This is why python will run until it hits an error.

Interpreter

A translator that converts source code into machine code line-by-line during execution.

  • Converts code line by line
  • No separate executable file is produced
  • Executes code directly and stops at the first error

Benefits:

  • Easier to debug, as errors are found and can be corrected immediately.
  • Code will run up until an error is found

Drawbacks:

  • Slower execution time, since translation happens during runtime.
  • Need to provide the source code when distributing

Understanding these concepts helps in choosing the right tools and languages for different programming tasks, ensuring efficient and effective software development.

For your course you will be studying Python which is an interpreted language. In A level you also study JavaScript which is another interpreted language.

Previous
Computational Thinking