sjasm and data structure arrays

Page 1/2
| 2

By DarkSchneider

Paladin (965)

DarkSchneider's picture

08-04-2014, 12:47

I have some basic questions (much time not using ASM), maybe someone can help me:

- If I don't define struct, I have to access by offset?. i.e.

player
  db 0 ; x
  db 0 ; y
  db .... ; more data

So to access player.y it is done by
(player+1)

- Using something like this topic
http://www.msx.org/forum/msx-talk/development/sjasm-042-and-structs

	map 0xc000
; other variables
dxmap		#1
xmap			#2
yship			#1
xship			#2
aniframe		#1
anispeed		#1
ram_sat			#128
                                
; struct declared
	struct enemy_data
y		db	0
x		dw	0
status	db	0
	ends

; actual enemy data  - at least in my idea ;-)
enemies:    #enemy_data*24

end_of_enemies #0

	endmap

Is there a way to access the enemies array by index?, something like
enemies[4].status

Thanks.

Login or register to post comments

By Daemos

Paragon (2044)

Daemos's picture

08-04-2014, 13:10

I am not sure if you mean something like this

enemies:
;    x  y  v1 v2  status
db 0,0, 0,   0,     0 ;first enemy
lenghtarray: equ $ - enemies
db 0,0, 0,   0,     0
db 0,0, 0,   0,     0
db 0,0, 0,   0,     0
db 0,0, 0,   0,     0
db 0 ;end of array
totalarraysize: equ $ - enemies
x:         equ 0
y:         equ 1
v1:       equ 2
v2:      equ 3
status:  equ 4

now you can load enemies into ix and when you want to access the 4th entry just add arraysize 4times to it

ld ix,enemies
ld b,4 ;enemies[4]
ld de,lenghtarray
.setpointer:
add ix,de
djnz .setpointer

ld a,(ix+status) ;enemies[4].status

Is this what you are looking for??

EDIT:

or even easier to get the right ix[4]

ld ix,enemies*(lenghtarray*4)
ld a,(ix+status)

By DarkSchneider

Paladin (965)

DarkSchneider's picture

08-04-2014, 13:11

The symbol '$' points to the current address?, I have not seen that in sjasm documentation.

You have not used structs so you directly use the offset defining the offsets by name (the final EQU), and each base data struct offset (lenghtarray).

I am not sure buy I think sjasm allows to use operators, so maybe this could be done (not sure but http://home.wanadoo.nl/smastijn/sjasmman4.html#s3):
ld a, (enemies+(lenghtarray*index)+status)
And let the assembler do the work :)
EDIT: now I have seen your edit ;)

And there is a way to do it using structs?

By Daemos

Paragon (2044)

Daemos's picture

08-04-2014, 13:18

Quote:

The symbol '$' points to the current address?

yes and Sjasm supports that.

Quote:

ld a, (enemies+(lenghtarray*index)+status)

Never tried it myself. Try it Smile I am curious

Quote:

And there is a way to do it using structs?

Not 100% sure. For a single variable defining structs is ease but making an array with structs is somewhat more difficult.

EDIT: I wonder. if you define enemies like this:

enemies[0]: equ (enemies*lenghtarray*0)
enemies[1]: equ (enemies*lenghtarray*1)
;etc.

Then you just have to load the right value into ix

By DarkSchneider

Paladin (965)

DarkSchneider's picture

08-04-2014, 16:01

Tested this:

Quote:

ld a, (enemies+(lenghtarray*index)+status)

And it works Wink

There is another question, about how to work over RAM, it has been problematic until I have defined this on the asm code:

...
previous code
...
  ; RAM section
  output ram.rom
  defpage 99,#c000,#f380-#c000
  page 99

  code @ #c000
enemies
  db 0,0,0,0,0 ; first enemy
lenghtenemy equ $ - enemies
  db 0,0,0,0,0
  db 0,0,0,0,0
  db 0,0,0,0,0
  db 0,0,0,0,0
  db 0 ; end of array

lenghtenemies equ $ - enemies
x equ 0
y equ 1
v1 equ 2
v2 equ 3
status equ 4

  end

Is this the correct way to do it? Any way for no output?

By Daemos

Paragon (2044)

Daemos's picture

08-04-2014, 16:34

You can just trow all your variables in a section which you either start with org $c000 or use phase $c000 and end it with dephase to get your variables on the right place on RAM.

So lets say for example your code starts in page1 rom at adress $4000:

org $4000

ld a,(testbyte)
and a
ld b,a
add b
ld (testbyte),a

;put variables on RAM page3
phase $c000

testbyte: rb 0 ;reserve the byte on this address

dephase

By DarkSchneider

Paladin (965)

DarkSchneider's picture

08-04-2014, 17:17

Right, phase is the key. Another one not documented on sjasm documentation. Is there a place to get all those things?, like the mentioned '$' or other key words/symbols. It is the first time I use z80 assembler and I don't know them, and sjasm documentation has missing things.

By Daemos

Paragon (2044)

Daemos's picture

08-04-2014, 18:59

I learned all these things by looking at other source, ask around or just happen to have someone around who knows about this. I am affraid that on the documentation part things will get difficult for my part.

If no one can supply you with proper documentation it is the best practice to just code something and ask around when things are not clear, like you are doing right now Smile

By ARTRAG

Enlighted (6923)

ARTRAG's picture

08-04-2014, 19:27

In sjasm I do this

    struct enemy_data
y               db  0
x               dw  0
status          db  0
kind            db  0
cntr            db  0
speed           dw  0
    ends

enemies:
[max_enem] enemy_data

ms_bullets:
[max_bullets] enemy_data

and later I do

   ld  ix,enemies
[...]
    ld  l,(ix+enemy_data.x+0)
    ld  h,(ix+enemy_data.x+1)
[...]
    ld  a,(ix+enemy_data.y)
[...]

By Daemos

Paragon (2044)

Daemos's picture

08-04-2014, 20:02

Sjasm is too powerfull Smile

By Huey

Prophet (2694)

Huey's picture

08-04-2014, 20:25

Sjasm works great.
The only thing I miss is page numbers in the symbol file output. Sad

Page 1/2
| 2