use address of label in z80asm?

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

use address of label in z80asm?

Post by Wall_Axe »

if i have a 'room' struct in asm:
room1:
defb "this room is dark",1,east_exit

room2:
defb "this room is big",2,east_exit
How do i put the address of the room2 label in 'east_exit' of room1?
I could do it in actual z80 later but it seems a waste. Just wondering if there is way of doing it with the pre-processor?
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: use address of label in z80asm?

Post by ParadigmShifter »

Just use the label? The label is an address (and is equal to $ at the time it is emitted - where $ is the current point in the assembler output) You need to use a dw not a db though.

Not sure what you want to do exactly, something like this?

"this room is dark", 1, east_exit, room2

in which case do this

db "this room is dark", 1, east_exit
dw room2

It doesn't matter that room2 is not yet defined when you use it (since your assembler will do 2 passes to resolve references not yet defined).

You probably want a count of items for variable length lists.

You can also use db if you split it into low byte, high byte (in that order) but I wouldn't recommend doing that. Occasionally useful if all data fits into 256 bytes (only need to store low byte) or each item starts on a 256 byte boundary (only store high byte) - that's mainly used as a memory saving device or an optimisation though.

db "this room is dark", 1, east_exit, room2&255, room2/256

macros or directives are probably available if you did want to do that (hi(x) and low(x) or maybe high and low perhaps). Far simpler to just use dw and let the assembler do the work.

You will want a terminator on the end of the string btw. Either use a 0 like in C or else "logical or" the last char with 128 (which is how Basic on Spectrum does it I think?). I think dc makes a string that is terminated by being or-ed with 128. (I can't remember, I use 0 terminated strings).

dc "Test"

should be same as

db "Tes", 't'|128

- check your assembler documentation for that
Last edited by ParadigmShifter on Thu Sep 28, 2023 12:36 am, edited 4 times in total.
User avatar
Seven.FFF
Manic Miner
Posts: 744
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: use address of label in z80asm?

Post by Seven.FFF »

Code: Select all

room1:
defb "this room is dark",1
defw room2
This counts as a forward reference. Most assemblers will do at least two passes if they decide it is needed. The first pass makes placeholders for all the values which aren’t known yet, and the second pass fills them in.

This can get much more complicated if the value from the second pass changes the size of the assembled code. Or if you have a forward reference to another forward reference. This can lead to multiple passes, and some are limited to just two (pasmo) or three (sjasmplus) passes. Others like Zeus can do up to 99. This is rarely necessary though, and in your simple case you only need two.
Last edited by Seven.FFF on Thu Sep 28, 2023 12:52 am, edited 1 time in total.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: use address of label in z80asm?

Post by ParadigmShifter »

I think all references can be resolved during pass 2 (since it can do it recursively)? Unless you are counting each recursion as another pass.

I think pass 3 in sjasm is only for fetching actual assembled code and data emitted values? EDIT: For example, pass 3 could be used to inject a checksum of all the emitted bytes somewhere into your code since it needs to have produced the actual instructions and data before it could do that.

Could be wrong though the docs are vague about it. Mostly seems to be fetching actual emitted data and copes with bank switching etc. from a brief glance.

Some assemblers can do code manipulation on the 3rd pass (like turning absolute jumps into relative jumps - may require "laddering" jumps if they are too far apart, code reordering, optimisation etc.) but sjasmplus does not do that. Assemblers that actually do do that can be used to generate relocatable code though.

EDIT2: Someone once told me about a 0-pass assembler which emits instructions as you type them, and backfills missing references along the way. Very unusual that though :)
Wall_Axe
Manic Miner
Posts: 500
Joined: Mon Nov 13, 2017 11:13 pm

Re: use address of label in z80asm?

Post by Wall_Axe »

Ok thanks all, that allows me to basically use pointers then.
Post Reply