What Is a Program?
A program is a set of instructions
Section titled “A program is a set of instructions”A program is a set of instructions written to make a computer perform some task. That task might be adding two numbers, displaying a website, decoding a photograph, or keeping an alarming number of browser tabs alive.
The instructions usually begin as source code: text written in a programming language. A programming language gives us a defined vocabulary and grammar for expressing operations. The grammar matters because the computer cannot politely infer what we probably meant.
Consider this tiny Python program:
name = "Ada"message = "Hello, " + nameprint(message)Even before learning Python, we can make a reasonable first pass:
- Store the text
Adaunder the namename. - Combine
Hello,with the stored name and call the resultmessage. - Display the message.
The source code is not the running program. It is a durable description of what should happen. When the program runs, the computer creates a process that carries out those instructions. You encountered processes in Linux Foundations; now we care about what one is doing internally.
Four questions make code less mysterious
Section titled “Four questions make code less mysterious”When you meet a small program, begin with four questions:
- What input can enter?
- What operations are performed?
- What state changes while it runs?
- What output or effect does it produce?
Input is data received by the program. It may come from the keyboard, a command-line argument, a file, a network connection, or another program.
An operation is work performed on that data: adding numbers, comparing text, copying bytes, or choosing which instruction comes next.
State is information the program currently remembers. In the example, name and message are pieces of state. State can change as instructions run.
Output is what leaves the program or changes outside it. Printed text is output, but so is a written file, a network message, or an exit status returned to the shell.
These categories are not merely vocabulary for a quiz. Vulnerability research often begins by asking whether unusual input can drive state and operations toward an output the author did not intend.
Knowledge check
Question 1 of 2
Knowledge check complete
You answered all 2 questions correctly.
How source code becomes behavior
Section titled “How source code becomes behavior”A processor does not directly execute Python or C source code as written. Something must bridge the gap between the language humans work with and instructions the machine can execute.
An interpreter reads a program and carries out its meaning. Python is normally introduced this way: the python3 program reads your Python source and executes it.
A compiler translates source code into another form before you run it. A C compiler can turn C source into a native executable containing machine instructions for a particular platform.
That distinction is useful, but reality has more machinery underneath it. Python first converts source into an intermediate bytecode, and modern compilers perform several stages of processing before producing an executable. We will inspect those details when they help answer a real question. For now, remember the practical difference:
- With our Python examples, we give the source file to
python3when we want it to run. - With our C examples, we compile the source into an executable and then run that executable.
Neither approach makes one language a toy and the other a “real” language. They expose different parts of the system, which is why we will use both.
Run the smallest useful example
Section titled “Run the smallest useful example”Open a terminal in your Linux course VM. First, confirm that Python 3 is available:
python3 --versionYou should see output beginning with Python 3. The remaining version numbers may differ from the example used to write this course, and that is fine.
If Python is not installed
Section titled “If Python is not installed”If the terminal reports something similar to:
python3: command not foundthen Python 3 is not currently available through the normal command path. On the Ubuntu course VM, install it with Ubuntu’s package manager.
First, refresh the package list:
sudo apt updatesudo asks Linux to run this administrative command with elevated privileges. Enter the password for your course VM when prompted. The terminal will not display dots or asterisks while you type the password; it is still accepting the keystrokes.
After the package list finishes updating, install Python 3:
sudo apt install python3When apt asks whether you want to continue, review the proposed change, type y, and press Enter. Then verify the result:
python3 --versionDo not substitute python if that command is missing. Some Linux systems do not create a python command, and others may configure it differently. This course uses python3 explicitly so there is no mystery about which major version we intend to run.
Create a directory for this module:
mkdir -p ~/underdunn-course/programming-foundationscd ~/underdunn-course/programming-foundationspwdThe final pwd output should end with:
/underdunn-course/programming-foundationsOpen the Text Editor application. Enter these three lines exactly:
name = "Ada"message = "Hello, " + nameprint(message)Save the file as hello.py in Home/underdunn-course/programming-foundations.
Return to the terminal and confirm the file is where you expect it:
ls -l hello.pyfile hello.pyfile should identify it as text. That tells us about the stored source file, not what the instructions will do.
Run the program:
python3 hello.pyThe output should be:
Hello, AdaCompare that observation with your prediction. The first line created state named name. The second line read that state, performed a text-combination operation, and created new state named message. The third line produced output.
The quotation marks do not appear because they mark text in the source code; they are not part of the text between them.
Change one thing at a time
Section titled “Change one thing at a time”Edit hello.py and replace Ada with your name. Before running it, predict exactly which part of the output will change and which parts will stay the same.
Run it again:
python3 hello.pyYou changed one input value embedded in the source while leaving the operations alone. If several lines changed unexpectedly, recheck the file rather than adding more changes. Controlled experiments are much easier to reason about than a pile of simultaneous edits.
Now add a second print instruction:
name = "your name"message = "Hello, " + nameprint(message)print("The program reached the end.")Predict the order of the two lines, then run the program. Python normally carries out these instructions from top to bottom. Later, conditions, loops, and functions will make the path less direct.
Errors are output too
Section titled “Errors are output too”Temporarily remove the closing quotation mark from the first line:
name = "AdaRun the program again. Python should report a SyntaxError and point near the place where it could no longer understand the source.
A syntax error means the source does not follow the language’s grammar, so Python cannot begin carrying out the program. The error message is evidence: it identifies the type of failure, the file, a line, and often the part that first became impossible to parse.
Repair the quotation mark and run the program once more. Do not leave the exercise in a broken state simply because the broken state was educational.
Record the behavior as evidence
Section titled “Record the behavior as evidence”Add a short entry to your research notebook:
| Question | Observation |
|---|---|
| What was the source file? | The absolute path to hello.py |
| What ran the source? | The command you used |
| What state did the program create? | The names and values you observed in the code |
| What output did it produce? | The exact terminal output |
| What happened after the syntax was broken? | The error type and referenced line |
This may feel excessive for three lines of Python. The program is deliberately simple so we can practice the method without drowning in code. Later, the same record will keep an unfamiliar program from turning into a vague memory of commands you tried.
Knowledge check
Question 1 of 3
Knowledge check complete
You answered all 3 questions correctly.
What this gives us as researchers
Section titled “What this gives us as researchers”You can now separate the file containing source code from the process created when that code runs. You also have a first-pass method for reading a program: identify its input, operations, state, and output; predict behavior; run it; and compare the evidence with the prediction.
Next, Python Scripts and Values will replace the fixed name with data supplied when the program runs. That is where programs become more useful—and where the input begins to deserve suspicion.