More Assembly/Spectrum confusion for me :D

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

OK I wanted to try to figure this out myself but yet again I'm stumped (even after much googling/pdf reading)

Hopefully it's a simple question to you experts.

In my example program (Z80 Assembly Spectrum Simple Game) there is a line near the start,

Code: Select all

call 3503 ;ROM routine, clears screen + open ch2
My question is, where does he find this information. I have read the 48k and 128k Spectrum Manuals. Zilog User manual (didn't expect to find answer there). I have googled "ZX Spectrum Memory map" "Rom routines" and many other things.

Surely there is a list somewhere with all of those memory address which have functions built-in on the Spectrum (unless I am still misunderstanding the whole thing here!) but I cannot find it anywhere
edjones
Drutt
Posts: 33
Joined: Fri Feb 28, 2020 1:42 pm

Re: More Assembly/Spectrum confusion for me :D

Post by edjones »

User avatar
Ersh
Manic Miner
Posts: 480
Joined: Mon Nov 13, 2017 1:06 pm

Re: More Assembly/Spectrum confusion for me :D

Post by Ersh »

and this.
User avatar
Morkin
Bugaboo
Posts: 3251
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: More Assembly/Spectrum confusion for me :D

Post by Morkin »

I'm sure this has come up before, and the eventual answer always seems to be 'there isn't one'... (Maybe you could start one..? ;) )

When I say there isn't one, I mean I've never seen a "hey, here's the top 4 or 5 useful ROM routines that a lot of people found useful when learning assembly".

There's a few 'here's the entire ROM documented' like the skoolkit one, but while that may be useful for reference, it's only useful if you already know what you're looking for. Otherwise it's a needle-in-a-haystack situation.

I reckon the best I've seen on this is the very tutorial/guide you've been looking at - from what I remember, it covers:

- clearing the screen with different PAPER, BORDER colours
- playing sound effects/music (the BEEPER sound routine)
- printing strings of characters/text using the ROM print routine

To be honest I don't think I've ever used any other ROM calls, from what I remember.
My Speccy site: thirdharmoniser.com
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

thanks for the links guys. I have already been looking extensively at the first link, would you be kind enough to show me where the function "3503" is described?

Also I do have that book already. I found the page as pictured in my screenshot , but again that function couldnt be found


Image
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

Morkin wrote: Thu Jun 25, 2020 3:46 pm I'm sure this has come up before, and the eventual answer always seems to be 'there isn't one'... (Maybe you could start one..? ;) )

When I say there isn't one, I mean I've never seen a "hey, here's the top 4 or 5 useful ROM routines that a lot of people found useful when learning assembly".

There's a few 'here's the entire ROM documented' like the skoolkit one, but while that may be useful for reference, it's only useful if you already know what you're looking for. Otherwise it's a needle-in-a-haystack situation.

I reckon the best I've seen on this is the very tutorial/guide you've been looking at - from what I remember, it covers:

- clearing the screen with different PAPER, BORDER colours
- playing sound effects/music (the BEEPER sound routine)
- printing strings of characters/text using the ROM print routine

To be honest I don't think I've ever used any other ROM calls, from what I remember.

I guess I will start one :D It will take me a long time since I don't even understand how to use 3503 yet lol

Seems insane that after around 40 years there is not a table of detailed description, in order of all functions that exist in memory
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

while i have your attention. Why is "RST 16" used for print?
Sometimes in that simple game example he just uses it to 'set' a position coordinate instead. But again I can't find much documentation that is actually helpful to me.

I mean this reference site has been a godsend for me so far,
http://z80-heaven.wikidot.com/opcode-reference-chart

aswell as this excellent tutorial of course (which I would have been lost ages ago without!)
Ralf
Rick Dangerous
Posts: 2279
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: More Assembly/Spectrum confusion for me :D

Post by Ralf »

Speking of ROM procedures surprisingly I found this quite useful:

https://spectrumcomputing.co.uk/entry.php?id=8636
Image

It's in German but you can get general sense from it even if your German sucks.
User avatar
PROSM
Manic Miner
Posts: 473
Joined: Fri Nov 17, 2017 7:18 pm
Location: Sunderland, England
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by PROSM »

chilledgamer wrote: Thu Jun 25, 2020 3:49 pm thanks for the links guys. I have already been looking extensively at the first link, would you be kind enough to show me where the function "3503" is described?

Also I do have that book already. I found the page as pictured in my screenshot , but again that function couldnt be found


