Re: How to write to Self from assembler in TP Object constructor? lazarus / 62-General ---------------------------------------------------------------------- Avinash · 2022-02-09 10:06 I just tried to start with the simplest, however in the case of Borland Pascal the object is initialized, but in the FPC it always outputs FAIL: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TObj = object    constructor Init;  end;   constructor TObj.Init; assembler;  asm  end; var Obj: TObj;begin  if Obj.Init then WriteLn('Init')              else WriteLn('FAIL');end. ---------------------------------------------------------------------- Thaddy · 2022-02-09 10:24 This writes init:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode tp} // also tested fpc, delphi, objfpc modes...type  TObj = object    constructor Init;  end;   constructor TObj.Init; assembler;  asm  end; var Obj: TObj;begin  if Obj.Init then WriteLn('Init')              else WriteLn('FAIL');end. ---------------------------------------------------------------------- Avinash · 2022-02-09 10:38 I only have FAIL, tested in 3.0.4 and 3.2.2, Win32 versions. ---------------------------------------------------------------------- marcov · 2022-02-09 10:46 Since you use a different datastructure (TP Object) vs managed records, I split the topic and adapted the title. It is a bit weird to use Delphi like constructor syntax with TP objects ? ---------------------------------------------------------------------- Avinash · 2022-02-09 10:59 Are you saying that I didn't create the object dynamically with the «New» function? But it's a documented standard feature in that old object model from Borland Pascal. The object can be static variable and the constructor returns True/False as a function. But this is not important here, I did it just to simplify the code, because creating an object dynamically does not change anything, only FAIL is still displayed (but everything is in fine in Borland Pascal): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  PObj = ^TObj;  TObj = object    constructor Init;  end;   constructor TObj.Init; assembler;  asm  end; var P: PObj;begin  P := New(PObj, Init);  if P <> Nil then WriteLn('Init')              else WriteLn('FAIL');end. ---------------------------------------------------------------------- Thaddy · 2022-02-09 11:06 Can not replicate this. ---------------------------------------------------------------------- marcov · 2022-02-09 11:15 Quote from: Avinash on February 09, 2022, 11:59:36 amBut it's a documented standard feature in that old object model from Borland Pascal. The object can be static variable and the constructor returns True/False as a function. Yeah, I was confused. The virtual constructor mechanism of Delphi is new, not the keyword constructor itself. But if you have the BP7 page number of where the return value of the constructor is specified without NEW I'd be interested (I quickly read through page 106 and on, but couldn't find it). My guess is that the assembler simply doesn't set the return value. Compare -al output with a pascal constructor. (calling e.g. FAIL) Still, it was only a side note. ---------------------------------------------------------------------- Avinash · 2022-02-09 11:54 «Constructor-error recovery» page 107 QuoteWhen dynamic instances are allocated through the extended syntax of New, a resulting value of nil in the specified pointer variable indicates that the operation failed. Unfortunately, there is no such pointer variable to inspect after the construction of a static instance or when an inherited constructor is called. Instead, Turbo Pascal allows a constructor to be used as a Boolean function in an expression: A return value of True indicates success, and a return value of False indicates failure due to a call to Fail within the constructor. Quote from: marcov on February 09, 2022, 12:15:05 pmCompare -al output with a pascal constructor. The output is different: Also when I compile with fpc.exe -al -Anasmwin32 switches I get FAIL even with the pascal constructor. With a pascal constructor: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         .file "test.pas"# Begin asmlist al_procedures .section .text.n_p$program$_$tobj_$__$$_init$$longbool,"x"        .balign 16,0x90.globl  P$PROGRAM$_$TOBJ_$__$$_INIT$$LONGBOOLP$PROGRAM$_$TOBJ_$ __$$_INIT$$LONGBOOL:# [test.pas]# [8] begin //asm        pushl   %ebp        movl    %esp,%ebp        leal    -8(%esp),%esp# Var $vmt located at ebp-4, size=OS_32# Var $self located at ebp-8, size=OS_32        movl    %eax,-8(%ebp)        movl    %edx,-4(%ebp)        leal    -4(%ebp),%edx        movl    -8(%ebp),%eax        movl    $0,%ecx      call    fpc_help_constructor        movl    %eax,-8(%ebp)        cmpl    $0,-8(%ebp)        je      .Lj5        jmp     .Lj6.Lj5:      jmp     .Lj3        .balign 4,0x90.Lj6:.Lj3:# [9] end;        movl    -8(%ebp),%eax        movl    %ebp,%esp        popl    %ebp        ret .section .text.n__main,"x"        .balign 16,0x90.globl  PASCALMAINPASCALMAIN:.globl  _main_main:# [12] begin        pushl   %ebp        movl    %esp,%ebp        pushl   %ebx        call    fpc_initializeunits# [13] P := New(PObj, Init);        movl    $VMT_$P$PROGRAM_$$_TOBJ,%eax        movl    %eax,%edx        movl    $0,%eax        call    P$PROGRAM$_$TOBJ_$__$$_INIT$$LONGBOOL        movl    %eax,U_$P$PROGRAM_$$_P# [14] if P <> Nil then WriteLn('Init')        cmpl    $0,U_$P$PROGRAM_$$_P        jne     .Lj7        jmp     .Lj8.Lj7:        call    fpc_get_output        movl    %eax,%ebx      movl    $_$PROGRAM$_Ld1,%ecx        movl    %ebx,%edx        movl    $0,%eax        call    fpc_write_text_shortstr        call    fpc_iocheck        movl    %ebx,%eax        call    fpc_writeln_end      call    fpc_iocheck        jmp     .Lj9.Lj8:# [15] else WriteLn('FAIL');        call    fpc_get_output        movl    %eax,%ebx        movl    $_$PROGRAM$_Ld2,%ecx        movl    %ebx,%edx        movl    $0,%eax        call    fpc_write_text_shortstr        call    fpc_iocheck        movl    %ebx,%eax        call    fpc_writeln_end        call    fpc_iocheck.Lj9:# [16] end.        call    fpc_do_exit        popl    %ebx        movl    %ebp,%esp        popl    %ebp        ret# End asmlist al_procedures# Begin asmlist al_globals .section .bss        .balign 4# [11] var P: PObj;U_$P$PROGRAM_$$_P:        .zero 4 .section .rodata.n_VMT_$P$PROGRAM_$$_TOBJ,"d"        .balign 4.globl  VMT_$P$PROGRAM_$$_TOBJVMT_$P$PROGRAM_$$_TOBJ:        .long   4,-4,0,0 .section .data.n_INITFINAL,"d"        .balign 4.globl  INITFINALINITFINAL:        .long   2,0        .long   INIT$_$SYSTEM      .long   0        .long   INIT$_$FPINTRES        .long   0 .section .data.n_FPC_THREADVARTABLES,"d"        .balign 4.globl  FPC_THREADVARTABLESFPC_THREADVARTABLES:        .long   1        .long   THREADVARLIST_$SYSTEM$indirect .section .rodata.n_FPC_RESOURCESTRINGTABLES,"d"        .balign 4.globl  FPC_RESOURCESTRINGTABLESFPC_RESOURCESTRINGTABLES:        .long   0 .section .data.n_FPC_WIDEINITTABLES,"d"        .balign 4.globl  FPC_WIDEINITTABLESFPC_WIDEINITTABLES:        .long   0 .section .data.n_FPC_RESSTRINITTABLES,"d"        .balign 4.globl  FPC_RESSTRINITTABLESFPC_RESSTRINITTABLES:        .long   0 .section .fpc.n_version        .balign 16__fpc_ident:        .ascii  "FPC 3.2.2 [2021/05/15] for i386 - Win32" .section .data.n___heapsize,"d"        .balign 4.globl  __heapsize__heapsize:        .long   0 .section .data.n___fpc_valgrind,"d"        .balign 4.globl  __fpc_valgrind__fpc_valgrind:        .byte   0# End asmlist al_globals# Begin asmlist al_typedconsts .section .rodata.n__$PROGRAM$_Ld1,"d"        .balign 4.globl  _$PROGRAM$_Ld1_$PROGRAM$_Ld1:        .ascii  "\004Init\000" .section .rodata.n__$PROGRAM$_Ld2,"d"        .balign 4.globl  _$PROGRAM$_Ld2_$PROGRAM$_Ld2:        .ascii  "\004FAIL\000"# End asmlist al_typedconsts# Begin asmlist al_indirectglobals .section .rodata.n_VMT_$P$PROGRAM_$$_TOBJ,"d"        .balign 4.globl  VMT_$P$PROGRAM_$$_TOBJ$indirectVMT_$P$PROGRAM_$$_TOBJ$indirect:      .long   VMT_$P$PROGRAM_$$_TOBJ# End asmlist al_indirectglobals  With a assembler costructor: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         .file "test.pas"# Begin asmlist al_pure_assembler .section .text.n_p$program$_$tobj_$__$$_init$$longbool,"x"        .balign 16,0x90.globl  P$PROGRAM$_$TOBJ_$__$$_INIT$$LONGBOOLP$PROGRAM$_$TOBJ_$ __$$_INIT$$LONGBOOL:# Temps allocated between ebp-4 and ebp+0# [test.pas]# [8] asm        pushl   %ebp        movl    %esp,%ebp      leal    -4(%esp),%esp        pushl   %ebx        pushl   %esi        pushl   %edi        movl    %edx,-4(%ebp)        leal    -4(%ebp),%edx        movl    $0,%ecx        call    fpc_help_constructor        testl   $-1,%eax        je      .Lj5      jmp     .Lj6.Lj5:        jmp     .Lj3        .balign 4,0x90.Lj6:      xorl    %eax,%eax        pushl   $.Lj9        pushl   %ebp        pushl   $__FPC_except_handler        pushl   %fs:(%eax)        movl    %esp,%fs:(%eax)#  CPU PENTIUM#  CPU PENTIUM        xorl    %eax,%eax        popl    %edx        addl    $12,%esp        movl    %edx,%fs:(%eax)        jmp     .Lj10.Lj9:# [9] end;        movl    %edx,-4(%ebp)        leal    -4(%ebp),%edx        movl    $0,%ecx      call    fpc_help_fail        call    fpc_reraise        call    FPC_DONEEXCEPTION.Lj10:.Lj3:        popl    %edi        popl    %esi        popl    %ebx        movl    %ebp,%esp        popl    %ebp      ret# End asmlist al_pure_assembler# Begin asmlist al_procedures .section .text.n__main,"x"        .balign 16,0x90.globl  PASCALMAINPASCALMAIN:.globl  _main_main:# [12] begin        pushl   %ebp        movl    %esp,%ebp        pushl   %ebx        call    fpc_initializeunits# [13] P := New(PObj, Init);        movl    $VMT_$P$PROGRAM_$$_TOBJ,%eax        movl    %eax,%edx        movl    $0,%eax        call    P$PROGRAM$_$TOBJ_$__$$_INIT$$LONGBOOL        movl    %eax,U_$P$PROGRAM_$$_P# [14] if P <> Nil then WriteLn('Init')        cmpl    $0,U_$P$PROGRAM_$$_P        jne     .Lj12        jmp   .Lj13.Lj12:        call    fpc_get_output        movl    %eax,%ebx      movl    $_$PROGRAM$_Ld1,%ecx        movl    %ebx,%edx        movl  $0,%eax        call    fpc_write_text_shortstr        call    fpc_iocheck        movl    %ebx,%eax        call    fpc_writeln_end      call    fpc_iocheck        jmp     .Lj14.Lj13:# [15] else WriteLn('FAIL');        call    fpc_get_output        movl    %eax,%ebx        movl    $_$PROGRAM$_Ld2,%ecx        movl    %ebx,%edx        movl    $0,%eax        call    fpc_write_text_shortstr        call    fpc_iocheck        movl    %ebx,%eax        call    fpc_writeln_end        call    fpc_iocheck.Lj14:# [16] end.        call    fpc_do_exit        popl  %ebx        movl    %ebp,%esp        popl    %ebp        ret# End asmlist al_procedures# Begin asmlist al_globals .section .bss        .balign 4# [11] var P: PObj;U_$P$PROGRAM_$$_P:        .zero 4 .section .rodata.n_VMT_$P$PROGRAM_$$_TOBJ,"d"        .balign 4.globl  VMT_$P$PROGRAM_$$_TOBJVMT_$P$PROGRAM_$$_TOBJ:        .long   4,-4,0,0 .section .data.n_INITFINAL,"d"        .balign 4.globl  INITFINALINITFINAL:        .long   2,0        .long   INIT$_$SYSTEM      .long   0        .long   INIT$_$FPINTRES        .long   0 .section .data.n_FPC_THREADVARTABLES,"d"        .balign 4.globl  FPC_THREADVARTABLESFPC_THREADVARTABLES:        .long   1        .long   THREADVARLIST_$SYSTEM$indirect .section .rodata.n_FPC_RESOURCESTRINGTABLES,"d"        .balign 4.globl  FPC_RESOURCESTRINGTABLESFPC_RESOURCESTRINGTABLES:        .long   0 .section .data.n_FPC_WIDEINITTABLES,"d"        .balign 4.globl  FPC_WIDEINITTABLESFPC_WIDEINITTABLES:        .long   0 .section .data.n_FPC_RESSTRINITTABLES,"d"        .balign 4.globl  FPC_RESSTRINITTABLESFPC_RESSTRINITTABLES:        .long   0 .section .fpc.n_version        .balign 16__fpc_ident:        .ascii  "FPC 3.2.2 [2021/05/15] for i386 - Win32" .section .data.n___heapsize,"d"        .balign 4.globl  __heapsize__heapsize:        .long   0 .section .data.n___fpc_valgrind,"d"        .balign 4.globl  __fpc_valgrind__fpc_valgrind:        .byte   0# End asmlist al_globals# Begin asmlist al_typedconsts .section .rodata.n__$PROGRAM$_Ld1,"d"        .balign 4.globl  _$PROGRAM$_Ld1_$PROGRAM$_Ld1:        .ascii  "\004Init\000" .section .rodata.n__$PROGRAM$_Ld2,"d"        .balign 4.globl  _$PROGRAM$_Ld2_$PROGRAM$_Ld2:        .ascii  "\004FAIL\000"# End asmlist al_typedconsts# Begin asmlist al_indirectglobals .section .rodata.n_VMT_$P$PROGRAM_$$_TOBJ,"d"        .balign 4.globl  VMT_$P$PROGRAM_$$_TOBJ$indirectVMT_$P$PROGRAM_$$_TOBJ$indirect:      .long   VMT_$P$PROGRAM_$$_TOBJ# End asmlist al_indirectglobals ---------------------------------------------------------------------- Thaddy · 2022-02-09 12:10 Can it be that my tests passed because I did NOT use an external assembler? Because it looks like you did... ---------------------------------------------------------------------- Avinash · 2022-02-09 12:19 No, I just compile as fpc file.pas I even created an fpc.cfg in the directory with one line -viwn ---------------------------------------------------------------------- Thaddy · 2022-02-09 16:01 -Anasmwin32 suggests otherwise. Nasm is an external assembler, not a mode. It can be that other debugger settings trigger this, but then you will get a warning. So what warning did you get? ---------------------------------------------------------------------- Avinash · 2022-02-09 16:40 Quote from: Thaddy on February 09, 2022, 05:01:48 pm -Anasmwin32 suggests otherwise.Markov asked me about assembler output with the -al option, and since Intel assembler is much more familiar to me, I started doing it with -al -Anasmwin32, and found that in this case I get FAIL even with the pascal constructor (in case of FPC 3.2.2, I already noted here before that FPC 3.2.2 buggy compiles with Nasm). I don't get any warnings, as I already wrote, I just compile without any switches and with an almost empty fpc.cfg ---------------------------------------------------------------------- ASerge · 2022-02-10 22:25 Quote from: Avinash on February 09, 2022, 11:59:36 amThe object can be static variable and the constructor returns True/False as a function. Something is doubtful. This is the first time I've heard this. QuoteBut this is not important here, I did it just to simplify the code, because creating an object dynamically does not change anything, only FAIL is still displayed (but everything is in fine in Borland Pascal): Because the size of the object is zero and no memory is allocated for it. Compatible with Delphi 7. ---------------------------------------------------------------------- Avinash · 2022-02-11 06:32 I tried FPC version 2.2.2 (yr 2008) and it shows «Init» fine.Quote from: ASerge on February 10, 2022, 11:25:54 pmSomething is doubtful. This is the first time I've heard this. In subsequent posts I quoted about this from the Language Guide.Quote from: ASerge on February 10, 2022, 11:25:54 pmBecause the size of the object is zero and no memory is allocated for it. Compatible with Delphi 7. SizeOf gives 4 bytes (VMT field). Another from the Language Guide: QuoteIf an object type defines virtual methods, constructors, or destructors, the compiler allocates an extra field in the object type. This 16-bit field, called the virtual method table (VMT) field, is used to store the offset of the object type's VMT in the data segment.QuoteThe code that performs allocation and virtual method table (VMT) field initialization of a dynamic instance is part of a constructor's entry sequence: When control arrives at the begin of the constructor's statement part, the instance will have been allocated and initialized already.