Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #127425

Re: Compile Assembly and C files together

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Compile Assembly and C files together
Date 2018-03-07 15:47 +0100
Organization A noiseless patient Spider
Message-ID <p7ou2q$lt7$1@dont-email.me> (permalink)
References (3 earlier) <20180306145910.01512e6b@Mars> <p7mcge$oci$1@dont-email.me> <09640d63-f1b3-43c5-970d-4ac66db41cf3@googlegroups.com> <p7ofcq$gru$1@dont-email.me> <NxRnC.20237$5c2.13407@fx09.am4>

Show all headers | View raw


On 07/03/18 14:11, bartc wrote:
> On 07/03/2018 10:37, David Brown wrote:
>> On 06/03/18 16:57, supercat@casperkitty.com wrote:
> 
>>>      long multiply_accumulate(void *p1, void *p2, int n)
>>>      {
>>>        pp1 = p1;
>>>        pp2 = p2;
>>>        nn = n;
>>>        return nn;
>>>      }
>>>
>>> and then editing in the actual machine instructions one needs, along
>>> with
>>> a human-readable prologue etc. is apt to be much faster than reading
>>> through
>>> all of the documentation to find out everything the development
>>> system will
>>> need.
> 
>> The key operation is perhaps this function:
>>
>> long int mac(long int a, int b, int c) {
>>     return a + (long int) b * c;
>> }
>>
>> Maybe your real types will be different, but these will do for an
>> example.
>>
>> On x86-64, this will be done with code like this:
>>
>> mac:
>>          movsx   rax, edx
>>          movsx   rsi, esi
>>          imul    rsi, rax
>>          lea     rax, [rsi+rdi]
>>          ret
>>
>> The generated assembly for your example function gives (with gcc -O2,
>> 64-bit Linux target using godbolt.org) :
>>
>> multiply_accumulate:
>>          mov     DWORD PTR nn[rip], edx
>>          movsx   rax, DWORD PTR nn[rip]
>>          mov     QWORD PTR pp1[rip], rdi
>>          mov     QWORD PTR pp2[rip], rsi
>>          ret
>>
>> How exactly does that give you /any/ sort of hint as to how to write the
>> assembly code for a "mac" function like the one I gave?
> 
> You're right insofar as gcc's assembler syntax, even 'intel' style and
> written in pure assembly within .s file, isn't the clearest.

You are missing the point.  This does not give you the information you
need to be able to write assembly code that fits in with
compiler-generated code.  Your two choices are to let the compiler do it
for you (that's my favourite), or learn the ABI used by your target (and
your compiler, for x86 Windows where there is no fixed ABI) and do it
manually.  Generating some code by a compiler and guessing is not helpful.

gcc's assembly generation is fine - it's x86 code that is a bit of a
mess, regardless of the format.  Intel or AT&T versions are a matter of
taste (or, apparently, pure dogma for some people).  No assembly code is
particularly clear or simple, but that is the nature of the game.

> 
>> long int mac2(long int a, int b, int c) {
>>      asm (
>>          "mac %[a], %[b], %[c]"
>>          : [a] "+r" (a)
>>          : [b] "r" (b), [c] "r" (c)
>>          :
>>      );
>>      return a;
>> }
>>
>> That is /one/ assembly instruction.
> 
> Hmmm.. it might /generate/ one assembler instruction, but to get that
> you have write three lines of gobbledygook in a language that looks
> nothing like any assembler I've ever seen.

What you call "gobbledygook", I call "inline assembly syntax" - as shown
reasonably clearly in the gcc manual and countless web sites.  But it is
always so much more fun for you to label something as "gobbledygook",
"rubbish", "nonsense", "incomprehensible" rather than trying to rub two
brain cells together and /learn/ something.

And once you have understood the basics of how operands and constraints
are given in gcc inline assembly, you can use the same principles for
any set of assembly code, or any target processor.  Most users have to
look up the details as few people write inline assembly very often, but
it is certainly easier this way.  And it can also be far more efficient
in use, because the compiler understands how your assembly code
interacts with the rest of the C code.

> 
> It also seems to be the opposite of how assembly code is usually
> generated, where you write a few lines of high level sources to result
> in multiple lines of assembly.
> 
>> Note carefully that there are only /two/ assembly lines here.  The
>> /compiler/ takes care of extending the 32-bit parameters to 64-bit.
> 
> Then you're not really writing assembly any more, so why keep up the
> pretence?

Think why you would want to write assembly code in the first place.
Maybe you are doing it for fun, or because you think it is macho, or
because you are under the mistaken impression that you can generate
assembly better than the compiler.  But for serious use, people use
assembly if and only if the compiler cannot do the job when you write in
C.  That can be because your cpu has instructions that the compiler does
not know about and which can significantly accelerate an operation, or
because you are dealing with something that does not fit a C concept
(like controlling internal registers, fiddling with interrupts, etc.).
No sane C developer will choose to write out the instructions for
loading data into registers, doing memory calculations, or anything else
that the compiler can handle /better/.  You write the assembly
instructions that you have to write - not more.

