3 ms·
One of the reasons for gcc's inline asm syntax being so verbose is it tells the compiler which registers are used and how. There is no indication in your last a
by asveikau 3y ago
One of the reasons for gcc's inline asm syntax being so verbose is it tells the compiler which registers are used and how. There is no indication in your last asm() that the value of rax before the asm() is read from. This means the compiler could assume it's safe to clobber rax just before. After all, you assigned a value into rax and never read it, a reasonable optimizer might say, why emit that first assignment at all?
- dundarious 3y agoI think you should read https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Input-Operands https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Input-O... and the link I gave earlier, if you think my example is something novel of my own construction. It's basically straight from the GCC docs. If your point is that I don't use result, that's because it's a snippet written into Hacker News. I didn't write the code to convert it into an errno and return -1 on error, etc., but doing so would be perfectly valid, and safe from your reasonable optimizer concerns.
- asveikau 3y agoI see absolutely no examples in that link there where they assign into a register via C code, not using it anywhere else, and then assume you can read the same value back from that register in an asm() statement without declaring it as an input.
- dundarious 3y agoI referenced both links (the latter links to the former, and the former, which was in the first comment of mine you replied to, contains the following examples: register int *p1 asm ("r0") = …; register int *p2 asm ("r1") = …; register int *result asm ("r0"); asm ("sysint" : "=r" (result) : "0" (p1), "r" (p2)); and int t1 = …; register int *p1 asm ("r0") = …; register int *p2 asm ("r1") = t1; register int *result asm ("r0"); asm ("sysint" : "=r" (result) : "0" (p1), "r" (p2)); In my example, nr is rax and listed in the input section, rfd is rdi and also listed in the input section, and result is rax and listed in the output section (I even used your preferred syntax for specifying rax here). Using result after the syscall asm statement is perfectly valid.