Page 1 of 1

Using the 'ds' directive to make value containers

Posted: Wed Oct 23, 2019 7:02 pm
by Kitsune Mifune
I've posted roughly the same question on the CPC forum, but just to clarify on here and get a different perspective before I move forward.

I've been using labels like variables, and I was setting a whole big page full of stuff like like "XPOS equ 0" to get empty containers that I could shift values in and out of that are easily user-readable. Obviously this hasn't worked out as I've now realised that 'equ' just makes the label a constant, and is the same value all the time.

I recently discovered the 'ds' directive, but I just want to clarify it does what's needed.

If I set say "XPOS ds 1,3" on initialisation, I'm assuming it'll reserve one byte and load it with the value '3' which can then be changed later on?

For example, if later in the code I want to go:

Code: Select all

	ld a,0
	ld (XPOS),a
Will it then overwrite the old value of '3' by loading the new value of '0' into XPOS and keep it there until changed again?

I've based my whole code around this concept for things like sprite width and height etc. so that I can load values for both player and enemies into the same 'draw to screen' code to reuse it. Until now it's worked by sheer fluke as most of the values were set to 'equ 0' by the old way I was doing it. However, I recently started to get some conflicts with the assembler mistaking labels for one another, and I think the code will work if I can just define a label to mean a space where I can lob values in and out of that won't be touched by anything else.

Thanks! :)

Re: Using the 'ds' directive to make value containers

Posted: Thu Oct 24, 2019 7:58 am
by Gee-k
Hey hey! me again.
I was thinking about variables etc yesterday and I've decided that i'm just going to reserve a spot in RAM to use. Although, my program is pretty small. No sprites. It's basically a glorified calculator.
Would that be any use? Just using a spot in RAM to store things I mean. bring it back when you need to, then change it and put it away again.

Re: Using the 'ds' directive to make value containers

Posted: Thu Oct 24, 2019 11:50 am
by akuyou
Yes your example is fine... "DS" defines a sequence of bytes... you're defining one byte of 3 which is fine!, but you could also do

Code: Select all

MyByte: DB 3 
To define one Byte with a value of 3 (with the label Mybyte) or

Code: Select all

MyWord: DW 3
to define a word (byte pair of 3)... you'd load this with something like ld hl,(MyWord)

DS is good for things like this:

Code: Select all

MyRAM:  DS 32 ;define 32 bytes of ram (defaults to zer0
MyByte equ MyRam
MyWord equ MyRam+1
MyByte2 equ MyRam+3
Don' worry if you don't understand what this does! I've defined 32 bytes of spare ram... and given the first byte (offset zero) the label 'MyByte'... the Second Byte as 'Myword'... and because I'm assuming that will store a word - MyByte2 as the third

DS/DB or DW are a good way to do things on machines where you're program is running in RAM (like the CPC or Speccy)... if you're running from cartridge rom, you'll need to use EQU to point to areas of the system ram instead (as your program code will be in ROM, so the area reserved by DS would be read only).

I hope this makes sense!

Re: Using the 'ds' directive to make value containers

Posted: Thu Oct 24, 2019 12:09 pm
by Kitsune Mifune
Excellent, thanks for the replies chaps!

That really helps me out knowing how to throw values around with labels, and the debugger isn't going crazy reading different labels for different things now.