[comp.sys.amiga.tech] ALINK,ML,and Metacomo Pascal

Dennis_Grant%CMR001.bitnet@ugw.utcs.utoronto.ca (02/09/91)

     OK, the story so far:

     I have to write a 'shell' program for my operating systems class. It
has to perform much as a 'real' shell would except that all the tasks are
internal to the shell i.e. they are procedures. The whole idea is that one
can create, suspend, change priority, etc. of the various tasks, and watch
what happens.

     I managed to convince my prof to let me write the program on my Amiga.
He is watching this very closely, and if I do a good job, he may recommend
that the OS course be done on the Amiga, and the school could presumably
pick up a few for use by the CS dept. So I really want to pull this off.

     It's also due Monday. :(

     The problem: I don't own a C. I'm writing this in Metacomo Pascal V2.

     Up until a few days ago, everthing was running great. I opened a
NEWCON: window as my input window, I opened another window to display
output and assigned a console.device to it with 'write only' channels.
I wrote a procedure to write characters to this window. All hunky-dory.

     Now it was time for adding tasks. Each task is a Pascal procedure
that writes a character to the output window, and loops forever. Problem:
AddTask() and CreateTask() have a parameter InitialPC, which is the start
address of the task to be created. In C, this is the name of the procedure.
In Pascal, it does not exist. There is no way, using Pascal, to determine
the start address of your procedure.

     My solution: It seems that when you call a procedure, it places its
start address on a stack pointed to by A1. So if I wrote a ML program that
the procedure called, I could look at the proper place on this stack, and
store the address in a variable for later use with AddTask(). (There is a
BOOLEAN flag in the procedure to prevent it from looping indefinately the
first time it is called)

    Here's the declaration:

  FUNCTION FindAddr : INTEGER; EXTERNAL;

   This makes Pascal generate an XREF to _FindAddr, and it expects the
result in D0;

   Here's the ML program:

    XDEF _FindAddr
_FindAddr   move.l -12(a1),D0     put the address I want into D0
            move.l a1,a3          copy a1 into a scratch register
            move.l -8(a3),a1      restore the old value of a1
            jmp    -4(a3)         hop back to the main program
    END

     Now the fun begins. I assembled the ML as linkable, and export XDEFs
with debugging enabled. (OPT L+,D+,X+) using GenAm2 from Hisoft. If I link
this program with the .obj file thus produced, and run the program, nothing
happens. My shell prompt doesn't process anything, and no windows open. Zip
If I link it with FindAddr.obj in the LIBRARY section of ALINK (The
previous was linked with the .obj file in the FROM section) the program
opens the windows, and then 'Software error, task held'

    This happens no matter if the procedure gets executed or not. I
put curly braces (commented out) the offending procedure, and the same things
happened, so I don't think my ML is ever being executed. If I remove all
references to 'FindAddr.obj' from ALINK, the program runs normally, less, of
course, the function I so desperately need. Am I doing the XDEF correctly?
I have been completely unsuccessful in finding any examples of how to link ML
to a high level language ANYWHERE.

     Now here's the best part: I decided to try to debug the program on the low
level level using MonAm2. (Also part of DevPack Amiga) No matter which
version of the program I tried, running or not, it would crash on the
third instruction: jsr (a5). Why? Because a5 was = 0. According to the Pascal
manual, a5 always points to some function that updates stack frames.
If I just run the program, it works. (less tasks). If I try to run a
working version under MonAm2, it crashes.  How does the proper value
get into a5? Any other executeable I tried running under MonAm2 worked!
Huh? I don't get it!

     Anyway, I'm at the end of my rope. I've been working nonstop on this for
the last 4 days, and I'm frazzled. I tried talking to CATS, but they wouldn't
talk to me, 'cause I'm not a developer. *Sigh* If anyone has any Idea
on what I'm doing wrong, please EMAIL me as soon as you can. I'm going to go
back now, and see if I can't get ROM-WHACK to help me out. It's been a long
week.

Sorry to take up so much bandwidth, but I really need help.


-------------------------------------------------------------------------------
  Dennis Grant   3rd year CS student at Le College Militaire Royal de St-Jean
  DETUD595@CMR001.BITNET
  How much do I love thee? My accumulator overflows!

dlorre@cbmfra.UUCP (D. LORRE) (02/09/91)

In article <44138@nigel.ee.udel.edu> Dennis_Grant%CMR001.bitnet@ugw.utcs.utoronto.ca writes:
>
>     OK, the story so far:
[stuff deleted]
>
>     Now it was time for adding tasks. Each task is a Pascal procedure
>that writes a character to the output window, and loops forever. Problem:
STOP!!!! Sorry but you cannot write anything to a console window from
within a Task : you need a true Process. As you do not have much time
and so am i, I would suggest you to create as much programs as you were 
needing tasks and make them talk altogether via messages and Ports. I know
this less elegant than using task or processes but it will work and is
not very difficult to program.

Functions that could be helpful are Execute(), Signal(), ObtainSemaphore()
SendMsg() and the others ...
Or you could try to CreateProc, but the problem would remain the same.
There is a system function , FindTask(0) that give you the address of
your own task. This could be a starting point.
>
>-------------------------------------------------------------------------------
>  Dennis Grant   3rd year CS student at Le College Militaire Royal de St-Jean
>  DETUD595@CMR001.BITNET
>  How much do I love thee? My accumulator overflows!

UUCP: ... cbmvax!cbmehq!cbmfra!dlorre
Tous ces mysteres nous depassent : feignons d'en etre l'organisateur.
Jean Cocteau