Skip to content

Commit

Permalink
Finally solved the question, why a numeric label was necessary in inl…
Browse files Browse the repository at this point in the history
…ine-assembly
  • Loading branch information
below committed Oct 11, 2021
1 parent c278cd8 commit a959901
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Chapter 09/uppertst4.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ int main()
"MOV X4, %2\n"
"loop: LDRB W5, [%1], #1\n"
"CMP W5, #'z'\n"
"BGT 2f\n"
"BGT Lcont\n"
"CMP W5, #'a'\n"
"BLT 2f\n"
"BLT Lcont\n"
"SUB W5, W5, #('a'-'A')\n"
"2: STRB W5, [%2], #1\n"
"Lcont: STRB W5, [%2], #1\n"
"CMP W5, #0\n"
"B.NE loop\n"
"SUB %0, %2, X4\n"
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,14 @@ No change was required.
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)

### Listing 9-8
The size of one variable had to be changed from int to long to make the assembler happy.
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:

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.
> 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.
(From [llvm source code](https://github.com/llvm/llvm-project/blob/89b57061f7b769e9ea9bf6ed686e284f3e55affe/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp#L568))

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.

Also, the size of one variable had to be changed from int to long to make the compiler complete happy and remove all warnings

### Listing 9-9

Expand Down

0 comments on commit a959901

Please sign in to comment.