Recent

Author Topic: Assembler procedures and function  (Read 5777 times)

dseligo

  • Hero Member
  • *****
  • Posts: 578
Assembler procedures and function
« on: January 17, 2022, 04:44:06 am »
When using assembler procedures and functions, like that:
Code: Pascal  [Select][+][-]
  1. procedure DoSomething(Data: Byte); assembler;
  2. asm
  3. ...
  4. end;

I know r1 must be left 0 at exit.
But, do I need to restore all registers I change and SREG?

ccrause

  • Hero Member
  • *****
  • Posts: 568
Re: Assembler procedures and function
« Reply #1 on: January 17, 2022, 06:54:40 am »
FPC follow the AVR GCC ABI:
https://gcc.gnu.org/wiki/avr-gcc#Register_Layout

While you are on that page, also read the calling convention section, for when you want to access parameters, or call functions from assembly.

Thaddy

  • Hero Member
  • *****
  • Posts: 11514
Re: Assembler procedures and function
« Reply #2 on: January 17, 2022, 10:21:52 am »
The compiler will restore the registers provided you announce them to the compiler:
https://www.freepascal.org/docs-html/prog/progse12.html#x148-1490003.4
That basically means that the compiler will do a save and restore for you when needed.

Right now I can not find the other option: FPC documents which registers are free to use and do not need save.restore, e.g. for 32 bit intel processors eax,edx and ecx,
« Last Edit: January 17, 2022, 10:26:54 am by Thaddy »
Путин преступник. Россияне дезинформированы.

kupferstecher

  • Hero Member
  • *****
  • Posts: 511

dseligo

  • Hero Member
  • *****
  • Posts: 578
Re: Assembler procedures and function
« Reply #4 on: January 17, 2022, 12:48:42 pm »
FPC follow the AVR GCC ABI:
https://gcc.gnu.org/wiki/avr-gcc#Register_Layout

While you are on that page, also read the calling convention section, for when you want to access parameters, or call functions from assembly.

Thanks

dseligo

  • Hero Member
  • *****
  • Posts: 578
Re: Assembler procedures and function
« Reply #5 on: January 17, 2022, 12:50:38 pm »
The compiler will restore the registers provided you announce them to the compiler:
https://www.freepascal.org/docs-html/prog/progse12.html#x148-1490003.4
That basically means that the compiler will do a save and restore for you when needed.

Right now I can not find the other option: FPC documents which registers are free to use and do not need save.restore, e.g. for 32 bit intel processors eax,edx and ecx,

Thanks, but I was asking for pure assembler procedures and specifically for AVR target.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3994
  • Compiler Developer
Re: Assembler procedures and function
« Reply #6 on: January 17, 2022, 01:25:43 pm »
The compiler will restore the registers provided you announce them to the compiler:
https://www.freepascal.org/docs-html/prog/progse12.html#x148-1490003.4
That basically means that the compiler will do a save and restore for you when needed.

This is only the case for asm-blocks. For assembly functions that clause is parsed, but ignored, because FPC adheres to the corresponding ABI.

Thaddy

  • Hero Member
  • *****
  • Posts: 11514
Re: Assembler procedures and function
« Reply #7 on: January 17, 2022, 02:33:25 pm »
I was not clear enough, but that is what I meant.
Путин преступник. Россияне дезинформированы.

kupferstecher

  • Hero Member
  • *****
  • Posts: 511
Re: Assembler procedures and function
« Reply #8 on: January 18, 2022, 04:52:40 pm »
This is only the case for asm-blocks. For assembly functions that clause is parsed, but ignored, because FPC adheres to the corresponding ABI.
What does ist mean "adheres to the corresponding ABI"? Will the registers be taken care of, or not?

Are there other differences between using an asm-block or a asm-function e.g. in regard to variables and function parameters?

Thaddy

  • Hero Member
  • *****
  • Posts: 11514
Re: Assembler procedures and function
« Reply #9 on: January 18, 2022, 06:02:36 pm »
I think that what Sarah means is that asm blocks are not portable. If you want to use assembler, you are basically on your own.
Путин преступник. Россияне дезинформированы.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3994
  • Compiler Developer
Re: Assembler procedures and function
« Reply #10 on: January 18, 2022, 09:46:53 pm »
This is only the case for asm-blocks. For assembly functions that clause is parsed, but ignored, because FPC adheres to the corresponding ABI.
What does ist mean "adheres to the corresponding ABI"? Will the registers be taken care of, or not?

The registers are taken care of according to the ABI of the platform. So if you play with assembler routines then you better check your platform's ABI. (With one of the few exceptions being the register calling convention of i386 cause that is part of the FPC documentation as it's a FPC/Delphi-only calling covention)

Are there other differences between using an asm-block or a asm-function e.g. in regard to variables and function parameters?

If you use an asm-block you'll always have an established stack frame while with an assembly function that depends on the nostackframe-directive. And depending on that the parameter locations are different which might mean that you need to write different assembly code depending on the platform (cause the compiler will simply insert the location in your assembly code, but it won't change the opcode to match that). This becomes important if you need to use different instructions for working with a register (e.g. on aarch64 the first 8 parameters are passed in registers) than for working with a value on the stack (which is were parameters will be moved to if there's a stack frame established).

kupferstecher

  • Hero Member
  • *****
  • Posts: 511
Re: Assembler procedures and function
« Reply #11 on: January 20, 2022, 02:18:29 pm »
Hello PascalDragon,

first I had to look up some things and do some tests.

So if I understand you correctly, for an ASM function the compiler doesn't insert any management code for parameters.
If according to the ABI a parameter is transfered via a register, then in the asm function the programmer has to consider that himself. In a normal function with asm block the parameter transfered via register will be stored to a local variable with the parameter's name, so in the asm block it can be used by accessing that local variable.

In an asm function local variables can be declared and used (but not if the directive nostackframe is used), and the compiler will insert some code to manage/create the stackframe in the same way as in normal functions.

For the registers modified in the asm code my understanding now is, that in the asm block the compiler takes care of pushing and popping if neccessary, but not in a asm function. The programmer has to take care of it himself.

Is that correct?

PascalDragon

  • Hero Member
  • *****
  • Posts: 3994
  • Compiler Developer
Re: Assembler procedures and function
« Reply #12 on: January 21, 2022, 04:06:53 pm »

kupferstecher

  • Hero Member
  • *****
  • Posts: 511
Re: Assembler procedures and function
« Reply #13 on: January 21, 2022, 06:34:38 pm »

 

TinyPortal © 2005-2018