summaryrefslogtreecommitdiff
path: root/notes.txt
blob: 5d8bbe06a6a0d3abf03c17256256f14390f25473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Build a table for a (x+1)-bit number and their signed values in binary/decimal
var a = [], x=4;
var zpad = (new Array(x+1)).join("0"), pad = (new Array(x+1)).join(" ");
for(var i=0;i<2<<x;i++) {
    var n = i & ((2<<(x-1))-1);
    n-=i&(2<<(x-1))
    a.push((zpad+i.toString(2)).substr(-(x+1)) + " = " + (pad+n).substr(-(x+1)))
}
a.join("\n")

Function call try to follow the convention at
http://www.cs.virginia.edu/~evans/cs216/guides/x86.html#calling

Reminder
The stack starts at the end of the memory. If the stack grows, SP decreases.

Caller:
1. <no regs to push>
2. For n parameters, do a PUSH. The last parameter is the first on the stack
3. Call subroutine (BRS: PUSH return address on stack and BRanch Always)
(after call when returned:)
4. Remove parameters from the stack: ADD SP n
5. Result is in R0
6. (perhaps a CMP R0 0 or LOAD R0 R0 if in expression context?)

Callee:
1. push R5 (base pointer "BP" in stack for local variables and parameters)
2. Store current stack pointer in R5 (LOAD R5 SP). [R5] now contains the return
   address, [R5+i+1] the i-th parameter and [R5+-i] the i-th local var. Param i
   is at R5 + i + 1 because BP was pushed too
3. Grow stack for local variables (by SUB SP n) if necessary
4. <function body>
5. LOAD SP R5 (clear local vars)
5. PULL R5 (restore the "BP" of the caller)
6. RTS (go back to caller)

parameter access (i-th parameter):
LOAD R0 [R5+i+1]
local variable access (i-th local var)
LOAD R0 [R5+-i]
global variable access
LOAD R0 [GB+name]


Initially, the stack size on the PP2 is 240 words, but it can be adjusted with:
@STACKSIZE newsize
The constant 'stacksize' (lowercase) contains the previous stack size