> 
> Here's how it might look when you use something that can still be called
> inline assembly [host language is not C]:
> 
> function mult_accum(int64 a, int b,c)int64=
>     assem
>         movsx rax,dword [b]
>         movsx rsi,dword [c]
>     imul2 rsi,rax
>     mov   rdi,[a]
>     lea   rax,[rsi+rdi]
>     end
> end
> 
> (Here, the compiler for the hosting language recognises parameter
> variable names, and can transform the address modes to suit. So that [b]
> becomes [rbp+b], where b is some offset. But it could equally deal with
> the case where b is passed in register r8d. Other than that, the
> compiler doesn't concern itself with what goes on.

gcc does that too.  In my example, I specified that the data should be
in registers - then I don't have to faff around picking my own registers
and either saving them or hoping that the compiler isn't using them for
other things.  If the assembly instructions can handle memory addresses
directly, then you specify that the compiler can use whichever is most
efficient for the surrounding code.  Use "rm" rather than "r" to say
"Use a register, or use a memory address".  There are many other options
for immediate data, floating point, specific registers, etc.  Some apply
to all target cpus, others are specific to the cpu.

Basically, your version automatically generates input operands like:

	[b] "rm" (b), [c] "rm" (c)
	
That is a convenient shortcut that works well in many cases.  But it is
a limitation in other cases, and your output operand seems to be limited
to "rax".  You have chosen to be limited, implicit, and easy - gcc has
chosen to be flexible, explicit and require a little more understanding.


> 
> This is the point: the assembly code you write has to call the shots,
> not the compiler for the host language.
> 
> I've only briefly tested this, getting a result of 7700 when a, b and c
> are 1000, 67 and 100, but it shows this is a real, working syntax.)
> 
>> Learning gcc inline assembly
> 
> They should really stop calling it that. There is already some confusion
> because what you have in a .s file is different from the inline syntax.
> 

Why should "they" stop talking about inline assembly?  Compilers of all
sorts (not just C, and not just gcc) have used the term "inline
assembly" to mean "putting a bit of assembly code inside your high-level
code" for decades.  What you have in a ".s" file is /not/ inline
assembly - it is an assembler file, and it is just "assembly code".
That may be assembly code generated by the compiler, or assembly code
written by a human, but it is not "inline assembly" and never has been.

