-2

I am porting a Delphi 32bit app to a Pascal Script - which does not support asm, and I only have 64bit machines so I cannot even run the code and mimic it.

{$ASMMODE INTEL}

function BitScanForward(var BB:Int64):Integer; assembler;
 asm
   bsf eax, dword ptr [BB]
   jnz @@2
 @@0: bsf eax, dword ptr [BB+04h]
   add eax, 20h
 @@2:
end;

function BitScanBackward(var BB:Int64):Integer; assembler;
 asm
   bsr eax, dword ptr [BB+04h]
   jz @@0
   add eax, 20h
   jnz @@2
 @@0: bsr eax, dword ptr [BB]
 @@2:
end;

function BitCountAsm(const BB:Int64): Integer; assembler;
 asm
   mov ecx, dword ptr BB
   xor eax, eax
   test ecx, ecx
   jz @@1
 @@0: lea edx, [ecx-1]
   inc eax
   and ecx, edx
   jnz @@0
 @@1: mov ecx, dword ptr BB+04h
   test ecx, ecx
   jz @@3
 @@2: lea edx, [ecx-1]
   inc eax
   and ecx, edx
   jnz @@2
 @@3:
end;

function BitScanForward2(BB:Int64): Integer; assembler;
asm
   bsf eax, dword ptr BB
   jnz @@2
 @@0: bsf eax, dword ptr BB+04h
   add eax, 20h
 @@2:
end;

I would like to get those in pure pascal. I also saw a YouTube video where someone demonstrates Asm->Pascal (but cannot find the app - is there one?).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Ozz Nixon
  • 159
  • 1
  • 8
  • 8
    I'm voting to close this question as off-topic because this is not a coding service. – Bo Persson Oct 26 '15 at 15:10
  • Can you recommend a site then? – Ozz Nixon Oct 26 '15 at 19:48
  • 2
    In C: http://graphics.stanford.edu/~seander/bithacks.html – Abstract type Oct 26 '15 at 23:15
  • I like that bithacks site - ported most of them since yesterday to Modern Pascal - that is my mission right now - port as many examples over to the new product. Thanks!! – Ozz Nixon Oct 28 '15 at 22:38
  • Just click on where it says "delete" below the tags in your question. – Ross Ridge Jan 03 '20 at 07:24
  • 2
    @RossRidge: you can't delete questions with score>=1 answers. I think the idea was to prevent people from removing content that could be useful to future readers. After my edit to the title, I think this question isn't terrible and could be reopened. – Peter Cordes Jan 03 '20 at 07:51

1 Answers1

3

Something like:

 function BitScanForward(var BB:Int64):Integer;

 var i : integer;
     work:int64;
 begin
   Work:=bb;     
   i:=0;
   while (i<64) and ((bb and 1)=0) do
     begin
       inc(i);
       bb:=bb shr 1;
     end;
   result:=i;
 end;
 

BitscanBackward is the same but tests the highest bit. Probably best done unsigned, but I couldn't test that, so I leave that as an exercise to the reader. Probably the 64th bit is dangerous in the above version too, in that case make "work" uint64.

 function BitScanBackward(var BB:Int64):Integer;

 var i : integer;
     work:int64;
 begin
   Work:=bb;       result:=0;
   for i:=0 to 63 do
     begin
       if (bb and 1)=1 then
          inc(result);
       bb:=bb shr 1;  
     end;  
 end;
Maxim Masiutin
  • 3,991
  • 4
  • 55
  • 72
Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • Thanks Marco! 2nd example is a good bit counter. Any idea what the difference is between the bsf eax, dword ptr BB -- versus dword ptr [BB]? no idea what the bracket does (if anything)... – Ozz Nixon Oct 28 '15 at 22:57
  • 1
    Afaik ptr in Borland speak is [] in Masm (Microsoft) assembler. Some tools allow both or ignore the other one – Marco van de Voort Oct 29 '15 at 08:46
  • Marco, I found [] in FPC at least means the parameter has to be a var. So, I could not call BitScanForward(1), changing without [] and dropping var in the definition - then I could call raw. (I have a 32bit system now, so I can manually port the asm to pas). – Ozz Nixon May 04 '17 at 19:31
  • 2
    Well, FPC has intrinsics for some of them meanwhile, at least popcnt for bitcountasm – Marco van de Voort May 04 '17 at 20:31