Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1184

Appendix C: Notes on C


The local variablesaandbmust be used as arguments for theaddproceduretobecalled.Thecompiler
builds the parameter list by placing the appropriate values at the end of the local stack — the first param-
eter is at the bottom, as already mentioned. The stack pointer is used to find the end of the stack. The
corresponding position in memory is determined by means of pointer de-referencing. This position is
supplied with the value in theeaxregister that was previously filled for both parameters with the value
of the local variables on the stack. Lines 7 and 8 set the second parameter (b), and lines 9 and 10 are
responsible for the first parameter (a). When reading the source code, it is important not to confuse the
very similar namesespandebp.

Figure C-2 shows the status of the stack once thepreceding operations have been carried out.

4

3

Old Frame Pointer

ebp–4

ebp

esp

ebp–8

ebp–12

esp+4

int b = 4

int a = 3

int ret

Frame Pointer

Stack Pointer

Arguments

Local
Variables

0 x 18 = 24 Bytes

Figure C-2: Frame status prior to callingadd.

addcan now be invoked using thecallcommand.(Inarealprogram,anaddresswouldbegivenfor
the function instead of the<add>placeholder on completion of relocation.) The command pushes the
previous value of the instruction pointer onto the stack and resumes the code flow at the beginning of
theaddroutine.

In accordance with convention, the routine starts by pushing the previous value of the frame pointer
onto the stack and assigning the value of the stack pointer to the frame pointer. This results in the stack
situation illustrated in Figure C-3 (only the parts relevant foraddare shown).

4

3

Return Address

Old Frame Pointer

Arguments

ebp+8

esp, ebp

ebp+12

Figure C-3: Stack layout after callingadd.

The procedure parameters are found by reference to the frame pointer. The compiler knows that
they are located directly at the end of the activation record of the calling function and that two 4-byte
Free download pdf