Recent

Author Topic: Difference in returning value  (Read 689 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 393
Difference in returning value
« on: September 21, 2025, 08:24:07 pm »
I found a difference between how return value in FPC 3.2.2 vs 3.3.1.
Let's say we have this two functions:
Code: Pascal  [Select][+][-]
  1. function F1(A: SizeUInt; B: SizeUInt): SizeUInt;assembler;nostackframe;
  2. asm
  3.   mov Result,0
  4. end;
  5.  
And:
Code: Pascal  [Select][+][-]
  1. function F1(A: SizeUInt; B: SizeUInt): SizeUInt;assembler;
  2. asm
  3.   mov Result,0
  4. end;
  5.  
The thing I discovered in FPC 3.3.1 is that the first function return result via rax, the second function use stack. In FPC 3.2.2 both functions use rax.

The question is why this behavior was changed? Isn't returning through stack is less performant?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thaddy

  • Hero Member
  • *****
  • Posts: 18666
  • Jungle wars. And failing health it seems.
Re: Difference in returning value
« Reply #1 on: September 21, 2025, 08:49:59 pm »
Given {$asmmode intel}.

The result is in rax on both 3.2.2 and 3.3.1
You think other wise?
Code: ASM  [Select][+][-]
  1. mov rax, r8
// with nostackframe

Given default assembler mode :
Code: ASM  [Select][+][-]
  1.         movq    -8(%rbp),%rax
// without nostackframe
Also means the result is in rax. Only the calculation is performed on the stack and the stack is restored.
« Last Edit: September 21, 2025, 08:55:08 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

LemonParty

  • Sr. Member
  • ****
  • Posts: 393
Re: Difference in returning value
« Reply #2 on: September 21, 2025, 09:03:59 pm »
Yeah. But why without nostackframe we have this stack variable in a middle?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Sr. Member
  • ****
  • Posts: 393
Re: Difference in returning value
« Reply #3 on: September 22, 2025, 04:24:33 pm »
I discovered one more thing.
If we call this function:
Code: Pascal  [Select][+][-]
  1. function F1(A: SizeUInt; B: SizeUInt): SizeUInt;assembler;
  2. asm
  3.   mov Result,16
  4.   mov rax,15
  5. end;
The result will be 16.
If we comment line "mov Result,16" the result will be 15. So when somewhere in a body of function appear Result the stack variable creates and when there are no mentions of Result – rax is used. When write assembler function you need to remember about this mechanism.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thaddy

  • Hero Member
  • *****
  • Posts: 18666
  • Jungle wars. And failing health it seems.
Re: Difference in returning value
« Reply #4 on: September 22, 2025, 06:30:16 pm »
Yeah. But why without nostackframe we have this stack variable in a middle?
Because a stackframe is setup? It is all rather standard. Are you dabbling with assembler for the first time ?
There's nothing I would not excpect.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6263
  • Compiler Developer
Re: Difference in returning value
« Reply #5 on: September 22, 2025, 10:11:21 pm »
I discovered one more thing.
If we call this function:
Code: Pascal  [Select][+][-]
  1. function F1(A: SizeUInt; B: SizeUInt): SizeUInt;assembler;
  2. asm
  3.   mov Result,16
  4.   mov rax,15
  5. end;
The result will be 16.
If we comment line "mov Result,16" the result will be 15. So when somewhere in a body of function appear Result the stack variable creates and when there are no mentions of Result – rax is used. When write assembler function you need to remember about this mechanism.

If you don't use nostackframe then the compiler will setup a stackframe for local variables and this also includes handling of Result. Just look at the generated assembly code using -al.

LemonParty

  • Sr. Member
  • ****
  • Posts: 393
Re: Difference in returning value
« Reply #6 on: September 22, 2025, 11:20:29 pm »
Yes, I seen what generates compiler. For me was surprise that compiler reserve space for Result depending on if you write something to Result or not.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018