SubRoutine Returning 
Author Message
 SubRoutine Returning

Hello...

  I need some help on making my subroutines return properly. I lay my
code out in the following fashion :

;SubRoutine Code
_SubCode SEGMENT Para PRIVATE 'Code'

;External Subroutines HERE
EXTRN OutSide : PROC

;Internal Subroutines HERE
InSide PROC Far
;Blah Blah
InSide ENDP
_SubCode ENDS

;Core Program Code
_MainCode SEGMENT Para PRIVATE 'Code
MainProg:
Call InSide
Call OutSide
;Terminate Sequence
_MainCode ENDS
End MainProg

  The calls to 'InSide' return properly. The calls to 'OutSide' are the
problem. Question One : Do the externaly linked subroutines in the
_SubCode segments need to be declared as FAR? And if so, do I declare
them in _SubCode as EXTRN OutSide : PROC or EXTRN OutSide : FAR? If I
pass parameters to either the internal or the external subroutines,
neither of them return properly. I interface the parameters using the ARG
directive and return using RETF N, with N being the size of my parameter
block. Question Two : Is there some important concept I've forgotten? How
do I pass parameters and return from both the internal and external
subroutines?

  Any help would be greatly appreciated.

- Vea



Thu, 24 Dec 1998 03:00:00 GMT  
 SubRoutine Returning

Vea

declare the external proc

extrn OutSide:FAR

The call will then push cs if its in the same segment or load it if its
in a different segment. You appear to be using tasm so the arg value
should work.

proc inside far
        Arg foo:word

        push    bp
        mov     bp,sp
        Blah.....
        pop     bp
        retf
inside endp

There should be no problem with this code.
Personaly I prefer to declare externals with the global keyword as this
not only checks the call statement but also the proc declaration.
Brian



Sat, 26 Dec 1998 03:00:00 GMT  
 SubRoutine Returning

I've since been helped in making my routine at least return to the
program and the parameters are now passing smoothly INTO the routine. I
am curious though how parameters can be returned to the caller. The
User's Manual (I'm using an older TASM 1.01) isn't clear at all on how to
read return parameters. I can already do it with global variables and
register passing but want to know how it's done with the stack. The
closest the User's Manual gets is a vague example that returns values to
Pascal, but never documented how Pascal reads them back.

  My guess would be to increment the stack pointer (after returning) back
where it would have been in the routine; before popping the CS:IP and BP
(I use far calls) in the return. I believe my parameters should still be
sitting there. Can someone tell me if I'm right?

- Vea



Sat, 26 Dec 1998 03:00:00 GMT  
 SubRoutine Returning

Quote:

>I've since been helped in making my routine at least return to the
>program and the parameters are now passing smoothly INTO the routine. I
>am curious though how parameters can be returned to the caller. The
>User's Manual (I'm using an older TASM 1.01) isn't clear at all on how to
>read return parameters. I can already do it with global variables and
>register passing but want to know how it's done with the stack. The
>closest the User's Manual gets is a vague example that returns values to
>Pascal, but never documented how Pascal reads them back.
>  My guess would be to increment the stack pointer (after returning) back
>where it would have been in the routine; before popping the CS:IP and BP
>(I use far calls) in the return. I believe my parameters should still be
>sitting there. Can someone tell me if I'm right?

In C you normally pass values back in registers.  In Pascal I believe
the compiler reserves space on the stack for the return value... you
put your value there (probably using some offset from BP) and then the
compiler will pick it up after you exit.  i would ask in a pascal or
turbo-pascal group; they probably know the specifics.  Alternately,
your PASCAL compiler can probably generate assembly language sources...
write a couple of simple functions which return values and see what the
compiler does with them.  Then do the same thing in your assembly code...

David

Quote:
>- Vea



Sat, 26 Dec 1998 03:00:00 GMT  
 SubRoutine Returning

Quote:


> >I've since been helped in making my routine at least return to the
> >program and the parameters are now passing smoothly INTO the routine. I
> >am curious though how parameters can be returned to the caller. The
> >User's Manual (I'm using an older TASM 1.01) isn't clear at all on how to
> >read return parameters. I can already do it with global variables and
> >register passing but want to know how it's done with the stack. The
> >closest the User's Manual gets is a vague example that returns values to
> >Pascal, but never documented how Pascal reads them back.

> >  My guess would be to increment the stack pointer (after returning) back
> >where it would have been in the routine; before popping the CS:IP and BP
> >(I use far calls) in the return. I believe my parameters should still be
> >sitting there. Can someone tell me if I'm right?

> In C you normally pass values back in registers.  In Pascal I believe
> the compiler reserves space on the stack for the return value... you
> put your value there (probably using some offset from BP) and then the
> compiler will pick it up after you exit.  i would ask in a pascal or
> turbo-pascal group; they probably know the specifics.  Alternately,
> your PASCAL compiler can probably generate assembly language sources...
> write a couple of simple functions which return values and see what the
> compiler does with them.  Then do the same thing in your assembly code...

