Foundations of programming

Published on:

Understanding computer programs

People use computers to perform tasks.

What “tasks”, you might ask? Well, you use it to perform mathematical calculations, write documents, design graphics, watch movies, send emails, play games, shop online, learn programming, you get the point.

However, you can’t just get on a computer and magically do the things you want.

Computers are incredibly fast, but they are not smart on their own. They can’t make decisions or perform tasks unless a human writes a set of specific commands for them to follow. That set of commands is called a “program”.

Technically, computers can make decisions, but only based on rules and logic that has been explicitly programmed.

This means that the things that we talked about earlier (like writing documents, designing graphics, watching movies, etc.) are actually performed by programs, like Microsoft Word, Photoshop, Google Chrome, Netflix, etc., that are developed and maintained by humans.

At its simplest, a computer program is a list of instructions that tells a computer what to do. In other words, you tell your computer what to do through a computer program.

Types of Programs

  1. System Software: Programs that run the computer.
    1. Firmware: Programs baked into the hardware that run immediately when you power on the machine. Without firmware, the OS (operating system) never even gets a chance to start. Examples: BIOS, UEFI.
    2. Operating Systems: Software that manages hardware resources and provides a platform for applications to run. Examples: Linux, macOS, Windows, iOS, Android.
    3. Device Drivers: Programs that enable the operating system to communicate with specific hardware devices. Examples: graphics card drivers, printer drivers, network adapters, USB drivers.
    4. Language Processors (or Translators): Software that converts human-readable source code into machine-executable instructions. Examples: compilers, interpreters, assemblers.
    5. System Utilities: Tools that maintain, monitor, and optimize the computer system’s performance and health. Examples: antivirus software, disk cleaners, backup tools
  2. Application Software (Apps): Programs designed for users to perform specific tasks. Examples: Google Chrome for web browsing, Microsoft Word for writing documents, Zoom for online meetings.
  3. Programming Software (Development Tools): Programs that create other programs. Tools used by developers to create, debug, and maintain other programs. Examples: IDEs, code editors, debuggers, version control systems, build tools.

Machine code

Your computer is an electronic device that runs on electricity. The only language it truly understands is binary, i.e. 0s and 1s.

Earlier we said that a program is a set of instructions. If you were to write a program that your computer could understand and execute, it would look like this:

10100001
10111100
10010011
00000100
00001000
00000011
00000101
11000000
...

This is called machine code or binary instructions. Believe it or not, these numbers actually contain instructions that your CPU can understand to perform operations like data transfer, arithmetic operations, logical instructions, control flow, read/write operations, etc.

In the 1940s, there was no coding as we know it today. Programmers literally flipped physical switches on a panel or plugged cables into holes to complete electrical circuits. It was incredibly slow and error-prone. If you flipped the wrong switch by mistake, the whole calculation failed, and you had to start over. You had to memorize that 10110000 meant “move data” for that machine.

Two early programmers (Gloria Ruth Gordon [Bolotsky] and Esther Gerston) at work on the ENIAC.

Machine code is not universal. If you take the machine code (the 1s and 0s) from a Windows PC and try to run it on a Mac, it won’t work. This is because the instruction set (the dictionary of 1s and 0s) is different for an Intel processor than it is for an Apple/ARM processor.

Assembly language

Clearly, writing machine code is too much work. Imagine, as a programmer, if you are expected to write code literally in 0s and 1s. It would be a nightmare to build applications this way.

The breakthrough came in 1947 when pioneers like Kathleen Booth wrote the first assembly language and designed the assembler.

This is a program in assembly language for an 8085 microprocessor to add two 8-bit numbers and store the result in memory.

LXI H, 2501H
MOV A, M
INX H
ADD M
INX H
MOV M, A
HLT

As you can see, it’s quite cryptic but still a huge upgrade from binary (0s and 1s).

As I mentioned earlier, Kathleen Booth designed the assembler. Assembler is essentially the program that converts assembly language into machine code (binary instructions) that the CPU can execute.

For example, this is an assembly language program:

MOV AX, 5
ADD AX, 3

The assembler would convert it to machine code (conceptual):

10110000 00000101
00000101 00000011

Remember, the CPU only ever understands binary machine code and nothing else.

Diagram showing that a CPU can only decode and execute binary machine instructions.

This means that any programming language must have a translator that converts it into machine code. For assembly language, that translator is called the assembler.

Assembly to machine code

Assembly is a low-level programming language. High-level programming languages (like C++, Python, Java) are designed to be human-friendly. Assembly is designed to be hardware-transparent.

Assembly is not portable. This means that an assembly program written for x86 processor (Intel/AMD chips in most laptops) will not work on ARM processor (chips in phones and MacBooks). This is because assembly instructions map directly to machine instructions, and since machine code is not universal (as mentioned above), neither is assembly.

How programming languages work

In very simple terms, a programming language is the way we communicate with the computer.

Why do we need programming languages in the first place? Let’s recap a bit:

  • People want to use computers to do stuff
  • But computers cannot do them magically
  • Programs running on the computer enable you to do stuff
  • Those programs have to be developed by humans
  • To develop a program, you have to provide a set of instructions to the computer
  • But the computer only understands binary machine code (0s and 1s)
  • Writing machine code is very hard for us
  • So we invent programming languages that make it easy for us to write code, develop programs and build applications
  • For the computer to be able to understand that programming language, we have to invent a translator that converts programs written in that programming language to machine code

The last two points capture the main essence of what I want to explain in this section. When you invent a new programming language, you invent two things:

  • Programming language: Specification (or documentation) that defines the syntax and semantics. It’s a set of rules, grammar, and logic.
  • Translator: Implements the language specification. This is the program that is responsible for converting code written in that programming language to machine code. It’s rarely ever called a “translator” though. It comes with different names:
    • Assembler for assembly language
    • Compiler for compiled languages like C, C++
    • Interpreter for interpreted languages like PHP, JavaScript, Python

Think of it like this:

  • Programming language is the menu that specifies a list of items that you can order
  • Translator is the chef who turns the order into a meal
  • Machine code is the final meal the customer consumes

It’s not a perfect analogy, but I hope you get the point.

The irony is that to a computer, a programming language is a human language; whereas to a human, it is a computer language.

Syntax

Syntax refers to the specific set of rules that dictate the structure, format, and arrangement of code elements so that the program can be understood and executed by a computer.

It is analogous to grammar in human languages, ensuring that instructions are clear and consistent.

For example:

  • Sentences in English must end with a period(.).
  • Statements in the C programming language must end with a semicolon (;).

Semantics

Semantics refers to the meaning and behavior of code, or what the code actually does when executed.

While syntax defines the grammatical rules for how to write code (the structure), semantics defines what that code does. Syntax checks form; semantics defines function.

For example:

  • In the English statement “Hello.”, you are greeting someone or starting a conversation.
  • In the C statement int x = 5;, you are assigning the value 5 to an integer variable named x.

If your code fails to compile, it’s usually a syntax error.

If it compiles successfully but behaves incorrectly, it’s usually a semantic issue.

Sign in to save your progress, leave comments, share feedback, and earn a certificate when you complete the course.

Comments 0