Skip to content

Commit 8529a6f

Browse files
committed
docs: add stdcall.md page
1 parent ab73949 commit 8529a6f

File tree

2 files changed

+79
-24
lines changed

2 files changed

+79
-24
lines changed

docs/fastcall.md

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,50 @@
11
# FASTCALL
22

3-
Fastcall is used to indicate that an assembly function should be jumped into with registers already set.
3+
`FastCall` is used to indicate that a [`SUB`](sub.md) or [`FUNCTION`](function.md) will follow the _FastCall_ Calling
4+
Convention. In this convention, the 1<sup>st</sup> parameter of the function will come in the registers as follows:
45

5-
* If the function takes a Byte (or uByte) parameter, then the A register will be set with this parameter.
6-
* If it takes an Integer (or uInteger) parameter, then the HL register will be set with the value of that parameter on entry to the function.
7-
* If it takes a Long (or uLong), or fixed parameter, then the DE and HL registers will hold the 32 bit value of the parameter.
8-
* If it takes a float type parameter, then the registers C, DE and HL will hold the five bytes for that value.
6+
* If the function takes a `Byte` (or `UByte`) parameter, then the `A` register will be set with this parameter.
7+
* If it takes an `Integer` (or `UInteger`) parameter, then the `HL` register will be set with the value of that parameter
8+
on entry to the function.
9+
* If it takes a `Long` (or `ULong`), or fixed parameter, then the `DE` and `HL` registers will
10+
hold the 32bit value of the parameter, where `HL` holds the lower 16 bits and `DE` the higher one.
11+
* If it takes a `Float` type parameter, then the registers C, DE and HL will hold the five bytes for that value.
12+
Here, `C` is the exponent (excess 127), and `DEHL` the mantissa, being `DE` the highest 16 bits and `HL` the lower ones.
913

1014
Return is automatic based on the function return type in the same way:
11-
* 8 bit returns should be in the A register
12-
* 16 bit returns should be in HL
13-
* 32 bit returns should be in DEHL
14-
* 40 bit FLOAT returns should be in CDEHL.
15+
* 8bit returns should be in the A register
16+
* 16bit returns should be in HL
17+
* 32bit returns should be in DEHL
18+
* 40bit FLOAT returns should be in CDEHL.
1519

16-
Fastcall should ONLY be used with functions that take a single parameter. If you use more than one parameter, you'll have to deal with the stack (SP register) and restore it to the previous it had before your function was called.
20+
Strings are a 16bit `UInteger` pointer, hence the `HL` register will be used (both for parameters and return values)
1721

18-
**Example:**
22+
`FastCall` should ONLY be used with functions that take a single parameter. If you use more than one parameter, the remaining
23+
parameters will come in the stack. Upon return, you will have to deal with the stack (SP register) and restore it to the
24+
state it had prior your function was called.
1925

20-
```
26+
When entering the function, the stack will be as follows:
27+
28+
* [SP + 00]: Return address. Top of the stack (16 bits)
29+
* [SP + 02]: 2<sup>nd</sup> parameter (if any)
30+
31+
32+
## Example
33+
34+
```vbnet
2135
FUNCTION FASTCALL whatLetter (A as uByte) as uByte
22-
Asm
23-
PROC
24-
LOCAL DATA, START
25-
JP START
26-
DATA: DEFB "A Man, A Plan, A Canal, Panama"
27-
START: LD HL,DATA
28-
LD D,0
29-
LD E,A
30-
ADD HL,DE
31-
LD A,(HL)
32-
ENDP
33-
End Asm
36+
Asm
37+
PROC
38+
LOCAL DATA, START
39+
JP START
40+
DATA: DEFB "A Man, A Plan, A Canal, Panama"
41+
START: LD HL,DATA
42+
LD D,0
43+
LD E,A
44+
ADD HL,DE
45+
LD A,(HL)
46+
ENDP
47+
End Asm
3448
END FUNCTION
3549
```
3650

@@ -40,3 +54,7 @@ The above function, when called with `whatLetter(<value>)` will return the `<val
4054
### Notes
4155
* Note that the A register already contains `<value>` when the inline assembly is reached.
4256
* Note that we do NOT need to put a ret opcode on the end of the assembly. The compiler will do that for us.
57+
58+
## See Also
59+
60+
* [STDCALL](stdcall.md)

docs/stdcall.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# STDCALL
2+
3+
`StdCall` is used to indicate that a `FUNCTION` or `SUB` will receive all its parameters in the stack, following the
4+
_Standard Calling Convention_.
5+
6+
The standard calling convention pushes all the function's parameters into the stack in _reverse order_. For 8bit values,
7+
since Z80 stack register SP register is word-aligned, the parameter is pushed as a 16 bit value, in the high 8 bits
8+
(the Z80 ASM instruction `push af` pushes `A` register in the higher part, and `F` register -flags- in the lower).
9+
10+
Return value for functions is placed in registers, based on the function return type:
11+
* 8 bit returns should be in the A register
12+
* 16 bit returns should be in HL <br/>
13+
Also for `String` values, the memory address of the string.
14+
* 32 bit returns should be in DEHL
15+
* 40 bit FLOAT returns should be in CDEHL.
16+
17+
`STDCALL` is used by default in any function if no calling convention is used.
18+
19+
The stack is automatically cleaned upon function termination.
20+
21+
## Example
22+
23+
```vbnet
24+
REM These two declarations are equivalent
25+
26+
SUB STDCALL Hello
27+
PRINT "HELLO WORLD!"
28+
END SUB
29+
30+
SUB Hello
31+
PRINT "HELLO WORLD!"
32+
END SUB
33+
```
34+
35+
## See Also
36+
37+
* [FASTCALL](fastcall.md)

0 commit comments

Comments
 (0)