Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mixing variable types in a numerical calculation
#15
(01-06-2021, 02:59 PM)patters Wrote: Thanks for the tips. I didn't know about macros, this would be useful to add the wiki (I couldn't find anything about it). I had forgotten about the Z80 registers being used in pairs for 16bit numbers - I had 8bits stuck in my mind so I was trying to use UBYTE wherever I could.

The four attribute bytes I need to poll in the screen memory are not all sequential (it's two pairs in fact) so I'm a little unsure of the optimal way to aggregate the four bytes into a single ULONG. How is the below code? It seems a mess, but I guess it's still probably faster than the addition I was doing? Is it? I'm not familiar with the intricacies of how a processor calculates arithmetic at the ASM level, but my guess is that it's more complex than a few PEEKs and POKEs.

Code:
DIM a1, a2 AS UINTEGER         'target gfx attribute addresses to poll (two pairs of bytes)
DIM e1, e2 AS UBYTE            'expected attribute bytes (sprite is 4 chars, but has lateral symmetry)
DIM expectedAttrs AS ULONG

'at beginning of game round (performance not important)
e1=16: e2=16                                 'these attributes do change with different landscape colours
POKE @exp, e1: POKE @exp+1,e2                'Is a union needed? Would inline ASM be better, to use a register pair? Perf not important here though
POKE (UINTEGER @exp+2, PEEK (UINTEGER @exp)) 'copy the first two bytes again
expectedAttrs=PEEK (ULONG, @exp)

'during each cycle of the cannon shot trajectory (performance-critical)
POKE (UINTEGER @test,   PEEK (UINTEGER,a1))  'copy the first pair of bytes to the ULONG
POKE (UINTEGER @test+2, PEEK (UINTEGER,a2))  'copy the second pair of bytes to the ULONG
IF PEEK (ULONG, @test) <> expectedAttrs THEN PRINT "castle hit"

STOP

exp:
ASM
DEFB 0,0,0,0 ; is there a better way to reserve bytes at a referenced address?
END ASM

test:
ASM
DEFB 0,0,0,0
END ASM

No need to reserve memory in this case: just declare an ULong variable and use @ULong + 0, @ULong + 1,etc..
Still not sure what are you trying to achieve with this. I suppose the castles I saw in the demo you send me are 2x2 attr cells and you want to store their values. If so:


Code:
DIM expectedAttr, testAttr as ULong
DIM e1 AT @expectedAttr as UByte      ' e1 maps into the first byte of expectedAttr
DIM e2 AT @expectedAttr + 1 as UByte  ' e2 maps into the 2nd byte of expectedAttr
DIM e3 AT @expectedAttr + 2as UByte   ' e3 maps into the 3rd byte of expectedAttr
DIM e4 AT @expectedAttr + 3as UByte   ' e4 maps into the 4th byte of expectedAttr
...

POKE Uinteger @testAttr, PEEK (Uinteger, ATTRaddr(castleRow1, castleCol1))
POKE Uinteger @testAttr + 2, PEEK (Uinteger, ATTRaddr(castleRow2, castleCol1))


Still believe you might be complicating things a bit, and doing premature optimization? Big Grin
I mean, consider use a much simpler solution first (even checking position by position). See how fast it goes and optimize later (i.e. you're starting to use this compiler: use arrays of bytes for this and compare the 4 positions instead of doing this dirty thing on first place).
Reply


Messages In This Thread
RE: Mixing variable types in a numerical calculation - by boriel - 01-06-2021, 05:14 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)