You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/static/asm.rst
+49-45Lines changed: 49 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,13 @@ Assembly
6
6
Assembly routines can be linked into a C/C++ program by putting them into the same **src** directory that your C/C++ sources are in, but with an **.asm** extension.
7
7
These can be placed in any subdirectory of **src** just like C/C++ sources.
8
8
9
+
Assembly Files
10
+
--------------
11
+
9
12
.. _asm-consts:
10
13
11
14
Constants
12
-
---------
15
+
^^^^^^^^^
13
16
14
17
The top of the file is a good place defining constants or including other files that define constants.
15
18
These will be availabe throughout the rest of the file, but not in other files.
@@ -22,42 +25,42 @@ See :ref:`asm-section` to fix this problem.
22
25
.. _asm-assume:
23
26
24
27
Assume
25
-
------
28
+
^^^^^^
26
29
27
30
You should add a :code:`assume adl=1` before trying to emit any code, which ensures that you get 24-bit eZ80 instructions.
28
-
If you end a assembly file in:code:`assume adl=0` which is the eZ80's 16-bit Z80 compatibility mode, it will propogate to another random assembly file.
29
-
All toolchain and compiler-generated sources make sure to reset the mode at the top of the file and end the file in the same mode, but if one of your sources end in Z80 mode, then any other one of your sources might begin in Z80 mode, so it is safer to put it in every file.
31
+
If you end an assembly file with:code:`assume adl=0` (which is the eZ80's 16-bit Z80 compatibility mode), it will propogate to another random assembly file.
32
+
All toolchain and compiler-generated sources make sure to reset the mode at the top of the file and end the file in the same mode, but if one of your sources end in Z80 mode, then any other one of your sources might begin in Z80 mode, so it is safer to put the :code:`assume` line in every file.
30
33
31
34
.. _asm-section:
32
35
33
36
Section
34
-
-------
37
+
^^^^^^^
35
38
36
39
Now that we are in the correct mode, we need to tell the linker where to put things.
37
40
We use :code:`section .text` for code, :code:`section .data` for variables, and :code:`section .rodata` for constant data.
38
41
Currently these are all placed in RAM, so which section you choose to switch to is much less important than how often you switch sections, even if you are switching to the same section you are already in.
39
42
This is because every time you start a new, or restart the same, section, the linker gets a new opportunity to delete a block of dead code/data.
40
43
Because of this, the correct time to switch sections is usually every time you start a new function or variable.
41
-
You should not let execution fall off the end of a block because you won't know if that block will be included or deleted from the output, however you can if you say `require _symbol` of some public or private symbol defined in the next section to ensure that if the current block is included, then that will force the next block to also be included.
44
+
You should not let execution fall off the end of a block because you won't know if that block will be included or deleted from the output, however you can if you say :code:`require _symbol` of some public or private symbol defined in the next section to ensure that if the current block is included, then that will force the next block to also be included.
42
45
To define a symbol in a block that can be referenced from other blocks, you should do :code:`private _symbol` or :code:`public _symbol` right before its definition.
43
-
If it is private then it is only able to be referenced from the same file and no :ref:`asm-extern` should be used.
44
-
If it is public then it can be referenced within the same file without :ref:`asm-extern` just like private symbols, but public symbols can also be referenced from other files and even C/C++!
46
+
If it is private then it is only able to be referenced from the same file and no :ref:`extern <asm-extern>` should be used.
47
+
If it is public then it can be referenced within the same file without :ref:`extern <asm-extern>` just like private symbols, but public symbols can also be referenced from other files and even C/C++!
45
48
The public assembly symbol named :code:`_symbol` is accessible in C by the global name :code:`symbol`, assuming it is properly declared, with your asm symbol acting as the definition.
46
49
47
50
.. _asm-extern:
48
51
49
52
Extern
50
-
------
53
+
^^^^^^
51
54
52
55
At the end of the file is a good place to list every external symbol that you might depend on like :code:`extern _symbol`.
53
56
This includes both public symbols defined in another assembly file and global symbols from C, prefixed with an underscore like usual.
54
57
Lastly, you should not let execution fall off the end of a file because the next file that gets assembled is unpredictable and you could end up anywhere!
55
58
Block ordering can only be relied on within a single file, and only for blocks belonging to the same section.
56
59
57
-
Meanwhile, back in C
58
-
--------------------
60
+
Linking ASM routines to C/C++
61
+
-----------------------------
59
62
60
-
On the C/C++ side of things, a separate header file should define a extern C global prototype for the function if it is to be called from C/C++ code.
63
+
If an assembly function needs to be called from C, a separate header file should define a extern C global prototype.
61
64
In C this looks like a normal function or global declaration, and in C++ it's the same thing but in an :code:`extern "C" {}` block.
62
65
63
66
Below is an example C prototype followed by the assembly implementation:
@@ -108,57 +111,58 @@ Below is an example C prototype followed by the assembly implementation:
108
111
}
109
112
110
113
Arguments
111
-
---------
114
+
^^^^^^^^^
112
115
113
116
Arguments are pushed from last to first corresponding to the C prototype.
114
117
In eZ80, 3 bytes are always pushed to the stack regardless of the actual size.
115
118
However, the assembly function must be careful to only use the valid bytes that are pushed.
116
119
For example, if a *short* type is used, the upper byte of the value pushed on the stack will contain arbitrary data.
117
120
This table lists the locations relative to *sp* from within the called funciton.
121
+
Note that :code:`sp + [0,2]` contains the return address.
0 commit comments