Image
I think the confusion here is because of a mix-up between decimal and hexadecimal. The reference you've posted is using hexadecimal, but you're looking up the decimal address of 3503, which is in fact 0x0DAF in hex.

It would definitely help to learn hex, but for now, you can probably get by just using the decimal version of the ROM disassembly. Here's the link for that function you were mentioning by the way.
All software to-date
Working on something, as always.
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

Ok I do understand hex quite well having played with NES roms for a long time.
I didn't see any prefix such as 0x & etc so assumed it was both decimal. So really I am 'calling' 13751?

Or am i looking for documentation reference to 13751?

EDIT: just found your link thank you very much
User avatar
PROSM
Manic Miner
Posts: 473
Joined: Fri Nov 17, 2017 7:18 pm
Location: Sunderland, England
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by PROSM »

chilledgamer wrote: Thu Jun 25, 2020 4:09 pm Ok I do understand hex quite well having played with NES roms for a long time.
I didn't see any prefix such as 0x & etc so assumed it was both decimal. So really I am 'calling' 13751?

Or am i looking for documentation reference to 13751?

EDIT: just found your link thank you very much
Sorry, I might have been misleading. I meant that the book was working in hexadecimal. Cauldwell is using decimal numbers in his code, so you want to look up 0DAF in your book.
All software to-date
Working on something, as always.
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

ah i see now yes :D

Well thanks again for all your help. I must admit I am really struggling, but I can remember even when simple code like Java was all gobbledy goop and then suddenly I understood it so I am hoping the same will happen here eventually.

Still stuck on exactly why rst 16 is used in the example, and also my main challenge right at this moment is just creating a loop (in my own project) so that I can control when it ends. I'll have to try again after work :|
Last edited by chilledgamer on Thu Jun 25, 2020 4:21 pm, edited 1 time in total.
User avatar
Joefish
Rick Dangerous
Posts: 2042
Joined: Tue Nov 14, 2017 10:26 am

Re: More Assembly/Spectrum confusion for me :D

Post by Joefish »

This is my favourite instruction reference.
http://www.z80.info/z80code.txt

It gives you all the op-codes, the times they take to normally execute, the flags they affect and a shorthand description of what they do.

The flags thing is handy as you might do a SUB or compare or something that sets the flags according to the result (like an 'IF'), then normally you'd do something like a RET NZ or conditionaly jump straight away based on those flags (your 'THEN' action). But there are some instructions that don't affect the flags (e.g. DEC BC), so you can fit them in between your 'IF' comparison and your 'THEN' jump (to maybe tidy up a pointer or something) without affecting the outcome.

e.g. you might want to do CP (HL) // RET Z to exit your function if a comparison fails. But maybe you still want to INC HL, whether you find a match or not. So according to that table of instructions, you can actually do CP (HL) // INC HL // RET Z because that INC HL doesn't alter the flags between the CP and the RET Z.

Also worth noting the differences between all the bitwise shift and rotate instructions. e.g. RRC is 'Rotate Right Circular', which just shuffles the 8 bits along and wraps the right bit back round to the left. Whereas RR is 'Rotate Right (with Carry)' (not confusing at all :lol: ) which uses the 'Carry' flag as a ninth bit in the rotation, so you can daisy-chain the bit you rotate out of one byte and into the carry flag, then from the carry flag into the next byte you use RR on.
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: More Assembly/Spectrum confusion for me :D

Post by R-Tape »

chilledgamer wrote: Thu Jun 25, 2020 3:50 pm Seems insane that after around 40 years there is not a table of detailed description, in order of all functions that exist in memory
There are quite a few ROM disassemblies out there. Here's one (I put it on my own webspace. I think it's by Geoff Wearmouth, but I couldn't find his link. Am I allowed to do this?) If you search this text file for "0DAF" (3503 in hexadecimal), you'll see the CLS routine (or you can look at the memory address in your emulator debugger).

RST 16 is the same, it's listed under "0010" (16 in hex). 'RST' is basically a CALL, but faster because it has special wiring (that phrasing will drive the hardware nuts wild :mrgreen: ). So CALL 16 will do exactly the same as RST 16, but ever so slightly slower (there will be no noticeable difference though).

My advice at this is it: DO NOT TRY TO UNRAVEL THESE ROM ROUTINES YET! They are a nightmare and if you're anything like me, you'll get tangled in confused, hateful knots in no time.

