2019-07-15 12:21:50 +00:00
|
|
|
[< back to index](../doc_index.md)
|
2018-04-02 22:21:26 +00:00
|
|
|
|
2018-01-04 00:15:04 +00:00
|
|
|
# Interfacing with external code
|
|
|
|
|
2018-01-18 21:35:25 +00:00
|
|
|
## Calling external functions at a static address
|
|
|
|
|
|
|
|
To call an external function, you need to declare it as `asm extern`. For example:
|
|
|
|
|
|
|
|
```
|
2020-03-30 17:23:48 +00:00
|
|
|
asm void putchar(byte register(a) char) @$FFD2 extern
|
2018-01-18 21:35:25 +00:00
|
|
|
```
|
|
|
|
|
2020-03-30 17:23:48 +00:00
|
|
|
In this example, the function parameter will be passed via the accumulator,
|
2018-01-18 21:35:25 +00:00
|
|
|
the function itself is located in ROM at $FFD2. A call like this:
|
|
|
|
|
|
|
|
```
|
|
|
|
putchar(13)
|
|
|
|
```
|
|
|
|
|
2018-07-12 16:30:35 +00:00
|
|
|
will be compiled to something like this on 6502:
|
2018-01-18 21:35:25 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
LDA #13
|
|
|
|
JSR $FFD2
|
|
|
|
```
|
|
|
|
|
2018-07-12 16:30:35 +00:00
|
|
|
For more details about how to pass parameters to `asm` functions, see:
|
|
|
|
|
|
|
|
* for 6502: [Using 6502 assembly within Millfork programs#Assembly functions](./assembly.md#assembly-functions).
|
|
|
|
|
2018-07-27 17:07:12 +00:00
|
|
|
* for Z80: [Using Z80 assembly within Millfork programs#Assembly functions](./assemblyz80.md#assembly-functions).
|
2018-01-18 21:35:25 +00:00
|
|
|
|
|
|
|
## Calling external functions at a dynamic address
|
|
|
|
|
|
|
|
To call a function that has its address calculated dynamically,
|
2018-07-12 16:30:35 +00:00
|
|
|
you just need to do the same as what you would do in assembly; 6502 example:
|
2018-01-18 21:35:25 +00:00
|
|
|
|
|
|
|
```
|
2020-03-30 17:23:48 +00:00
|
|
|
asm void call_function(byte register(a) param) {
|
2018-01-18 21:35:25 +00:00
|
|
|
JMP (function_address)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
where `function_address` is a variable that contains the address of the function to call.
|