Inline (was Buffer not filled while reading disk)

Programming, for all ages and all languages.
Post Reply
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Inline (was Buffer not filled while reading disk)

Post by PeterX »

In the other thread (" Buffer not filled while reading disk...") we were talking about operating with CPU registers in inline asm.
Octocontrabass wrote: This is handled by the constraints in the inline assembly. You can read about it in the GCC manual.
psimonson1988 wrote: Oh by the way, "a"(val), "b"(buffer), "c"((c << 8) | s), "d"((h << 8) | p->drive) is how you set ax, bx, cx, and dx in GNU inline assembly. Just in case you didn't know.
Woooah! That's both too sophisticated for me! I think I stick to Nasm and C, neatly separated...

But ok, I give it a try:
"a"(0x0201), "b"(buffer) etc. where buffer is the variable name for the buffer. Would that be correct?
Edit: I think it must be "a"($0x0201)

But I don't understand the constraints from the GCC manual. Any chnace there's a more easy to understand text about it?

Next thing to learn for me is Git. I hope I can understand at least that. I will get two books about it from the library.
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: Inline (was Buffer not filled while reading disk)

Post by Octocontrabass »

PeterX wrote:But I don't understand the constraints from the GCC manual. Any chnace there's a more easy to understand text about it?
This page on the wiki might help.

Constraints are the letters inside the quotation marks that tell the compiler where the input or output may be located. To use the quoted code as an example, the "a" and "b" are constraints. The "a" tells the compiler to use AL or AX or EAX for the operand, chosen depending on the operand size. The "b" constraint does the same thing, but for BL/BX/EBX. There are constraints for all kinds of registers, memory references, and immediate values.

The part inside the parentheses is the C expression that describes the input or output. So, for example, an input operand specified as "a"(0x0201) tells the compiler to put the value 0x201 into EAX before running the inline assembly code. (Why EAX instead of AX? In C, integer literals are of type int by default.)

GCC's inline assembly is notoriously difficult, so it's not a good idea to use it if you're not entirely certain it will work how you want.
Post Reply