[comp.sys.apple2] Orca/C & M Questions

2hnemarrow@kuhub.cc.ukans.edu (05/21/91)

Another question:
I am trying to make Orca/M talk to Orca/C.  What do I do to C to make this
work.  Specifically, how do I make C call a subroutine x in a library y written
in Orca/M.   The ML part is ok, I'm sure.  

Note:
1.  Yes, I have the manuals.
2.  Yes, I have read them.
3.  No, they weren't very helpful.
4.  Assuming we are passing parameters.
5.  Yes, I know I need header files and all that stuff.
6.  I need specific examples.

Thanks.
--
               <|
                |
               / \
          <|  ,| |,<| 
    <|     |   | |  | <|
<|   |    / \/'   |/ \ |  <|
 |  / \ /\|        | |/ \  |
/\ _|  /    _--_         \/ \
|          /    \           |
|_________|      |__________|
2hnemarrow@kuhub.cc.ukans.edu 

2hnemarrow@kuhub.cc.ukans.edu (05/22/91)

In article <1991May21.202948.3268@ux1.cso.uiuc.edu>, jb10320@uxa.cso.uiuc.edu (Jawaid Bazyar) writes:
[everything cut out]

None of that helped much.  Like I said, I'm using an ML library.  I've done
this before, but I can't seem to repeat it.  What I have now is the following:

Header file (sample.h):

#ifndef _sample_
#define _sample_

extern void PROCNAME (void);
#endif

C program:

#include <sample.h>
    * * *
PROCNAME ();
    * * * 

I have no problems with the ML part.  It's C I've had little experience with.

-- 
               <|
                |
               / \
          <|  ,| |,<| 
    <|     |   | |  | <|
<|   |    / \/'   |/ \ |  <|
 |  / \ /\|        | |/ \  |
/\ _|  /    _--_         \/ \
|          /    \           |
|_________|      |__________|
2hnemarrow@kuhub.cc.ukans.edu 

jb10320@uxa.cso.uiuc.edu (Jawaid Bazyar) (05/22/91)

2hnemarrow@kuhub.cc.ukans.edu writes:

>Another question:
>I am trying to make Orca/M talk to Orca/C.  What do I do to C to make this
>work.  Specifically, how do I make C call a subroutine x in a library y written
>in Orca/M.   The ML part is ok, I'm sure.  

>Note:
>1.  Yes, I have the manuals.
>2.  Yes, I have read them.
>3.  No, they weren't very helpful.

   They made perfect sense to me (of course I'm just a nerd, what can I say?)

>4.  Assuming we are passing parameters.
>5.  Yes, I know I need header files and all that stuff.
>6.  I need specific examples.

  It's not so much what you have to do to C, but what you have to do to your
assembly routine. There are two things you can do.

1) set up your Orca/M routine to look just like a C function.
2) just jsl to the Orca/M routine (can't pass parameters though)

So, since you said we're passing parameters, you need to do something like
the following.  You've got the Orca/C manual, so open up and follow along :-)

The parameters to the routine are pushed onto the stack.  (Page 67
for the Orca/C 1.1 version manual).  After the parameters are pushed,
a JSL is made to the routine.  According to the diagram, you parameters
start at stack offset 4. 
Now to make you code work a little better, we'll set the direct page to
point to your parameters.  
    tsc		put stack pointer in accumulator
    phd		save old value of direct page
    tcd		set direct page to accum (i.e., the stack)

So now 
	lda [4] 
will get the value stored at the pointer Z, and 
	lda 8
will get the value of the parameter 'a'.  

One thing that might be tripping you up (depending on the version of the
assembler, I think) is that the assembler converts all it's labels to
all uppercase.  So the C example on page 68 should actually read

extern int REVERSE(int parm);

It really is pretty simple.  If you have further questions, just ask!
(Now writing a Tool Set can get hairy...)

--
Jawaid Bazyar               |  "Twenty seven faces- with their eyes turned to
Graduated!/Comp Engineering |    the sky. I have got a camera, and an airtight
bazyar@cs.uiuc.edu          |     alibi.."
   Apple II Forever!        |  I need a job... Be privileged to pay me! :-)

meekins@anaconda.cis.ohio-state.edu (Tim Meekins) (05/22/91)

In article <1991May21.202948.3268@ux1.cso.uiuc.edu> jb10320@uxa.cso.uiuc.edu (Jawaid Bazyar) writes:
>2hnemarrow@kuhub.cc.ukans.edu writes:
>
>>Another question:
>>I am trying to make Orca/M talk to Orca/C.  What do I do to C to make this
>>work.  Specifically, how do I make C call a subroutine x in a library y written
>>in Orca/M.   The ML part is ok, I'm sure.  
>
>One thing that might be tripping you up (depending on the version of the
>assembler, I think) is that the assembler converts all it's labels to
>all uppercase.  So the C example on page 68 should actually read
>
>extern int REVERSE(int parm);


