Jump to content
IGNORED

Mad Pascal: efficiently handling an array of records?


Recommended Posts

I would like to define four players in Mad Pascal.

 

Usually I'd do something like

 

type
    TPlayer = record
      Name: TString;
      Strength: Byte;
    end;


var
  players: array[0..3] of ^TPlayer;

begin;
  // not interesting
  for i:=0 to High(players) do begin
    GetMem(players[i], sizeof(TPlayer));

  players[0].strength := 30;
end;

 

However, the simple assignment compiles into

 

3A68:AD 79 41          LDA $4179
3A6B:85 92             STA $92     ;MEOLFLG
3A6D:AD 7A 41          LDA $417A
3A70:18                CLC
3A71:69 00             ADC #$00
3A73:85 93             STA $93
3A75:A0 00             LDY #$00
3A77:B1 92             LDA ($92),Y ;MEOLFLG
3A79:85 84             STA $84     ;VNTD
3A7B:C8                INY
3A7C:B1 92             LDA ($92),Y ;MEOLFLG
3A7E:85 85             STA $85     ;VNTD+1
3A80:A0 42             LDY #$42
3A82:A9 1E             LDA #$1E
3A84:91 84             STA ($84),Y ;VNTD

 

which looks like a complete overkill for a simple assignment.

Am i missing something, like a nice compiler optimization flag?

Or am i doing it wrong?

 

I think in assembler I could get it in 5-6 instructions.

I'd love to see MadPascal to do something similar.

But my Pascal got rusty and I hate Pointers.

 

Thanks for any help!

 

Link to comment
Share on other sites

Hi!

2 hours ago, drunkeneye said:

I would like to define four players in Mad Pascal.

 

Usually I'd do something like

 

type
    TPlayer = record
      Name: TString;
      Strength: Byte;
    end;


var
  players: array[0..3] of ^TPlayer;

begin;
  // not interesting
  for i:=0 to High(players) do begin
    GetMem(players[i], sizeof(TPlayer));

  players[0].strength := 30;
end;

Using dynamic memory in a 6502 is never going to be fast - you are allocating memory at runtime and storing the address into an array, so the compiler has no way of knowing the address of your variable.

 

If you know which index you are accessing, it is better if you initialize your array with static pointers, then the compiler will generate optimal code:

 

type
    TPlayer = record
      Name: TString;
      Strength: Byte;
    end;

var
  P0, P1, P2, P3: TPlayer;
  players: array[0..3] of ^TPlayer;

begin;
  players[0] := @P0;
  players[1] := @P1;
  players[2] := @P2;
  players[3] := @P3;

  P0.Strength := 30;
end.

 

And try to use functions to access the pointers - it is also faster if you access many elements of the array:

type
    TPlayer = record
      Name: TString;
      Strength: Byte;
    end;
    PlayerPtr = ^TPlayer;


var
  players: array[0..3] of ^TPlayer;
  i : byte;

procedure initPlayer(n : Byte);
var
   p: PlayerPtr;
begin
   GetMem(p, sizeof(TPlayer));
   p^.Strength := 30;
   (* ...... *)
   players[n] := p;
end;

begin;
  for i:=0 to High(players) do
      initPlayer( i );
end.

 

Have Fun!

 

  • Like 4
Link to comment
Share on other sites

Thank you very much!

 

I realize that i should not use anything dynamic to get more compact code.

So i opted now create the four variables using var and then use a simple case switch

to set currentPlayer := @player1/2/3/4. this way i can use currentPlayer via

currentPlayer^.strength := 30; and the code seems efficient.

 

However-- I cannot use the strings like i want to (in blibs):

    CRT_WriteCentered(2, p^.name);
yields 

    Error: Incompatible types: got "UNTYPED" expected "STRING"

 

So i am still missing something. Can anyone help why this doesnt work with strings,

but does so with e.g. bytes?

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...