I'd just use them for now, understand that CLS clears the screen & sets paper colours, and RST 16 prints stuff. Even though the ROM code is a mile long, you can write your own CLS, PAPERFILL and PRINT routines in tens of bytes. We can post some if you like.

Though IMO, starting off just using the ROM routines without worrying too much about their contents is a good way to get into coding.
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

Joefish wrote: Thu Jun 25, 2020 4:21 pm This is my favourite instruction reference.
http://www.z80.info/z80code.txt

It gives you all the op-codes, the times they take to normally execute, the flags they affect and a shorthand description of what they do.

The flags thing is handy as you might do a SUB or compare or something that sets the flags according to the result (like an 'IF'), then normally you'd do something like a RET NZ or conditionaly jump straight away based on those flags (your 'THEN' action). But there are some instructions that don't affect the flags (e.g. DEC BC), so you can fit them in between your 'IF' comparison and your 'THEN' jump (to maybe tidy up a pointer or something) without affecting the outcome.

e.g. you might want to do CP (HL) // RET Z to exit your function if a comparison fails. But maybe you still want to INC HL, whether you find a match or not. So according to that table of instructions, you can actually do CP (HL) // INC HL // RET Z because that INC HL doesn't alter the flags between the CP and the RET Z.

Also worth noting the differences between all the bitwise shift and rotate instructions. e.g. RRC is 'Rotate Right Circular', which just shuffles the 8 bits along and wraps the right bit back round to the left. Whereas RR is 'Rotate Right (with Carry)' (not confusing at all :lol: ) which uses the 'Carry' flag as a ninth bit in the rotation, so you can daisy-chain the bit you rotate out of one byte and into the carry flag, then from the carry flag into the next byte you use RR on.
Thanks for the useful info. I will be reading all these threads back as I continue my learning, and hopefully will understand more soon. I understand most of what you've said here, except for the '//' parts. But I have plenty of time on my hands in evenings now and much will to learn :D
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

R-Tape wrote: Thu Jun 25, 2020 4:22 pm
chilledgamer wrote: Thu Jun 25, 2020 3:50 pm Seems insane that after around 40 years there is not a table of detailed description, in order of all functions that exist in memory
There are quite a few ROM disassemblies out there. Here's one (I put it on my own webspace. I think it's by Geoff Wearmouth, but I couldn't find his link. Am I allowed to do this?) If you search this text file for "0DAF" (3503 in hexadecimal), you'll see the CLS routine (or you can look at the memory address in your emulator debugger).

RST 16 is the same, it's listed under "0010" (16 in hex). 'RST' is basically a CALL, but faster because it has special wiring (that phrasing will drive the hardware nuts wild :mrgreen: ). So CALL 16 will do exactly the same as RST 16, but ever so slightly slower (there will be no noticeable difference though).

My advice at this is it: DO NOT TRY TO UNRAVEL THESE ROM ROUTINES YET! They are a nightmare and if you're anything like me, you'll get tangled in confused, hateful knots in no time.

I'd just use them for now, understand that CLS clears the screen & sets paper colours, and RST 16 prints stuff. Even though the ROM code is a mile long, you can write your own CLS, PAPERFILL and PRINT routines in tens of bytes. We can post some if you like.

Though IMO, starting off just using the ROM routines without worrying too much about their contents is a good way to get into coding.

This is really helpful to me thanks!!!! Could I ask about RST 16 again. In the example (im sure your aware of it) he uses RST 16 to print, i get that no problem. But later he 'sets' the xy position of the character for player. After each set of accumulator, he does RST 16 with comment "set the player x coord". Nothing is printed with this action i assume?

code snippet:

Code: Select all

basexy 
	ld a,22 ; AT code.
	rst 16
	ld a,(plx) ; player vertical coord.
	rst 16 ; set vertical position of player.
	ld a,(ply) ; player's horizontal position.
	rst 16 ; set the horizontal coord.
	ret
; Show player at current print position.
splayr 
	ld a,69 ; cyan ink (5) on black paper (0),
; bright (64).
	ld (23695),a ; set our temporary screen colours.
	ld a,144 ; ASCII code for User Defined Graphic 'A'.
	rst 16 ; draw player.
	ret
Last edited by chilledgamer on Thu Jun 25, 2020 4:30 pm, edited 1 time in total.
User avatar
Joefish
Rick Dangerous
Posts: 2042
Joined: Tue Nov 14, 2017 10:26 am

Re: More Assembly/Spectrum confusion for me :D

Post by Joefish »