Ohhhh, eeeek! Jawaid, take a look at those C -> 65816 conversions I did. Note
the "case on" statement. In laymen's terms, place a "case on" in your assembly
code to make the labels case sensitive.


If the parameter passing scares you, get a macro to do the job. Orca/Pascal
comes with macros parameter passing and my Zavtra Macros Disk also contains
macros for parameter passing. Mine uses the same syntax as Mike's. Put
the following in your code:

localvar1	gequ	0
localvar2	gequ	2
space		gequ	4

		subroutine (4:parm1,2:parm2),space


A stack frame is now automatically set up for a paramter of 4 bytes and a 
second with 2 bytes. 4 Bytes are also reserved on the stack for local 
variables.


		return

Generates the proper code to return back. Function return values are in the
A register if 2 bytes, and (if memory serves) X & A or Y & A for 4 bytes.
--
++------------S-U-P-P-O-R-T---S-H-A-R-E-W-A-R-E---O-R---D-I-E-!-----------++
|| Tim Meekins                   ||  Snail Mail:           ||   Apple II  || 
||   meekins@cis.ohio-state.edu  ||    8372 Morris Rd.     ||   Forever!  ||
\\___timm@pro-tcc.cts.com________/\____Hilliard, OH 43026__/\_____________//

fadden@cory.Berkeley.EDU (Andy McFadden) (05/22/91)

In article <1991May21.170310.30961@kuhub.cc.ukans.edu> 2hnemarrow@kuhub.cc.ukans.edu writes:
>None of that helped much.  Like I said, I'm using an ML library.  I've done
>this before, but I can't seem to repeat it.  What I have now is the following:
[snip]
>I have no problems with the ML part.  It's C I've had little experience with.

The generic approach:

1) Write the C routines.
2) Decide what your assembly code should do, and then write a C function
   that looks like this:

foo(arg1, arg2, arg3)
int arg1, arg2;
long arg3;			(substitute appropriate args/types)
{
    arg1 = 1;
    arg2 = 2;
    arg3 = 3L;
}

3) Compile it.
4) DUMPOBJ +D foo.root
5) Copy down all the TSC and SBC stuff at the start and end, and write
   down which DP locations are used by which variables (you can tell by
   looking at the assignment statements).  Oh, you may also want to slide
   some temp variables in there... just make some local variables and
   assign stuff to them so you can get their addresses.
6) Create the ML subroutine, with OBJCASE ON.  Type in all the TSC
   stuff for the prolog and epilog, and use EQU for the arguments and/or
   local variables.  You're set.
7) Return values are in the accumulator (or Acc/X-reg for long words)... at
   least that's how APW C works.

I do this all the time in APW C...

When you say you are "using an ML library", it sounds like you have a bunch
of routines that are already written, and want to call them from C.  If
you want to pass arguments, they have to follow C or Pascal calling conventions
(declaring the ML routine as, for example, "extern pascal int foo()" for
Pascal... the same set of stuff applies for Pascal routines, but the arguments
will be handled differently (only your compiler knows for sure :-) )).

That's all I can say based on what you've told us.  You'll have to be more
specific if this isn't working (i.e., what errors are you getting?  Where
are things failing?  Does it compile?  Link?  Execute incorrectly?)

>               <|
>                |
>               / \
>          <|  ,| |,<| 
>    <|     |   | |  | <|
><|   |    / \/'   |/ \ |  <|
> |  / \ /\|        | |/ \  |
>/\ _|  /    _--_         \/ \
>|          /    \           |
>|_________|      |__________|

Lemme guess... King Lerch's castle?  ;-}

>2hnemarrow@kuhub.cc.ukans.edu 

-- 
fadden@cory.berkeley.edu (Andy McFadden)
..!ucbvax!cory!fadden
fadden@hermes.berkeley.edu (when cory throws up)

dat33228@uxa.cso.uiuc.edu (Derek A. Taubert) (05/22/91)

2hnemarrow@kuhub.cc.ukans.edu writes:

>Another question:
>I am trying to make Orca/M talk to Orca/C.  What do I do to C to make this
>work.  Specifically, how do I make C call a subroutine x in a library y written
>in Orca/M.   The ML part is ok, I'm sure.  

orca/m makes everything uppercase.  so, when you 

extern asmcode();

make it

extern ASMCODE();

instead.
--
+ Derek Taubert --> derek@mrcnext.cso.uiuc.edu + Author of : GScii+	      +
+		    dat33228@uxa.cso.uiuc.edu  + and the world's most useless +
++++++++++++++++++++++++++++++++++++++++++++++++ desk accessory -> Amaze me   +
+ There are MOUSE technotes?    +  *******8-)  ++++++++++++++++++++++++++++++++
+ Ask me about my GS load meter +  ^^^^^^^^^^ Marge Simpson                   +