Skip to content

Commit a959901

Browse files
committed
Finally solved the question, why a numeric label was necessary in inline-assembly
1 parent c278cd8 commit a959901

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Chapter 09/uppertst4.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ int main()
1919
"MOV X4, %2\n"
2020
"loop: LDRB W5, [%1], #1\n"
2121
"CMP W5, #'z'\n"
22-
"BGT 2f\n"
22+
"BGT Lcont\n"
2323
"CMP W5, #'a'\n"
24-
"BLT 2f\n"
24+
"BLT Lcont\n"
2525
"SUB W5, W5, #('a'-'A')\n"
26-
"2: STRB W5, [%2], #1\n"
26+
"Lcont: STRB W5, [%2], #1\n"
2727
"CMP W5, #0\n"
2828
"B.NE loop\n"
2929
"SUB %0, %2, X4\n"

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,14 @@ No change was required.
255255
Instead of a shared `.so` ELF library, a dynamic Mach-O libary is created. Further information can be found here: [Creating Dynamic Libraries](https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html)
256256

257257
### Listing 9-8
258-
The size of one variable had to be changed from int to long to make the assembler happy.
258+
In inline-assembly, which we are using here, The `cont` label must be declared as a local label by prefixing it with `L`. While this was not necessary in pure assembly, like in Chapter 5, the llvm C-Frontend will automatically add the directive [`.subsections_via_symbols`](https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html#//apple_ref/doc/uid/TP30000823-SW13) to the code:
259259

260-
More importantly, I had to change the `loop` label to a numeric label, and branch to it with the `f` — forward — option. If anyone has an idea how a non-numeric label can be used here, that would be apprecated.
260+
> Funny Darwin hack: This flag tells the linker that no global symbols contain code that falls through to other global symbols (e.g. the obvious implementation of multiple entry points). If this doesn't occur, the linker can safely perform dead code stripping. Since LLVM never generates code that does this, it is always safe to set.
261+
(From [llvm source code](https://github.com/llvm/llvm-project/blob/89b57061f7b769e9ea9bf6ed686e284f3e55affe/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp#L568))
262+
263+
While we are using the LLVM toolchain, in assembly — including inline-assembly — all safety checks are off so we must take extra precautions and specifically declare the forward label local.
264+
265+
Also, the size of one variable had to be changed from int to long to make the compiler complete happy and remove all warnings
261266

262267
### Listing 9-9
263268

0 commit comments

Comments
 (0)