chilledgamer wrote: Thu Jun 25, 2020 4:20 pmStill stuck on exactly why rst 16 is used in the example, and also my main challenge right at this moment is just creating a loop (in my own project) so that I can control when it ends. I'll have to try again after work :|
Well, according to my favourite table, here's what those instructions do. RST 16 is RST 10H:

Code: Select all

RST 0          1  C7              11     ------  -SP,-SP,[SP+1,SP]=PC,PC=00
RST 8H         1  CF              11     ------  -SP,-SP,[SP+1,SP]=PC,PC=08
RST 10H        1  D7              11     ------  -SP,-SP,[SP+1,SP]=PC,PC=10
RST 18H        1  DF              11     ------  -SP,-SP,[SP+1,SP]=PC,PC=18
RST 20H        1  E7              11     ------  -SP,-SP,[SP+1,SP]=PC,PC=20
RST 28H        1  EF              11     ------  -SP,-SP,[SP+1,SP]=PC,PC=28
RST 30H        1  F7              11     ------  -SP,-SP,[SP+1,SP]=PC,PC=30
RST 38H        1  FF              11     ------  -SP,-SP,[SP+1,SP]=PC,PC=38
What they do is make calls to functions in the ROM. The current address is saved on the stack (for a RETurn) then they just jump to the given address. If they're not in the ROM by design, then whoever sets up the system should pre-program some jumps in at the right place to catch these.

They're designed to make it easy to call operating system functions that may move around with different versions of the operating system. So you PUSH some instruction data onto the stack to say exactly what function you want and what you want it to do, then call the relevant 'RST' function.

Look at the first 11 entries under the ROM disassembly from 'Skoolkit':
https://skoolkid.github.io/rom/maps/all.html
e.g. at address $10 (16 in decimal) is a JUMP that takes you to the character print routine. You could call it directly at its address in ROM, but RST 16 is a lot simpler.
User avatar
Joefish
Rick Dangerous
Posts: 2042
Joined: Tue Nov 14, 2017 10:26 am

Re: More Assembly/Spectrum confusion for me :D

Post by Joefish »

chilledgamer wrote: Thu Jun 25, 2020 4:25 pmI understand most of what you've said here, except for the '//' parts.
That's where I couldn't be @rsed to type out each instruction on a separate line!!! :lol:
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

haha JoeFish, I thought so but wasnt sure!

