Escape from a call

By Daemos

Paragon (2044)

Daemos's picture

24-01-2014, 16:08

When you return from call you always return at the adress you started from but sometimes this is inconvinient. In cases where there are calls inside calls and you want to return from there.

So for example.

call test

test:
ld a,(value)
dec a
ret z

now we return back from where we started if ret z would be jp z I would get myself in big trouble with stack overflows. There has to be another convinient way to escape a call.

So basicly I want to turn a call into a jump when some conditions are met within the call:

test:
ld a,(value)
dec a
special jump out of here z, adress

I find the problem difficult to explain so bear with the vagueness of the story please.

Login or register to post comments

By msd

Paragon (1510)

msd's picture

24-01-2014, 16:19

Just remove the return address from the stack with a pop hl.

By anonymous

incognito ergo sum (116)

anonymous's picture

24-01-2014, 16:33

or a "pop null": inc sp \ inc sp
this is faster on R800. On Z80 it's slower than a normal pop, but at least you don't destroy any registers this way.

Generally speaking though, it's better to avoid situations like this completely. Because if you're not carefull, you could end up with hard to find bugs.

By flyguille

Prophet (3031)

flyguille's picture

24-01-2014, 16:56

chained escapes conditions like, an error trap. Is better to be handled trough a flag . By example 'scf' before the 'ret z' , and in the calling code 'ret c' after the call.

By Daemos

Paragon (2044)

Daemos's picture

24-01-2014, 17:11

Quote:

chained escapes conditions like, an error trap. Is better to be handled trough a flag . By example 'scf' before the 'ret z' , and in the calling code 'ret c' after the call.

thats the way I am doing it now and by seeing the above comments I think I will keep it that way. Thank you all for the usefull responses.

By NYYRIKKI

Enlighted (6016)

NYYRIKKI's picture

25-01-2014, 00:05

Remeber that "CALL x" is only "PUSH PC, JP x"

If your stack is completely messed up, then you might take approach similar to this one:



	LD A,(XXXX)
	LD (RETURN),SP
	CALL MYROUTINE
FAIL:
	LD SP,0
RETURN:	EQU $-2
	RET

MYROUTINE:
	PUSH AF
	{do something}
	POP AF
	INC A
	CP 13
	CALL C,MYROUTINE

	CP 100
	JP NC,FAIL	; Too many recursions!
	{do things}
	RET