Recent

Author Topic: Assembly language trade-offs  (Read 4841 times)

Rick314

  • New Member
  • *
  • Posts: 28
Assembly language trade-offs
« on: May 08, 2015, 07:01:21 am »
The Reference Guide gives the following assembly language example:
Code: [Select]
asm 
  Movl $1,%ebx 
  Movl $0,%eax 
  addl %eax,%ebx 
end [’EAX’,’EBX’];
where the "[’EAX’,’EBX’]" tells the compiler EAX and EBX are used, so it can push and pop them before and after the "asm" block. I would tend to leave out "[’EAX’,’EBX’]", do the pushes and pops myself, not need to trust the compiler to do it, and prove to the reader they are available for use. Is there any advantage to using the "[’EAX’,’EBX’]" construct other than typing a little less?

The Programmers Guide says an all-assembler procedure can be written either of these ways:
Code: [Select]
procedure Foo(I:Integer);assembler;
asm
   ...
end;

procedure Foo(I:Integer);
begin
   asm
   ...
   end;
end;
Why would I pick one way over the other?

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Assembly language trade-offs
« Reply #1 on: May 08, 2015, 06:15:37 pm »
to me, the answer to both ? would be whichever form you prefer.

i would expect that the code emitted would be result in the same functionality regardless of which style was used.
Lazarus Ver 1.4.0 FPC Ver 2.6.4 SVN 48774 32-bit
Windows 7 U 64-bit

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7574
Re: Assembly language trade-offs
« Reply #2 on: May 08, 2015, 06:18:45 pm »
Well, first and for all it is best to simply not write assembler.

The difference between the two variants, is that in the first variant (assembler;) you can do your own prologue and epilogue.

Laksen

  • Hero Member
  • *****
  • Posts: 624
    • J-Software
Re: Assembly language trade-offs
« Reply #3 on: May 08, 2015, 06:39:38 pm »
Well, first and for all it is best to simply not write assembler.

The difference between the two variants, is that in the first variant (assembler;) you can do your own prologue and epilogue.
Only if you add nostackframe too

And yes. Don't write assembler ;)

Eugene Loza

  • Hero Member
  • *****
  • Posts: 563
    • My "almost daily" development blog
Re: Assembly language trade-offs
« Reply #4 on: May 08, 2015, 07:21:51 pm »
Quote
And yes. Don't write assembler ;)
Why???
I was writing assembler in far mid 1990s till early 2000s (until Windows closed the direct processor access and Delphi switched assembler logic) - and it was producing a +16% efficiency just by rewriting the pascal code into asm without optimizations, and sometimes up to 10x (e.g. for map generation algorithm "over a minute -> a few seconds")
I had no time to dig into asm since then (and lived with a dream to do so some day), but are there any cross-platform problems/issues?
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

Septe

  • Jr. Member
  • **
  • Posts: 68
Re: Assembly language trade-offs
« Reply #5 on: May 08, 2015, 09:50:56 pm »
I look at it this way, it's your choice to use assembler or not.  I think what they're trying to say is that yes, assembler can create gains in speed but at a cost of readability and maintainability.  Personally, I'd stay away from assembler because if I didn't write it and it broke, I'm screwed.  >:(

Rick314

  • New Member
  • *
  • Posts: 28
Re: Assembly language trade-offs
« Reply #6 on: May 09, 2015, 05:31:37 am »
Assembly is appropriate for the program I am working on -- an extended-precision math program. I need the x86_64 platform 64*64=128-bit multiply and 128/64=64 rem 64-bit divide instructions in tight loops to extend to 1000s of digits. They set the overall execution time of the program.

The original 2 questions remain, unless someone can positively assert "there are no advantages one way or the other". For the 2nd question, I learned that the compiler will put passed parameters in different places (registers and/or stack) for the two methods shown, but that still doesn't make one preferred over the other.

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Assembly language trade-offs
« Reply #7 on: May 09, 2015, 06:37:17 am »
if i may...

