Introduction to Computing Systems
from bits & gates to C & beyond
Introduction to Part II Introduction to Part II
HLL Structures
Variables
Activation records
Higher Level Languages Higher Level Languages
HLLs allow symbolic naming of variables
After mastering Assemly, you can now appreciate what a luxury this is!
HLLs provide expressiveness
Compare an if statement to a conditional Branch instruction!
HLLs abstract from the underlying hardware
While each microprocessor has its own ISA, HLL code is usually portable between many different platforms
etc., etc.
Translation Translation
HLL code has to be translated into the ISA of each machine it runs on:
Interpretation:
The HLL code is simply a set of commands for a program called an interpreter, which is a virtual machine that translates the HLL code a piece at a time into Machine Language, executes it on the spot, then goes back for another piece.
Both the HLL source code and the interpreter are required every time the program is run.
Compilation
The HLL code is translated into an entire ML program (an executable image) by a program called a compiler.
Only the executable image is needed to run the program.
Variables Variables
Symbolic names for variables
The compiler creates a symbol table which holds information about each variable:
Identifier (name), data type, memory location (as an offset from a
“frame pointer”), scope, etc.
Suppose a variable “seconds” has been assigned an offset of -5 from the frame pointer, which is maintained in R5: then we can access seconds by the Base+Offset addressing mode:
LDR R0, R5, # -5
i.e. each reference to the variable seconds is translated by the compiler to (R5) - 5
We have finally found a use for the offset in LDR!
Activation Records Activation Records
A Record
A collection of contiguous memory locations treated as one entity
The struct construct in C/C++ describes a record
Activation Record
Is allocated for each function invocation in memory
The area of memory where activation records are allocated is called the stack memory or run-time stack
In LC-3, R6 is the stack pointer: it points to the top of the stack (TOS)
R5 is the frame pointer: it points to the first local variable
Each element on the stack is an activation record
Function calls Function calls
When a function call is made:
The calle saves space on the stack for the return value
this is located immediately on top of the callee’s parameters
The callee pushes a copy of the return address (in R7) onto the stack
The callee pushes a copy of the dynamic link (the callee’s frame pointer, in R5) onto the stack
The callee allocates enough space on the stack for its local variables, and adjusts R5 to point to the base of the local variables, and R6 to point to the top of the stack.
Main calls Main calls
Watt, Watt,
which calls which calls
Volta
Volta
Watt Watt
prepares prepares
to call to call
Volta
Volta
Volta is Volta is ready to ready to
run run
Returning from a call Returning from a call
Ending the callee function:
If there is a return value, it is written into the return value entry of the activation record
The local variables are popped off the stack
The dynamic link (the caller’s frame pointer) is restored
The return address is restored (R7)
The RET instruction returns control to the caller function
Returning to the caller function:
The return value is popped off the stack
The arguments are popped off the stack