trying to copy memory address then use it

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Wall_Axe
Manic Miner
Posts: 500
Joined: Mon Nov 13, 2017 11:13 pm

trying to copy memory address then use it

Post by Wall_Axe »

I need to explain this first:

I want have a string and copy its address to the start of the character struct. i.e. right on the playerCharacter label.
The string will be the characters name, but there are going to be loads of characters called 'goblin' so im just copying a pointer to the string goblin to the character's address....well, trying to.

Im using numbers here to make the example super short. So usually number: would hold a string.

the result im looking for is two black lines on the screen ie 255,255
that would mean it works for strings.

i copy the address of playerCharacter to the RAM address thisCharacter because I want a function that works on any character. So i copy the character I want to work on to 'thisCharacter'.

Code: Select all

ORG #8000


LD HL,playerCharacter


LD (thisCharacter),HL



LD HL,(thisCharacter)
LD DE,number
LD A,D
LD (HL),A
INC HL
LD A,E
LD (HL),A

DEC HL

LD D,(HL)
INC HL
LD E,(HL)

LD (16384),DE


keeplooping:
jp keeplooping

thisCharacter:
defw #0


playerCharacter:
defw #1
defb 3;strength
defb 3;skill
defb 3; charisma 

number:
defb 255,255			
Edited by PJ to format code
User avatar
Bedazzle
Manic Miner
Posts: 305
Joined: Sun Mar 24, 2019 9:03 am

Re: trying to copy memory address then use it

Post by Bedazzle »

Code: Select all

	LD DE,number
	LD A,D
	LD (HL),A
	INC HL
	LD A,E
	LD (HL),A
can be replaced by

Code: Select all

	LD DE,number
	LD (HL),D
	INC HL
	LD (HL),E
And please note, that in memory 16 bit values are stored not like D, E, but vice versa.
Maybe it is better to keep it in same way:

Code: Select all

	LD DE,number
	LD (HL),E
	INC HL
	LD (HL),D
Wall_Axe
Manic Miner
Posts: 500
Joined: Mon Nov 13, 2017 11:13 pm

Re: trying to copy memory address then use it

Post by Wall_Axe »

Thanks I copied that code.

Got the thing in the original post working as well. Just had to use brackets at the right time :ugeek:
Post Reply