[fpc-pascal] (no subject)

Sven Barth pascaldragon at googlemail.com
Sat Mar 8 13:21:43 CET 2014


On 07.03.2014 20:26, Barracuda wrote:
> Code:
> ---------------------------------------------------------
> function SentenceSample(N: integer): string; stdcall;
> begin
>    //writeln('N = ', N);
>    //SentenceSample := RuGetSentence(N);
>    inc(N);
> end;
> ---------------------------------------------
> Of course, inc(N); added just to simplify disassembling. And there is disasm:

You don't need to disassemble. Just pass "-al" and the compiler will 
keep the generated assembler files (normally *.s).

> ---------------------------------------------
> sentencesample  proc near
>    push    ebp
>    mov     ebp, esp
>    inc     [ebp+0Ch]
>    leave
>    retn    8
> ---------------------------------------------

If you would have used "-al" you would see this:

=== asm begin ===

.section .text.n_p$hiddenarg_$$_sentencesample$longint$$ansistring
         .balign 16,0x90
.globl  P$HIDDENARG_$$_SENTENCESAMPLE$LONGINT$$ANSISTRING
         .type   P$HIDDENARG_$$_SENTENCESAMPLE$LONGINT$$ANSISTRING, at function
P$HIDDENARG_$$_SENTENCESAMPLE$LONGINT$$ANSISTRING:
.Lc1:
# [thiddenarg.pas]
# [6] begin
         pushl   %ebp
.Lc3:
.Lc4:
         movl    %esp,%ebp
.Lc5:
# Var N located at ebp+12
# Var $result located at ebp+8
# [9] inc(N);
         addl    $1,12(%ebp)
# [10] end;
         leave
         ret     $8
.Lc2:
.Le0:
         .size   P$HIDDENARG_$$_SENTENCESAMPLE$LONGINT$$ANSISTRING, .Le0 
- P$HIDDENARG_$$_SENTENCESAMPLE$LONGINT$$ANSISTRING

=== asm end ===

Especially note the "N located at ..." and "$result located at ..." 
comments ;)

 > As you can see, procedure has only one parameter, but FPC generates 
code as if there are two
 > parameters; and the main problem is that it trying to work with wrong 
variable (N is in ebp+08,
 > not 0C; it's obvious and proved by debugging). The result is bad =)

Is there a specific reason/problem why you decided to look at the 
assembler code?


Regards,
Sven



More information about the fpc-pascal mailing list