Everyone thanks again for the help. I have to go work in 2 mins :[ Hence I cannot reply. But I will get home and get my head in the books again. After that I bet I will be here with more stupid questions :D
User avatar
Joefish
Rick Dangerous
Posts: 2042
Joined: Tue Nov 14, 2017 10:26 am

Re: More Assembly/Spectrum confusion for me :D

Post by Joefish »

Not sure what that print routine might be doing, but from BASIC you can PRINT special control characters that temporarily alter the INK and PAPER colour, and relocate the cursor. So I presume you can send these codes through the RST 16 print routine and it will happily obey.

That it wastes time with this sort of c@rp is another good reason for writing your own PRINT routine!

Here's a CLS example that you just give it the attribute you want the screen cleared to in 'A'. It's not the fastest in the world but it's pretty short and simple. You write one byte then use LDIR to copy it loads of times. Once to clear all the pixels, then again to set all the attributes.

Note it's handy to have the sort of constant definitions I give at the start of your code (or in an 'included' file) to save you having to remember all the relevant numbers.

Code: Select all

SCREEN        EQU 16384
SCREEN_SIZE   EQU 6144
ATTRS         EQU 22528
ATTRS_SIZE    EQU 768

LD HL,SCREEN
LD DE,SCREEN+1
LD BC,SCREEN_SIZE-1
LD (HL),0
LDIR
INC HL
INC DE
LD (HL),A
LD BC,ATTRS_SIZE-1
LDIR
+3code

Re: More Assembly/Spectrum confusion for me :D

Post by +3code »

If you need the 128k ROM, they are too in the net:

http://www.fruitcake.plus.com/Sinclair/ ... sembly.htm
AndyC
Dynamite Dan
Posts: 1388
Joined: Mon Nov 13, 2017 5:12 am

Re: More Assembly/Spectrum confusion for me :D

Post by AndyC »

chilledgamer wrote: Thu Jun 25, 2020 4:28 pm
R-Tape wrote: Thu Jun 25, 2020 4:22 pm There are quite a few ROM disassemblies out there. Here's one (I put it on my own webspace. I think it's by Geoff Wearmouth, but I couldn't find his link. Am I allowed to do this?) If you search this text file for "0DAF" (3503 in hexadecimal), you'll see the CLS routine (or you can look at the memory address in your emulator debugger).

RST 16 is the same, it's listed under "0010" (16 in hex). 'RST' is basically a CALL, but faster because it has special wiring (that phrasing will drive the hardware nuts wild :mrgreen: ). So CALL 16 will do exactly the same as RST 16, but ever so slightly slower (there will be no noticeable difference though).

My advice at this is it: DO NOT TRY TO UNRAVEL THESE ROM ROUTINES YET! They are a nightmare and if you're anything like me, you'll get tangled in confused, hateful knots in no time.

I'd just use them for now, understand that CLS clears the screen & sets paper colours, and RST 16 prints stuff. Even though the ROM code is a mile long, you can write your own CLS, PAPERFILL and PRINT routines in tens of bytes. We can post some if you like.

Though IMO, starting off just using the ROM routines without worrying too much about their contents is a good way to get into coding.

This is really helpful to me thanks!!!! Could I ask about RST 16 again. In the example (im sure your aware of it) he uses RST 16 to print, i get that no problem. But later he 'sets' the xy position of the character for player. After each set of accumulator, he does RST 16 with comment "set the player x coord". Nothing is printed with this action i assume?

code snippet:

Code: Select all

basexy 
	ld a,22 ; AT code.
	rst 16
	ld a,(plx) ; player vertical coord.
	rst 16 ; set vertical position of player.
	ld a,(ply) ; player's horizontal position.
	rst 16 ; set the horizontal coord.
	ret
; Show player at current print position.
splayr 
	ld a,69 ; cyan ink (5) on black paper (0),
; bright (64).
	ld (23695),a ; set our temporary screen colours.
	ld a,144 ; ASCII code for User Defined Graphic 'A'.
	rst 16 ; draw player.
	ret
The BASIC Print command accepts a number of control codes that can be embedded within a string to do things like move the cursor position, change the current INK or PAPER etc. These control codes are documented in the manual.

Internally, this is handled by the RST 16 routine, so passing appropriate control code sequences to that will have the same effect as in BASIC. This is one of the reasons that the code behind it is incredibly complex and also not terribly quick.
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: More Assembly/Spectrum confusion for me :D

Post by R-Tape »

chilledgamer wrote: Thu Jun 25, 2020 4:28 pm This is really helpful to me thanks!!!! Could I ask about RST 16 again. In the example (im sure your aware of it) he uses RST 16 to print, i get that no problem. But later he 'sets' the xy position of the character for player. After each set of accumulator, he does RST 16 with comment "set the player x coord". Nothing is printed with this action i assume?
Yep not every CALL/RST 16 prints a character.

If you LD A with numbers 32 - 127, RST 16 prints the corresponding ASCII), 128 - 165 is the blocks & then UDGS. So this code will print all the ASCII set and UDGS:

Code: Select all

org 32768
	ld a,2
	call 5633
	ld a,32
loop:	push af
	rst 16
	pop af
	inc a
	cp 165
	jr c,loop
	ret
Numbers outside of this range do different things, and some don't do anything. So in your example, RST 22 is the 'PRINT AT' code that tells it that the following two bytes will be the Y & X coords to print at.

Some other useful ones are:

LD A,16 ;INK colour
RST 16 ;it expects to next byte to be a viable colour 0-7
LD A,2 ;red = 2
RST 16 ;anything printed from now will be red

Others:
17 (does PAPER 0 -7)
18 (FLASH 0 or 1)
19 BRIGHT (0 or 1)

Have you got the BASIC manual? This info is listed on p183, "The character set"
User avatar
chilledgamer
Dizzy
Posts: 63
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: More Assembly/Spectrum confusion for me :D

Post by chilledgamer »

Thank you all. I wangled the night off work haha :D I understand much better now about RST , which has helped me somewhat understand the whole thing a little better also.
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: More Assembly/Spectrum confusion for me :D

Post by R-Tape »

chilledgamer wrote: Thu Jun 25, 2020 5:52 pm Thank you all. I wangled the night off work haha :D I understand much better now about RST , which has helped me somewhat understand the whole thing a little better also.
You have a very understanding boss!

Mr Pargeter? I'm afraid I can't come to work tonight; I need to unravel the convoluted wonders of the RST 16 instruction on the ZX Spectrum.
Post Reply