Implementation Details
5-26
5.6.2 Division
Integer division currently requires the use of several accumulator pointers. We
divide a 16 bit integer located in A0 by a 16 bit integer located in A0~. We return
the quotient in A0~, and the remainder in A0. We make use of A3~ and A3 for
scratch pads. We also set flag 1 if a division by zero is attempted, and zero out
the quotient and the remainder in this case. We also use PH for temporary
storage of the divisor.
5.6.3 Function Calls
Every function is associated with a stack frame. A regular C program is initially
given control by a call to main(). A C– – program starts with a jump to the
_main symbol, which must therefore be present in the C– – source code.
The stack frame has the following structure:
First Argument Low Address
• • • ⇓
Last Argument
⇓
Return Address ⇓
BP Previous BP ⇓
Locals ⇓
SP High Address
BP is the frame pointer (base pointer), SP the stack pointer.
We use R7 for stack pointer, and yet another register for BP, REG_BP (R5,
because of its special arithmetic capabilities). Before a function is called, the
arguments are pushed on the stack, first argument first. The function call
automatically pushes the return address on the stack. Immediately upon
entering the function body, the current BP is pushed on the stack to preserve
it, so that the stack pointer now points to the next location. This location is
copied to REG_BP, which becomes our fixed reference point for the current
function. Locals are then allocated on the stack from this starting location.
When the function returns, SP is made to point to the return address, after the
previous BP is popped. The return is performed by a RET instruction. The
calling routine is then responsible for moving the stack pointer to its previous
location, before the arguments were put on the stack. Because all functions
return via A0, the only function return type allowed is integer. Our
implementation of C– – allows for function prototyping, and checks that
prototype functions are called with the correct number of arguments. Function