Programming

  • Functions
    • arguments, return values
  • Conditionals
  • Boolean expressions
  • Loops
  • Variables
OIM3600
OIM3600

C

#include <stdio.h>

int main(void)
{
    printf("Hello, world!\n");
}

OIM3600

Rust

fn main() {
    println!("Hello, world!");
}
OIM3600

Java

public class HelloWorld 
{
    public static void main(String[] args) 
    {
        System.out.println("Hello, world!");
    }
}
OIM3600

Python

print("Hello, world!")
OIM3600

JavaScript

console.log("Hello, world!");
OIM3600

Source Code

OIM3600
01111111 01000101 01001100 01000110 00000010 00000001 00000001 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000010 00000000 00111110 00000000 00000001 00000000 00000000 00000000
10110000 00000101 01000000 00000000 00000000 00000000 00000000 00000000
01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
11010000 00010011 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 01000000 00000000 00111000 00000000
00001001 00000000 01000000 00000000 00100100 00000000 00100001 00000000
00000110 00000000 00000000 00000000 00000101 00000000 00000000 00000000
01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
01000000 00000000 01000000 00000000 00000000 00000000 00000000 00000000
01000000 00000000 01000000 00000000 00000000 00000000 00000000 00000000
11111000 00000001 00000000 00000000 00000000 00000000 00000000 00000000
11111000 00000001 00000000 00000000 00000000 00000000 00000000 00000000
00001000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000011 00000000 00000000 00000000 00000100 00000000 00000000 00000000
00111000 00000010 00000000 00000000 00000000 00000000 00000000 00000000
...
OIM3600
>objdump -d hello.o  

hello.o:     file format pe-x86-64

...

0000000000000054 <main>:
  54:   55                      push   %rbp
  55:   48 89 e5                mov    %rsp,%rbp
  58:   48 83 ec 20             sub    $0x20,%rsp
  5c:   e8 00 00 00 00          call   61 <main+0xd>
  61:   48 8d 05 00 00 00 00    lea    0x0(%rip),%rax        # 68 <main+0x14>
  68:   48 89 c1                mov    %rax,%rcx
  6b:   e8 90 ff ff ff          call   0 <printf>
  70:   b8 00 00 00 00          mov    $0x0,%eax
  75:   48 83 c4 20             add    $0x20,%rsp
  79:   5d                      pop    %rbp
  7a:   c3                      ret
OIM3600

Machine Code

OIM3600
input
→
algorithm
→
output
OIM3600
source code
→
compiler / interpreter
→
machine code
OIM3600
OIM3600

What actually happened?

  • C is a compiled language.
  • C's compilation process:
    1. Preprocess: gcc -E hello.c -o hello.i (expands macros, includes headers, deletes comments, creates preprocessed source code)
    2. Compile: gcc -S hello.i -o hello.s (creates assembly code)
    3. Assemble: gcc -c hello.s -o hello.o (creates object file which is machine code)
    4. Link: gcc hello.o -o hello (combines object files and libraries, creates executable file)
  • Note:
    • Different compilers may have slightly different commands or steps.
    • Compilation process occurs before program execution.
OIM3600
OIM3600

What actually happened?

  • Python is an interpreted language (with a compilation step.)
  • Python's execution process:
    1. Parse and compile: Source code to bytecode
      • python -m py_compile hello.py (creates .pyc file)
      • python -m dis hello.py (displays readable bytecode)
    2. Interpret: Bytecode executed by Python Virtual Machine (in CPython)
  • Note:
    • CPython is the default and most used interpreter, but others exist.
    • Interpreter converts source code or bytecode into machine code at runtime.
    • This process occurs line-by-line or block-by-block during execution.
OIM3600
OIM3600
input
→
Algorithm
→
output
OIM3600
arguments
→
function
→
side effects
OIM3600
→
→
OIM3600
print("Hello, world!")
OIM3600
arguments
→
function
→
return values
OIM3600
→
→
OIM3600
answer = input("What is your name?")
OIM3600
print("Hello, " + answer)
OIM3600

Conditionals

OIM3600
if x < y:
    print("x is less than y")
OIM3600
if x < y:
    print("x is less than y")
else:
    print("x is not less than y")
OIM3600
if x < y:
    print("x is less than y")
elif x > y:
    print("x is greater than y")
else:
    print("x is equal to y")
OIM3600

Variables

OIM3600
counter = 0
OIM3600
counter = counter + 1

# or

counter += 1
OIM3600

Loop

OIM3600
counter = 3

while counter > 0:
    print("meow")
    counter = counter - 1
OIM3600
i = 3

while i > 0:
    print("meow")
    i -= 1
OIM3600
i = 1

while i <= 3:
    print("meow")
    i += 1
OIM3600
i = 0

while i < 3:
    print("meow")
    i += 1
OIM3600
// using C:

for (int i = 0; i < 3; i += 1)
{
    printf("meow\n");
}
OIM3600
// using Python:

for i in range(3):
    print("meow")
OIM3600
while True:
    print("meow")
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600