ECEC 201 · Week 6

Modern Computer: Von Neumann

A C file becomes a running program in steps: the preprocessor, the compiler, the assembler, the linker, and the loader.

Week 6 of 10 · Das Frite

ECEC 201 · Week 6 · In class

A C file becomes a running program in steps: the preprocessor, the compiler, the assembler, the linker, and the loader.

ECEC 201 · Week 6 · In class

In 64-bit Arm assembly, a function's first arguments arrive in x0 and x1 and its result leaves in x0.

ECEC 201 · Week 6 · Lecture

What each step does

StepDoes
PreprocessorPastes in #include files, expands macros, drops comments
CompilerTurns C into assembly for this processor
AssemblerTurns assembly into machine code with unresolved names
LinkerJoins object files and libraries, gives every name an address
LoaderThe kernel maps the program into a new process and starts it

ECEC 201 · Week 6 · Lecture

A link error names a symbol

$ gcc main.o -o prog
main.c:(.text+0x10): undefined reference to `scale'
$ nm main.o util.o
main.o:
                 U scale
0000000000000000 T main
util.o:
0000000000000000 T scale

ECEC 201 · Week 6 · Lecture

Arguments in registers

long add(long a, long b)
{
	return a + b;
}

add:                    // gcc -O2 -S, 64-bit Arm
	add	x0, x0, x1      // a in x0, b in x1, result to x0
	ret                 // return to the address in x30

ECEC 201 · Week 6 · Lecture

Stack frames in gdb

ECEC 201 · Week 6 · Lab

Lab

  1. Build a two-file program step by step with `gcc -E`, `-S`, `-c`, and a link, and fix one undefined-symbol error using `nm`.
  2. In the assembly of one two-argument function, mark where the arguments arrive in `x0` and `x1` and where the result leaves in `x0`. In `gdb`, stop inside a recursive function and print the backtrace. Second pass: recurse three levels deeper and compare the stack pointer between frames.

ECEC 201 · Week 6 · Lab

Lab (continued)

  1. Assemble the lab hand-in: The annotated assembly of the function, the `nm` line that fixed the link error, and the backtrace with the stack-pointer difference per frame.

ECEC 201 · Week 6 · Lab

Hand in

The annotated assembly of the function, the `nm` line that fixed the link error, and the backtrace with the stack-pointer difference per frame.

ECEC 201 · Week 6 · Homework

Homework

ECEC 201 · Week 6 · Homework

Turn in

The four steps and their output files.

ECEC 201 · Week 6 · Read

Read

On the lab site: Module 8, Linux boots, shows the kernel starting programs. This week grades one program's build and its stack.

ECEC 201 · Week 6 · Next

Week 7

Linux shows the system as files.

Arrow keys or space to move, F for full screen, N for speaker notes, Home and End for the first and last slide. Print gives one slide per page.