Unit 1: Computer Languages

CSE111 — Orientation To Computing 9 min read

I. Orientation: Communicating with Computers

Computer languages are formal systems used to express instructions that a computer can store, translate, and execute. They developed from machine-dependent binary instructions to increasingly readable languages such as Python, Java, and C++. The governing principle is that a processor ultimately executes machine-language instructions, regardless of the language used by the programmer.

  • Binary representation: Computer instructions and data are represented internally using bits, with 0 and 1 corresponding to two electrical or logical states.
  • Translation requirement: Assembly and high-level programs must be translated into machine code before a processor can execute them.
  • Machine dependence: Machine language and assembly language are closely tied to a processor's instruction set, while high-level languages are generally more portable.
  • Language levels: A lower-level language provides greater hardware control; a higher-level language provides greater abstraction and programmer convenience.
  • Program correctness: A program must satisfy both syntax rules, which govern form, and semantic rules, which govern meaning.
  • Execution basis: The central processing unit (CPU) fetches, decodes, and executes machine instructions stored in memory.

II. Machine Language

A. Machine Language

Machine language is the lowest-level programming language, consisting of binary instructions directly understood by a particular CPU.

  • Instruction format: A machine instruction commonly contains an operation code, or opcode, and one or more operands. An opcode such as ADD specifies the operation, while an operand identifies data or a memory location.
  • Binary encoding: An instruction may be represented as a sequence such as:
TEXT
  10110000 01100001

The exact meaning depends on the processor's instruction set and encoding rules.

  • Direct execution: No translator is required after the instruction has been stored in executable memory because the CPU operates on machine code.
  • Hardware dependence: A program written for an x86 processor will not necessarily run on an ARM processor because their instruction sets and encodings differ.
  • Advantages: Machine language provides maximum execution control and can use processor resources efficiently.
  • Limitations: Binary notation is difficult to read, write, debug, modify, and maintain. A single incorrect bit can change an instruction completely.

III. Assembly Language

A. Assembly Language

Assembly language is a symbolic, low-level language that represents machine instructions with readable abbreviations called mnemonics.

  • Symbolic instructions: A statement such as MOV R1, #5 may mean “place the constant 5 into register R1.” MOV is a mnemonic, R1 is a register, and #5 is an immediate value.
  • One-to-one relationship: In many processors, one assembly statement corresponds closely to one machine instruction, although pseudo-instructions may expand into several instructions.
  • Use of labels: Labels represent addresses, making jumps and data references easier to write:
TEXT
  START:  MOV R1, #5
          ADD R2, R1, R1
          B START

Here, START identifies a program location and B represents a branch instruction.

  • Assembler requirement: Assembly source must be translated into machine code by an assembler before execution.
  • Advantages: Assembly provides detailed control over registers, memory, interrupts, and hardware operations. It is useful in device drivers, embedded systems, and performance-critical routines.
  • Limitations: It remains processor-specific and requires more code than a high-level language. Programs are harder to maintain and less portable.

IV. High-Level Languages

A. High-Level Languages

High-level languages use English-like keywords, mathematical notation, and structured constructs to hide most hardware details from the programmer.

  • Abstraction: A statement such as total = price * quantity expresses a calculation without specifying registers or memory addresses.
  • Examples: C, C++, Java, Python, and JavaScript are high-level languages, although they differ in design, execution model, and typical applications.
  • Portability: The same source program can often be used on different systems after translation by an appropriate compiler or interpreter.
  • Structured programming: Constructs such as if, while, functions, and classes organize complex programs into understandable units.
  • Data representation: A declaration such as int count = 10; identifies count as an integer variable initialized to 10; the translator handles its lower-level storage.
  • Advantages: High-level languages improve productivity, readability, debugging, reuse, and software maintenance.
  • Limitations: Translation may introduce runtime overhead, and some hardware-specific operations require libraries, extensions, or embedded assembly.

V. Compiler

A. Compiler

A compiler is a translator that converts an entire high-level source program, or a substantial part of it, into another form such as machine code or intermediate code before execution.

  • Input and output: The source file may contain C++ statements, while the compiler produces object code, an executable file, or bytecode.
  • Analysis stages: A compiler generally performs lexical analysis, syntax analysis, semantic analysis, optimization, and code generation.
    • Lexical analysis: Characters are grouped into tokens such as identifiers, operators, and keywords.
    • Syntax analysis: Tokens are checked against grammar rules; x = + 4 may be rejected if the language grammar does not allow that form.
    • Semantic analysis: Meaning and type rules are checked, such as preventing a string from being used where an integer is required.
  • Error reporting: Compile-time errors are reported before the program runs. For example, a missing semicolon in C may prevent object-code generation.
  • Optimization: The compiler may replace equivalent instructions with faster or smaller code, while preserving the program's meaning.
  • Advantages: Compiled programs often execute quickly because translation has already occurred.
  • Limitations: A compiled program may need recompilation for another operating system or processor, and compilation errors must be corrected before execution.