In 16-bit C a near pointer is passed back in AX, and a far pointer is passed back in DX:AX.
In 32-bit C a pointer is passed back in EAX.  

Aside from pointers, all values are either passed back in (E)AX or in (E)DX:(E)AX depending
on the size of the value.



Sat, 26 Dec 1998 03:00:00 GMT  
 SubRoutine Returning


Quote:

>I've since been helped in making my routine at least return to the
>program and the parameters are now passing smoothly INTO the routine.
>I
>am curious though how parameters can be returned to the caller. The
>User's Manual (I'm using an older TASM 1.01) isn't clear at all on how
> to
>read return parameters. I can already do it with global variables and
>register passing but want to know how it's done with the stack. The
>closest the User's Manual gets is a vague example that returns values
>to
>Pascal, but never documented how Pascal reads them back.

>  My guess would be to increment the stack pointer (after returning) b
>ack
>where it would have been in the routine; before popping the CS:IP and
>BP
>(I use far calls) in the return. I believe my parameters should still
>be
>sitting there. Can someone tell me if I'm right?

>- Vea

If I understand you correctly, the best way I can explain it is an
example.

this is a QuickBasic4.5 example:

CALL ABSOLUTE(xN%,...,x2%,x1%,SADD(Program$))

Some languages use a FORWARD or REVERSE order...

FORWARD:        x1%,x2%,...,xN%,ProgramSegment%

REVERSE:        xN%,...,x2%,x1%,ProgramSegment%

'Passed Variables x1%,x2%,xN%
x1%=[bp+6]
x2%=[bp+8]
xN%=[bp+{(2*index#)+4}]

'Assembly Program:

'preserve STACK
        push    bp
        mov     bp,sp
        push    ax
        push    bx
        push    cx
        push    dx
        push    si
        push    di
        push    ds

        {your stuff}    

'restore STACK
        pop ds
        pop di
        pop si
        pop dx
        pop cx
        pop bx
        pop ax
        mov sp,bp
        pop bp

'Return to CALLING program
        ret far + {index# * 2}

Note:
        index# - the number of individual variables you are PASSING
                 to and from the Assembly program.

Beau Schwabe



Sun, 27 Dec 1998 03:00:00 GMT  
 SubRoutine Returning

Hi Vea,

        Usually a function only returns one value, if its a char or int
then its in ax, longs in ax and dx etc. If you wish to pass a parameter
to a function and read whats happened to it then the way to do it is to
pass the address of the variable, so when you return from the call the
variable will still be at the same address but modified. This type of
programming is called using pointers.

Example

proc outside
        arg foo:word,fa:word

        enter 0,0       ; set up stack for the variables
        push    foo     ; the values of foo and fa
        push    fa
        call    routine
        add     sp,4
        lea     ax,foo
        push    ax
        push    ss              ; the addresses are offset from the ss
        lea     ax,fa
        push    ax
        push    ss
        call    routine2        ; this routine now uses the addresses
        add     sp,4
        leave
        ret
outside endp

proc routine2
        arg  add1:dword,add2:dword

        les     bx,add1
        mov     ax,es:[bx]      ; ax now is equal to fa
        inc     es:[bx]         ; fa has now changed in outside
        ret
routine2 endp

; All errors in the coding are mine (and I make a lot, but I'm good at
debugging).
Brian



Mon, 28 Dec 1998 03:00:00 GMT  
 SubRoutine Returning


[snip]

Quote:
>        lea     ax,foo
>        push    ax
>        push    ss              ; the addresses are offset from the ss
>        lea     ax,fa
>        push    ax
>        push    ss
>        call    routine2        ; this routine now uses the addresses
>        add     sp,4

;   Shouldn't the above statement be:

         Add    sp,8            ; because you have pushed 4 WORDS?

Quote:
>        leave
>        ret
>outside endp
>; All errors in the coding are mine (and I make a lot, but I'm good at
>debugging).

Brian-

   Thats one you won't have to find, I found it for you.  :)

   Jim Neil



Mon, 28 Dec 1998 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. SubRoutine Returning

2. Return value from awk snipet via korn shell subroutine

3. subroutines that return variable #'s of results

4. subroutines that return variable #'s of results

5. allocate arrays in subroutine and return it to main

6. Unused return arguments in subroutine

7. IVF odd behaviour: subroutine vs function returning floating point number

8. Vax Fortran calling C subroutine, need to return a character string

9. Return statement in subroutines

10. Passing Complex array to FOTRAN subroutine from C and get return

11. Fortran subroutine to return #second

12. Write statement in subroutine affecting output of return values

 

 
Powered by phpBB® Forum Software