flat assembler
Message board for the users of flat assembler.

Index > Main > Embedding Assembly into C

Author
Thread Post new topic Reply to topic
UniverseIsASimulation



Joined: 23 Sep 2016
Posts: 26
UniverseIsASimulation
Hey, guys!
So, I've studied a bit how you can embed Assembly into C, and I've posted an example of how to do that on my website. Do you think what I wrote there is clear and accurate? How can I improve that example?
Post 25 Aug 2019, 15:03
View user's profile Send private message Reply with quote
st



Joined: 12 Jul 2019
Posts: 35
Location: Russia
st
Thanks, I did not inline asm in GCC in the past, so find out some new things in your example (had to copy-paste its text into an editor with syntax highlighting).

However I am sure, this simplification:
Code:
/*Declare variables used in Assembly as global, so that you don't
  have to deal with both the FPU stack and the system stack
  (of the "esp" or "rsp" registers).
*/    

results in:
Code:
#else /*For Windows, you need to write [_result] instead of [result],
and similarly for other variable names (it has to do with the way linker makes EXE files).*/    

Therefore all this code is written twice.

I believe GCC features something to access stack variables from asm (and may be to reserve some registers). Hope the Universe will give us a hint. Smile
Post 26 Aug 2019, 07:10
View user's profile Send private message Visit poster's website Reply with quote
UniverseIsASimulation



Joined: 23 Sep 2016
Posts: 26
UniverseIsASimulation
Quote:
I believe GCC features something to access stack variables from asm

Yes, but that, as far as I know, only works if you use AT&T syntax.
Post 27 Aug 2019, 06:57
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1433
Furs
IMO you should either write the entire function in asm, or use inline asm with operands. This is very powerful, because you tell the compiler what are the inputs, outputs, clobbers, jump targets, etc.

When it knows such information, it can produce much better code, and you don't have to deal with global variables. You can specify an operand to reside either in memory or register or whatever else, and GCC will choose based on surrounding code what it thinks it's best, etc.

Raw inline asm by contrast produces sub-optimal code surrounding your asm.

Only thing is that it takes a while to learn it properly. But I love it. Smile
Post 27 Aug 2019, 15:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.