Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mixing variable types in a numerical calculation
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.

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 vertical 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"


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

DEFB 0,0,0,0

Messages In This Thread
RE: Mixing variable types in a numerical calculation - by patters - 01-06-2021, 02:59 PM

Forum Jump:

Users browsing this thread: 1 Guest(s)