Assembly Language Help

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
MatGubbins
Dynamite Dan
Posts: 1239
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Assembly Language Help

Post by MatGubbins »

Yup, glad that you've taken the plunge into the deep depths of machine code and swimming to the surface. You'll have fun, you'll have late nights of decoding something, you'll have that evening of hitting a wall/shouting at the screen because of a stupid coding mistake (missing out a Z from a JP Z,nn or something similar), but you'll have the satisfaction of everything coming together and working how you want it to be, the enjoyment of re-writing the code to be faster and smaller, finding new ways to get a routine working and laughing at your own old code from a few weeks earlier that you thought was oh-so great but now you would quite happily bury it under the patio with next doors cat.

Enjoy!
User avatar
PeterJ
Site Admin
Posts: 6873
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Assembly Language Help

Post by PeterJ »

Thanks for all the comments everyone.

Another question if I may. If I use Call label-name, then from that have another call to a different label-name, and Ret at the end of each label does the Z80 keep track and return to after the last call instruction issued?

So I suppose I'm asking is it OK to have a call to a subroutine within another subroutine?

Thanks
User avatar
R-Tape
Site Admin
Posts: 6402
Joined: Thu Nov 09, 2017 11:46 am

Re: Assembly Language Help

Post by R-Tape »

PeterJ wrote: Wed May 02, 2018 4:52 pm Thanks for all the comments everyone.

Another question if I may. If I use Call label-name, then from that have another call to a different label-name, and Ret at the end of each label does the Z80 keep track and return to after the last call instruction issued?

So I suppose I'm asking is it OK to have a call to a subroutine within another subroutine?

Thanks
That's most definitely okay, and you will struggle to make complex programs otherwise.

When you CALL a subroutine the 16 bit address to RETurn to is automatically PUSHed on the stack so the stack builds downwards by two bytes every time. CALL another one and it builds down again. You can just keep doing this as long as the stack doesn't get too big and write over your program.

So this is perfectly fine:

Code: Select all

sub1:	call sub2	;address r1 pushed onto the stack
r1:	ret
	;
sub2:	call sub3	;address r2 pushed onto the stack
r2:	ret
	;
sub3:	call sub4	;address r3 pushed onto the stack
r3:	ret
	;
sub4:	call sub5	;address r4 pushed onto the stack
r4:	ret
	;
sub5	do stuff*
	ret

*when we are here the stack looks like this:

SP>	r4
	r3
	r2
	r1

So when we RET from sub5, we POP r4 and the stack reduces by two, we return to r4, the next RET to r3 and so on.
It's the same as PUSH and POP, you need to keep an eye on what your last CALL was so you know where you return to, it has to balance (unless you're doing trickery which I don't recommend yet).

This can cause a LOT of problems early on, but keeping the stack balanced becomes second nature sooner or later. The books do a decent job of explaining, but in my case I only fully 'got' it after trial and error.
Post Reply