Using the 'ds' directive to make value containers

Post programming questions related to the CPC Here!
Post Reply
Kitsune Mifune
Posts: 33
Joined: Tue Oct 22, 2019 6:46 am
Location: Glasgow
Contact:

Using the 'ds' directive to make value containers

Post by Kitsune Mifune » Wed Oct 23, 2019 7:02 pm

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! :)
Programming: 90% failures, 10% victories, and 100% hair loss!

User avatar
Gee-k
Posts: 28
Joined: Sat May 04, 2019 9:35 am
Location: Scotland
Contact:

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

Post by Gee-k » Thu Oct 24, 2019 7:58 am

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.
You don't fail, You learn how it's not done.
https://www.gee-k.net : Where I blog about my random geeky goings on.

User avatar
akuyou
Posts: 563
Joined: Mon Apr 22, 2019 3:19 am
Contact:

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

Post by akuyou » Thu Oct 24, 2019 11:50 am

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!
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

Kitsune Mifune
Posts: 33
Joined: Tue Oct 22, 2019 6:46 am
Location: Glasgow
Contact:

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

Post by Kitsune Mifune » Thu Oct 24, 2019 12:09 pm

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.
Programming: 90% failures, 10% victories, and 100% hair loss!

Post Reply

Return to “Amstrad CPC Assembly Programming”