Recently, I started learning Arm64 assembly. The assembler I'm using is the GNU Assembler (GAS). The version of GAS I'm using is GNU assembler (GNU Binutils for Ubuntu) 2.42
.
I want to make my code more modular so it's easier to read and maintain.
Specifically, I'd like to know if there's a best practice for sharing code between files? When sharing code between files, I'd like to be able to control what functions and structs are accessible.
I know two methods for sharing assembly code between files.
- The
.global
directive. This makes a function or struct visible to the entire assembly project. - The
.include
directive. This includes the entire text contents of a file into another file.
Originally, I was asking for something that works like C header files, because I thought they perform access control but that's incorrect. I would like some way to declare some symbols as private so they cannot be accessed when included. I don't know if that's even possible in assembly. Or if it's just not worth worrying about.
Right now, the best idea I can think of is putting code that needs to be shared in its own file. If that file needs any dependencies, it's included and given pointers to structs and functions it needs to access. Then other files can just include that file.
.global
directive just marks a symbol as linkable, either exporting or importing it (compareextern
in C, or the absence of astatic
qualifier). The.include
doesn't work on a linker level, but on a textual level. You don't have header files in assembly because there are no types. The closest approximation would be to include a file that contains.global
declarations, without defining those symbols. Header files in C aren't access control, they don't export functions from a translation unit.