VI. Interpreter

A. Interpreter

An interpreter translates and executes a program during runtime, usually statement by statement or through an intermediate representation.

  • Runtime operation: When an interpreter encounters print(2 + 3), it evaluates the expression and performs the output as the program runs.
  • Immediate feedback: Errors can be identified during execution without first producing a complete native executable.
  • Typical use: Interpreted execution is common in scripting, command shells, educational environments, and rapid application development.
  • Portability: Programs may run on different systems when a compatible interpreter is available, such as a Python interpreter or JavaScript engine.
  • Advantages: Interpreters support interactive testing, flexible execution, and simpler development cycles.
  • Limitations: Repeated translation or runtime checking can make execution slower than well-optimized native code. Errors in later statements may remain undiscovered until those statements are reached.
  • Hybrid systems: Some languages first translate source code into bytecode, after which a virtual machine interprets or just-in-time compiles it.

VII. Assembler

A. Assembler

An assembler translates assembly-language source into machine-readable object code for a specific processor.

  • Mnemonic translation: An instruction such as ADD R1, R2 is converted into the binary opcode and operand fields defined by the processor.
  • Symbol resolution: The assembler assigns addresses to labels and replaces symbolic references with numeric addresses.
  • Two-pass process: In a first pass, labels and instruction locations are recorded; in a second pass, symbolic references are converted into final machine addresses.
  • Object file creation: The assembler commonly produces an object file containing machine code, symbol information, and relocation information.
  • Linking relationship: If an assembly program calls a library routine, a linker may combine its object file with other object files to form an executable.
  • Error detection: Invalid mnemonics, incorrect operands, undefined labels, and illegal addressing modes are reported during assembly.
  • Distinction from a compiler: An assembler translates symbolic instructions that closely represent a processor's instruction set; a compiler translates higher-level structures and decisions into lower-level instructions.

VIII. Program Development Cycle

A. Program Development Cycle

The program development cycle is the organized sequence used to transform a problem into a tested and maintainable software solution.

  • Problem definition: The required result, inputs, outputs, constraints, and users are identified. For a payroll program, inputs may include hours and pay rate, while output is gross pay.
  • Analysis: The problem is divided into data requirements, processing rules, and exceptional cases such as negative hours.
  • Algorithm design: A finite, ordered solution is prepared using pseudocode or a flowchart:
TEXT
  READ hours, rate
  grossPay = hours * rate
  DISPLAY grossPay

hours and rate are inputs; grossPay is the calculated output.

  • Coding: The algorithm is expressed in a selected programming language using variables, operators, control structures, and functions.
  • Translation: A compiler, interpreter, or assembler converts the source into a form that can be executed.
  • Testing and debugging: Test data is used to expose syntax, runtime, and logic errors. A boundary test might use hours = 0 to check whether the result is handled correctly.
  • Documentation: Internal comments, user instructions, and technical descriptions explain how the program works and how it should be maintained.
  • Maintenance: After release, the program may be corrected, adapted to new systems, or enhanced with additional features.

IX. Compilation and Execution Process

A. Compilation and Execution Process

The compilation and execution process explains how source code becomes a running program, from translation through loading and CPU execution.

  • Source preparation: The programmer writes source code in a file such as main.c, Main.java, or program.py. Source code is designed for human reading and is not directly executed by the CPU.
  • Preprocessing: In languages such as C and C++, a preprocessor expands directives such as #include and replaces macros before compilation.
  • Compilation: The compiler translates source statements into assembly or another intermediate form, checks rules, and generates lower-level code.
  • Assembly: If assembly output is produced, the assembler converts it into an object file containing machine instructions.
  • Linking: The linker combines object files and libraries. A call such as printf() may be connected to the implementation stored in a standard library.
  • Loading: The operating system loader places the executable into memory, prepares required addresses, and establishes the program's initial execution environment.
  • CPU execution: The processor repeatedly performs the instruction cycle:
    1. Fetch: Retrieve the next instruction from memory.
    2. Decode: Determine the operation and operands.
    3. Execute: Perform the operation, such as adding two register values.
    4. Store: Save the result in a register or memory when required.
  • Worked example: For sum = a + b, translation may produce instructions that load a and b into registers, execute an addition, and store the result in the memory location associated with sum.
  • Error categories: Compilation errors occur before an executable is produced; runtime errors occur during execution, such as division by zero; logic errors produce incorrect results despite successful execution.
  • Execution models: A native compiler usually translates before execution, an interpreter translates during execution, and a virtual machine may combine bytecode interpretation with just-in-time compilation.