Hopefully that will clear up your confusion here.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Compile Assembly and C files together Ilias Abrams <iliasabrams@gmail.com> - 2018-03-05 08:23 -0800
  Re: Compile Assembly and C files together "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2018-03-06 19:20 +0800
    Re: Compile Assembly and C files together druck <news@druck.org.uk> - 2018-03-06 13:53 +0000
      Re: Compile Assembly and C files together Rob Morley <nospam@ntlworld.com> - 2018-03-06 14:59 +0000
        Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-06 16:35 +0100
          Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-06 07:57 -0800
            Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-06 16:35 +0000
              Re: Compile Assembly and C files together Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-03-06 18:15 +0000
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-06 18:51 +0000
                Re: Compile Assembly and C files together Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-03-06 19:22 +0000
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-06 19:30 +0000
                Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-06 12:31 -0800
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-06 23:05 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-07 12:17 +0100
                Re: Compile Assembly and C files together Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-03-07 16:05 +0000
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-07 16:45 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-07 17:54 +0100
                Re: Compile Assembly and C files together Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-03-07 16:59 +0000
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-07 17:13 +0000
              Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-07 12:26 +0100
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-07 14:25 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-07 15:57 +0100
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-07 15:24 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-07 17:56 +0100
            Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-07 11:37 +0100
              Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-07 13:11 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-07 15:47 +0100
                Re: Compile Assembly and C files together Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-03-07 07:31 -0800
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-07 21:00 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-08 12:33 +0100
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-08 13:05 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-08 15:14 +0100
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-08 15:23 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-08 23:22 +0100
                Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-08 14:40 -0800
                Re: Compile Assembly and C files together Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-03-08 23:59 +0000
                Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-08 16:54 -0800
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-09 10:37 +0100
                Re: Compile Assembly and C files together Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-03-09 10:47 +0000
                Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-09 09:19 -0800
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-09 10:23 +0100
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-09 12:28 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-09 16:43 +0100
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-09 17:14 +0000
                Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-09 09:32 -0800
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-08 15:26 +0000
                Re: Compile Assembly and C files together Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-03-08 19:23 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-08 23:27 +0100
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-08 16:17 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-08 23:47 +0100
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-09 11:59 +0000
              Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-07 09:13 -0800
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-07 17:42 +0000
                Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-07 10:55 -0800
      Re: Compile Assembly and C files together "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2018-03-06 23:01 +0800
    Re: Compile Assembly and C files together ray carter <ray@zianet.com> - 2018-03-06 15:44 +0000
      Re: Compile Assembly and C files together "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2018-03-07 00:07 +0800
        Re: Compile Assembly and C files together Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2018-03-07 22:39 +0200
          Re: Compile Assembly and C files together "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2018-03-08 18:18 +0800
            Re: Compile Assembly and C files together Martin Gregorie <martin@mydomain.invalid> - 2018-03-08 12:37 +0000
              Re: Compile Assembly and C files together Martin Gregorie <martin@mydomain.invalid> - 2018-03-08 13:13 +0000
                Re: Compile Assembly and C files together The Natural Philosopher <tnp@invalid.invalid> - 2018-03-08 13:34 +0000
                Re: Compile Assembly and C files together Martin Gregorie <martin@mydomain.invalid> - 2018-03-08 15:27 +0000
                Re: Compile Assembly and C files together mm0fmf <none@invalid.com> - 2018-03-08 17:33 +0000
                Re: Compile Assembly and C files together The Natural Philosopher <tnp@invalid.invalid> - 2018-03-09 09:06 +0000
                Re: Compile Assembly and C files together Richard Kettlewell <invalid@invalid.invalid> - 2018-03-09 09:17 +0000
                Re: Compile Assembly and C files together The Natural Philosopher <tnp@invalid.invalid> - 2018-03-09 09:30 +0000
                Re: Compile Assembly and C files together Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2018-03-09 15:56 +0200
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-09 14:13 +0000
                Re: Compile Assembly and C files together Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2018-03-09 11:14 -0500
                Re: Compile Assembly and C files together The Natural Philosopher <tnp@invalid.invalid> - 2018-03-09 17:44 +0000
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-09 17:16 +0100
                Re: Compile Assembly and C files together Robert Wessel <robertwessel2@yahoo.com> - 2018-03-09 11:19 -0600
                Re: Compile Assembly and C files together bartc <bc@freeuk.com> - 2018-03-09 18:11 +0000
                Re: Compile Assembly and C files together Robert Wessel <robertwessel2@yahoo.com> - 2018-03-09 13:41 -0600
                Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-09 10:12 -0800
                Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-09 19:20 +0100
                Re: Compile Assembly and C files together supercat@casperkitty.com - 2018-03-09 11:45 -0800
                Re: Compile Assembly and C files together Robert Wessel <robertwessel2@yahoo.com> - 2018-03-09 13:48 -0600
                Re: Compile Assembly and C files together Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2018-03-09 13:16 -0500
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-09 18:29 +0000
                Re: Compile Assembly and C files together Jack Fearnley <jack@alcor.concordia.ca> - 2018-03-09 19:07 +0000
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-09 19:13 +0000
                Re: Compile Assembly and C files together Michael J. Mahon <mjmahon@aol.com> - 2018-03-10 13:16 -0600
                Re: Compile Assembly and C files together Martin Gregorie <martin@mydomain.invalid> - 2018-03-11 19:39 +0000
                Re: Compile Assembly and C files together Michael J. Mahon <mjmahon@aol.com> - 2018-03-12 01:16 -0500
                Re: Compile Assembly and C files together Martin Gregorie <martin@mydomain.invalid> - 2018-03-12 20:49 +0000
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-12 16:11 +0000
                Re: Compile Assembly and C files together Michael J. Mahon <mjmahon@aol.com> - 2018-03-12 14:58 -0500
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-12 20:38 +0000
                Re: Compile Assembly and C files together scott@slp53.sl.home (Scott Lurndal) - 2018-03-12 16:06 +0000
                Re: Compile Assembly and C files together Michael J. Mahon <mjmahon@aol.com> - 2018-03-12 14:58 -0500
                Re: Compile Assembly and C files together Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2018-03-09 21:27 +0200
                Re: Compile Assembly and C files together The Natural Philosopher <tnp@invalid.invalid> - 2018-03-09 17:40 +0000
                Re: Compile Assembly and C files together Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2018-03-09 21:30 +0200
                Re: Compile Assembly and C files together Richard Damon <Richard@Damon-Family.org> - 2018-03-09 13:16 -0500
                Re: Compile Assembly and C files together Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2018-03-09 21:33 +0200
                Re: Compile Assembly and C files together mm0fmf <none@invalid.com> - 2018-03-09 17:40 +0000
                Re: Compile Assembly and C files together The Natural Philosopher <tnp@invalid.invalid> - 2018-03-09 17:47 +0000
                Re: Compile Assembly and C files together mm0fmf <none@invalid.com> - 2018-03-09 18:05 +0000
                Re: Compile Assembly and C files together The Natural Philosopher <tnp@invalid.invalid> - 2018-03-09 09:05 +0000
                Re: Compile Assembly and C files together Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2018-03-09 16:01 +0200
            Re: Compile Assembly and C files together Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2018-03-08 17:25 +0200
    Re: Compile Assembly and C files together The Natural Philosopher <tnp@invalid.invalid> - 2018-03-07 07:36 +0000
  Re: Compile Assembly and C files together Richard Kettlewell <invalid@invalid.invalid> - 2018-03-06 15:29 +0000
    Re: Compile Assembly and C files together David Brown <david.brown@hesbynett.no> - 2018-03-06 16:38 +0100

csiph-web