CMPT 295: Unit - Machine-Level Programming

Last Lecture

Today’s Menu

What could these 32 bits represent?

What kind of information could they encode?

00011000111011001000001101001000

Answer:

C program in memory?

We have just spent a few lectures looking at how our data can be represented as a series of 0’s and I’s, now …

  1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)?
  2. Question: Then, how does our C program (once it is represented as a series of 0’s and 1’s) end up being stored in memory?
  3. Question: Then, how does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)?

Demo – C program: sum_store.c

  1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)?

Let’s answer these questions with a demo

Turning C into machine code - gcc

The Big Picture (transcriber’s note: each step depends on the last; this is implied in the diagram):

Snapshot of compiled code

Fetch-Execute Cycle

Terms:

PC - program counter
Defn: register containing address of instruction of ss that is currently executing
IR - instruction register
Defn: register containing copy of instruction of ss that is currently executing

Question: How does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)?

Answer: The microprocessor executes the machine code version of our C program by executing the following simple loop:

DO FOREVER:

Summary

  1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)?
    • Compiler: C program -> assembly code -> machine level code
    • gcc: 1) C preprocessor, 2) C compiler, 3) assembler, 4) linker
  2. Question: How does our C program (once it is represented as a series of 0’s and 1’s) end up being stored in memory?
    • When C program is executed (e.g. from our demo: ./ss 5 6 )
  3. Question: How does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)?
    • CPU executes C program by looping through the fetch-execute cycle

Next Lecture