Today’s Outline


  • What’s a computer
  • Programming languages
  • Basics of computer programming

What’s a computer


Arithmetic Circuits

Stored Program Computer

Instruction

  • A sequence of bits that defines a single operation of a CPU
    • e.g. add, sub, read, write

Instruction Set Architecture, ISA

  • ISA is the specification of instructions supported by a CPU
    • The dictionary of CPU language
    • e.g. x86, RISC, …
  • ISA is the interface between HW and SW
    • Different CPUs can run the same computer program as long as they support the same ISA

Programming Languages


Machine Language

  • A language that is directly understood by the computer.
  • Defined by ISA such as x86, RISC, …

Symbolic Language

  • English-like abbreviations representing elementary computer operations.
  • e.g. Assembly language

High-level Language

  • Close to human language.
  • e.g. C/C++, Java, Python, …

Compiler

  • Computers only understand binary code
  • Humans write programs using a high-level language
  • The compiler translates high-level language programs to binary code

Compiled Languages

  • Compiled language programs are compiled to binary machine code, which is then directly executed by the HW
  • e.g. C/C++, Rust, Pascal, …
  • Pros - fast
  • Cons - need to re-compile on a different HW

Interpreted Languages

  • Programs are first compiled to an Intermediate Representation, IR, which is then converted by a Virtual Machine, VM to machine code and executed by the HW
  • e.g. Java, Python, …
  • Pros - better portability
  • Cons - slow

Basics of Computer Programming


Computer Program (External View)

  • Basic elements of a computer program
    • Input
    • Process
    • Output

Computer Program (Internal View)

  • A list of instructions ordered logically
  • Usually, involve data access

Developing a C++ Program

  1. Coding
    • Write source code into a file
    • e.g. hello.cpp
  2. Compile
    • Source code is converted to object code in machine language
    • e.g. hello.obj
  3. Link
    • Combines objects and libraries to create a binary executable
    • e.g. hello.exe

Function


ReturnType FunctionName(input parameters)
{
    instructions within function body
}
  • A sequence of instructions grouped together, which implement a specific task.

main()

  • main() is a special function, which is the starting point of a C++ program.

Statement


cout << "Hello world!" << endl;
  • A syntactic unit that expresses some action to be carried out.

cout

  • An output function provided by iostream library
  • cout
    • Console Output allows our program to output values to the standard output stream
  • <<
    • output operator, which output values to an output device
    • The right-hand side of the << is the string to output
  • endl
    • end of the line. advance the cursor on the screen to the beginning of the next line

#include


#include <iostream>
using namespace std;
 
void main()
{
    cout << "Hello world!" << endl;
}
  • Include the libraries you want to use
  • e.g. iostream commonly used functions for I/O, including cout
  • e.g. cmath commonly used math functions, such as sin() cos() log()

using namespace


#include <iostream>
using namespace std;
 
void main()
{
    cout << "Hello world!" << endl;
}
  • namespace
    • A declarative region that provides a scope to the identifiers inside it
    • Declare namespace to avoid writing the full name
  • e.g. std namespace is used such that we can write cout instead of std::cout

Summary


What’s Computer

  • Numbering System
  • Logic gates and circuits
  • Stored Program Computer and ISA

Programming Language

  • Machine / Symbolic / High-level Language
  • Compiler

Basics of Computer Programming

  • Logic Flow
  • Developing: Coding / Compilation / Linking
  • C++ Syntax: Function / Statement / …