5 ms·
Why involve C at all? This is much cleaner in assembly .global _start .data what: .string "hello\n" .set len, .-wh
by oguz-ismail 2y ago
Why involve C at all? This is much cleaner in assembly
.global _start
.data
what: .string "hello\n"
.set len, .-what - 1
.text
_start:
mov w0, 1
adr x1, what
mov w2, len
mov w8, 4
99: svc 0
dsb nsh
isb
mov w8, 1
98: svc 0
.section .openbsd.syscalls, ""
.long 99b, 4
.long 98b, 1
.section .note.openbsd.ident, "a"
.long 8, 4, 1
.string "OpenBSD"
.long 0
- hello_computer 2y agoespecially when confronted with a soup of C flags and declarations, and still not being entirely sure what the C compiler is going to emit
- kccqzy 2y agoHow is that an issue? You inspect the generated assembler. Godbolt is the industry standard tool for that, but of course just use objdump on the command line if a browser UI isn't your fancy. Mixing C is helpful because most code doesn't need to be written in raw assembly.
- deleted 2y ago[deleted]
- hello_computer 2y agokeeping assembly in asm files, c in c files, compiled by their respective compilers, then linked, has fewer footguns than inline asm
- kccqzy 2y agoYeah but then you have to maintain function interfaces between them in order to link them. The case in this article is for inserting one single asm instruction in an otherwise C codebase.
- hello_computer 2y agoplatform ABI is usually better and more clearly defined than whatever GCC & Clang feel like doing to inline ASM across versions and optimization levels
- hello_computer 2y agoand inspecting the output is obviously easy. the hard part is figuring out which flags, pragmas & directives are required to get gcc to emit what you wanted it to emit when it doesn’t.
- Joker_vD 2y agoYou also need to re-inspect it at every toolchain change/upgrade. In my experience, most programmers don't, and then we end up spending two day chasing some impossible-to-happen bug which has happened nonetheless because new Clang version had a regression in some obscure and rarely exercised part of its codegen.
- kccqzy 2y agoNo. You have a unit test to inspect it, if your other unit tests cannot cover this codegen bug. Asking programmers to re-inspect by hand is like asking programmers to run all tests manually after each commit. Of course most programmers don't.
- pjmlp 2y agoProbably due to the audience. I can grasp the example, but back in my days, if we wanting something faster than interpreted BASIC, Assembly was the way. Folks nowadays start with Python and JavaScript.
- johnisgood 2y agoIsn't "dsb nsh" or "isb" redundant for simple syscalls like write and exit?
- oguz-ismail 2y agoWithout them it'll segfault on QEMU, IIRC OpenBSD libc uses them invariably as well. I don't know how it fares on real hardware
- johnisgood 2y agoYeah, probably (although I am not sure) because QEMU doesn't fully emulate hardware behavior, but on real hardware some CPUs may internally handle these state transitions better. I suppose its inclusion is recommended to ensure correctness across all CPUs. It may happen because without "dsb nsh" (Data Synchronization Barrier) and "isb" (Instruction Synchronization Barrier), QEMU may continue execution before the syscall fully completes, which causes a segfault or UB, but I am not entirely sure. "dsb nsh" and "isb" ensures correct execution order which prevents speculative execution issues.
- pm215 2y agoThat doesn't sound right -- QEMU always works one instruction at a time, so we never start executing a following insn until the previous one is completed.
- oguz-ismail 2y agoYeah the segfault doesn't happen on other OSes, I believe it's an OpenBSD thing
- johnisgood 2y agoSo what causes QEMU to segfault without the synchronization? I have not tested it myself, however, so I cannot guarantee that it indeed does segfault.
- 2y ago