if the code will spend a very high percentage of time in the loops - i'm guessing it could since you will be working with such high-precision numerics -  does it make much difference how the loops/your asm code is entered/exited?

unless you can/need to take advantage of how/where the parameters are passed, doesn't it really come down to your own preference?
Lazarus Ver 1.4.0 FPC Ver 2.6.4 SVN 48774 32-bit
Windows 7 U 64-bit

Rick314

  • New Member
  • *
  • Posts: 28
Re: Assembly language trade-offs
« Reply #8 on: May 10, 2015, 12:30:25 am »
This isn't just preference, as I continue to discover details about their use -- Using both the "assembler;" and "[’EAX’,’EBX’]" constructs together results in "Warning: Register list is ignored for pure assembler routines". That's news. If it is documented somewhere, please provide a link to it. So in pure assembler functions and procedures ("assembler;") does the compiler save and restore all registers and allow the user access to any of them, or just some subset, or none of them (and the programmer has to take care of that), or what? I'm not asking for guesses at the answer, but for documentation on exactly what the two constructs ("assembler;" and the "[’EAX’,’EBX’] list") do. If someone knows how to ask the appropriate compiler author to comment here, please do so.
« Last Edit: May 10, 2015, 12:33:11 am by Rick314 »

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Assembly language trade-offs
« Reply #9 on: May 10, 2015, 12:37:44 am »
i see your point.
i must say, you've dug much further into the use of assembly that i.
i've no links or info that would be of use to you.

perhaps someone will chime in here.

Lazarus Ver 1.4.0 FPC Ver 2.6.4 SVN 48774 32-bit
Windows 7 U 64-bit

lagprogramming

  • Full Member
  • ***
  • Posts: 159
Re: Assembly language trade-offs
« Reply #10 on: May 16, 2015, 07:30:40 pm »
   I consider you've read "Programmer's guide" Chapter 3(especially 3.4). What I write here is what I remember at the moment from additional sources, for this reason, you should double check the following content.
   Using the "assembler" directive will lead the compiler to save all your registers. This is why you get "Warning: Register list is ignored for pure assembler routines". I think that the "nostackframe" modifier is the only one that has an influence over the produced code. Everything else may be optimized by the compiler at free will.
   Regarding the second approach, without the register series found within pharanthesis the compiler doesn't understand what registers you are about to modify, yet, reason why it will save all of them like it's done in the previous case("assembler"). This is why you are allowed to tell the compiler what registers you'll modify, otherwise what you would gain in assembly might get losed at saving/restoring registers.
   I also remember that the "assembler" case would save all registers as a precaution. A series of registers between brackets is ignored because long time ago people didn't instructed the compiler right and the compiler produced wrong code, code that was very hard to be debugged. Again, this is what I remember at the moment, I may be totally wrong.

MathMan

  • Full Member
  • ***
  • Posts: 166
Re: Assembly language trade-offs
« Reply #11 on: June 13, 2015, 08:48:05 am »
The Reference Guide gives the following assembly language example:
Code: [Select]
asm 
  Movl $1,%ebx 
  Movl $0,%eax 
  addl %eax,%ebx 
end [’EAX’,’EBX’];
where the "[’EAX’,’EBX’]" tells the compiler EAX and EBX are used, so it can push and pop them before and after the "asm" block. I would tend to leave out "[’EAX’,’EBX’]", do the pushes and pops myself, not need to trust the compiler to do it, and prove to the reader they are available for use. Is there any advantage to using the "[’EAX’,’EBX’]" construct other than typing a little less?

Hi - I was absent from this forum for some time and just discovered this. If you want take a look at the attached - that's the way how it is working for me. Beside that the division routine is probably 4-5 times faster than your approach and I understand speed is essential for you. For the Pascal Version you'll Need some additional includes etc - but I also understand that you are looking for asm.

Regards,
MathMan