Hi,
I'm new to this board since the last one closed.

Here's what I need to know:

I'm calling a procedure from another and then call one more and need to return to the first:

Proc1 PROC
call Proc2
;need to return here
ret
Proc1 endp

Proc2 PROC
call Proc3
ret
Proc2 endp

Proc3 PROC
;need to return to Proc1
Proc3 endp

Thanks,
goofee
Posted on 2002-02-20 09:18:32 by goofee
goofee,

With the way you have written your procs, you either put a RET in the third one so it returns back through the earlier two or rewrite the earlier two so that the stack is balanced. Its easy enough to jump back to the first but you miss a RET or two on the way back which leaves you with an unbalanced stack.

I would be inclined to redesign the proc layout so you get the instruction sequence another way if using a RET in proc3 does not satisfy what you are after.

Regards,

hutch@movsd.com
Posted on 2002-02-20 09:26:18 by hutch--
If all the procs have stack frames then you could do an extra mov esp,ebp/pop ebp to back up a frame, but this is fairly crapping coding (ie without heavy documentation, your going to have to pause to understand what is going on.)
Posted on 2002-02-20 09:54:39 by bitRAKE
Use jumps instead of calls to get to the second and subsequent functions, that way you will have the correct return address on the stack when you do a RET.
Posted on 2002-02-20 17:33:11 by sluggy

Hi,
I'm new to this board since the last one closed.

Here's what I need to know:

I'm calling a procedure from another and then call one more and need to return to the first:

Proc1 PROC
call Proc2
;need to return here
ret
Proc1 endp

Proc2 PROC
call Proc3
ret
Proc2 endp

Proc3 PROC
;need to return to Proc1
Proc3 endp

Thanks,
goofee


If it's just that that you need, then in Proc3 you need to do:


ADD ESP,4
RET
The first instruction will dischard the return address for Proc2, so that your next RET instruction returns to where Proc2 would have returned (i.e. Proc1).
Note that this is not good programming style though.. since if you someday modify your procs to pass parameters on the stack, you may easily forget of this "trick" and crash crash crash. ;)

A better and more general method thus becomes the follow (which you may use, for example, for some simple kind of starting_to_be_serious error/exception handling):

in Proc1:



Proc1 PROC
MOV [SAVED_ESP],ESP ;let's save ESP to a static variable (i.e. SAVED_ESP must NOT be on the stack)
CALL Proc2
I_pretend_to_come_back_here: ;this is a label, we may want to return here from anywhere in the program, regardless of stack state
RET
Proc1 ENDP



Proc2 PROC
CALL Proc3
RET
Proc2 ENDP



Proc3 PROC
;need to return to Proc1, so we first fix ESP, and then jump wherever we want
MOV ESP,[SAVED_ESP]
JMP I_pretend_to_come_back_here
Proc3 ENDP


Greets,
Maverick
Posted on 2002-02-20 18:16:39 by Maverick
PS.. forgot to add:


Note that this is not good programming style though.. since if you someday modify your procs to pass parameters on the stack, you may easily forget of this "trick" and crash crash crash. ;)

Even worse.. if you for example skip the Proc2 passage, and go straight from Proc1 to Proc3, then crash crash crash :grin:

If you are a beginner, please get the right/clean habits from now.. or you will regret the day that you started to write spaghetti code. :grin:
Posted on 2002-02-20 18:21:04 by Maverick
The question can not be answered untill some additional info
specified.
What the procs doing in hence of control blocks, are there params
what way are they passed etc?
In different case there would be different solutions.
After all why there is need for call jumping?
Why you could not do it with jumps?
Call and rets is nothing but jumps with additional work.
Call = push offset of next instruction and jump to specified
address
ret = jump to and if no params specified - pop esp (add esp 4) if some param specified then jump add esp,4+param
Posted on 2002-02-20 18:38:39 by The Svin
Thanks everyone.

I don't have an internet connection anymore so I'm at friends house now (took a while for a reply).

The problem is I'm calling Proc3 about 50 times from Proc2 and I want to save the code for Error checking after each return.

I got four or five dwords as parameters so for four parameters would

add esp,20
ret

be OK ?

Can I "rebalance" the stack ?
Posted on 2002-02-23 15:23:49 by goofee
Why don't you use the method I proposed you? Maybe because it's too simple and straightforward? ;)
Posted on 2002-02-23 21:44:14 by Maverick
No,
It,s fine,thanks.
Posted on 2002-02-28 08:57:36 by goofee