TweetFollow Us on Twitter

Trace Pascal
Volume Number:5
Issue Number:1
Column Tag:Programmer's Workshop

Trace LightSpeed Pascal

By Alan Wootton, Santa Monica, CA, MacTutor Contributing Editor

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Alan Wootton currently of MTS Informix Software, Los Angeles, is a famous early contributor of MacTutor and is well known in Mac programming circles. We welcome his return with this article.

A method of obtaining an indented listing recording the execution history, and call chain, of any LightSpeed Pascal program, is presented.

Introduction

Once upon a time (in 1975) in a far away land (Arizona) there was a young man (your humble author) who went to a great place of learning (ASU) to seek his fame and fortune. While he was there he happened upon a course of study that involved the learning of a strange new numerical language (Fortran) that could be read and understood by a new breed of Machine Folk known as ‘Computers’. Unfortunately for the students in this class, it was necessary to learn to read and understand as Machine Folk, and the ways of the Machine Folk were strange indeed. This was a most tedious and difficult job. Surely only Wizards could train their brains to such a task!

Often was the day when these poor disciples would be presented with a riddle (translated here into a more moderne language for all ye moderne folk) such as:

PROBLEM SET #4 (25 points) Given the procedure: 

PROCEDURE A (i: integer);
 begin
 if i > 2 then
 begin  
 write(i);
 A(i div 2);
 A(i - i div 2);
 end;
 end;

4a) Please write the the output produced by the call:

A(123); 

Whereupon the diligent student would, after much mental effort, correctly write:

123 61 30 15 7 3 4 8 4 4 15 7 3 4 8 4 4 31 15 7 3 4 8 4 4 16 8 4 4 8 
4 4 62 31 15 7 3 4 8 4 4 16 8 4 4 8 4 4 31 15 7 3 4 8 4 4 16 8 4 4 8 
4 4

The result of such a wizardly task was the bestowal of the desirable, yet non-negotiable, commendation known as the ‘A’. Provided of course that the student correctly answered the other parts:

4b) How many times is the procedure A invoked?

4c) What is the maximum depth to which A recurses?

The answers being, of course:

{See if you can guess. Answers at the end of the article}

As you might imagine, a great call was heard throughout the land (or at least the classroom). “Why must we learn to do the work of the machine folk when it is so admirably done by those very folk”. The answer was “so that ye may all be able to recognize the errors of the machine folk”. Errors of the machine folk? Yes, the machine folk, being of simple nature, were capable of doing only as told. When those directions were the least bit imprecise then resulting machine actions could defy all gentle reason.

At that time I thought “why don’t we teach the machines to find their own mistakes?” Being a neophyte, and not knowing the nature of the machine instructions required for such a task, I did not pursue this topic any further.

Now, things are different.

A Goal

Often I am faced with the daunting task of understanding the execution of a large, complicated, and sometimes foreign, pieces of code. My tools are the writeln, the breakpoint, the observe window, and the rest of the fine features of the LightSpeed development environment. Additionally I usually code routines to monitor the correctness of difficult data structures. And recently, I have developed a method of using the ‘hooks’ that LightSpeed puts in its code to monitor the entire execution of a program.

Having the ability to monitor the execution of a program brings up several possibilities. One could stop at the end of every procedure and verify that all the data structures are intact, or that the heap is not damaged, or that an excessive amount of time has not elapsed. As space is limited in this article I shall present what I think is a most interesting utility: A method of producing an indented listing of all procedures called.

Given the procedure above we wish to obtain a listing such that the call: A(7) produces the result:

BEGIN A       
 . BEGIN A       
 .  . BEGIN A       
 .  . END   A       
 .  . BEGIN A       
 .  . END   A       
 . END   A       
 . BEGIN A       
 .  . BEGIN A       
 .  . END   A       
 .  . BEGIN A       
 .  . END   A       
 . END   A       
END   A       

From this one can immediately deduce that A is called 7 times and that A recurses 2 levels deep (do you know what is output?). While I must stick with short examples, traces of complex programs can easily exceed 1,000,000 characters. For instance, it is very hard to trace through the sources of a MacApp program and observe what the execution order is. Given a trace (The MacApp debugger will do this) in the proper format (MacApp uses a very bad format, we use a better one here) one can very easily follow the path of execution and see immediately which procedure calls another one and when.

In order to obtain the desired result two things are needed. One, a way of putting ‘hooks’ into all the procedures. To obtain the format shown above we will need a hook at the start and at the end of every procedure. One way would be to type in a procedure call at the beginning and end of every procedure. Fortunately, there is a way to get LightSpeed to do this using the debug option. Second, we need a way for the ‘hooks’ to find the name of the procedure currently executing. The names option in LightSpeed (and other compilers) embeds the names in the code. Our hook routines can find, and use, these names.

How To Work It

To trace your program follow this simple recipe:

1) Modify your program so that it Uses the unit ‘LightTrace’.

2) Add the files ‘LightTrace.p’ and ‘LightTrace.a.lib’ to your project. Turn OFF the debug (D) option for these files. Turn ON the debug option for all files you wish traced.

3) Before any of the LightTrace routines are called you should call the routine ‘InitTracing’.

4) When you wish to start tracing, call StartTracing(false, true, true);. The first parameter specifies whether you want output in the Text (WriteLn) window. The second, if you want to open a window and show the output there. And the third, whether you want output to the file ‘Trace_File’ in the current directory.

5) (optional) Call one or more procedures or functions that may or may not have the debug option set. Try not to call StartTracing again. You may call EndTracing more than once.

6) To stop tracing you must have a call to EndTracing.

For most people this is all you need. Please skip now to the section titled “The Example:”.

How it Works

Let’s quickly run through the procedures in LightTrace and I’ll explain their operation. Please refer to listing 1.

First, InitTracing. This simply initializes the global variables used in the unit. Mostly this prevents explosions if you inadvertently call EndTracing or WriteComment at the wrong time.

Then, one calls StartTracing. The options are saved in globals, and then windows and files are opened as needed (MakeScreenWindow: opens a small window. MakeTraceFile: creates and/or opens an MPW text file). Then some of the low memory variables, holding the locations of the code to be run when the processor encounters a TRAP instruction, are hacked. They are set to the assembly language routines ReplaceTrap7 and ReplaceTrap8. These, very short, routines go directly to TraceProcTop and TraceProcBot. The effect is one of TraceProcTop being called at the start of every procedure and TraceProcBot being called at the end of every procedure.

TraceProcTop: As mentioned, all your procedures will now call TraceProcTop as the first thing they do. TraceProcTop first gets the address of the routine that called it (GetRTS returns the address that will be returned to) and then passes this address to GetTheName. What GetTheName does is to call ScanForRTS to find the end of the routine pointed at by iP. Then, assuming that the N (names) option is turned on there should be an 8 character string at that position. This string is copied to str and GetTheName returns. Actually, some MPW programs provide 16 bit names and GetTheName is setup to handle those too. Having gotten the name of the routine TraceProcTop then calls WriteStr to output the indent, the word ‘BEGIN ‘ and then the name that was found. The indent level is incremented.

TraceProcBot does almost exactly the same thing as TraceProcTop except it prints ‘END ‘ and decrements the indent level.

WriteStr, and WriteStrLn are the output routines I use here. They go through a simple bottleneck and, according to the settings of the global variables, output is sent to the window, to the file or to the WriteLn window.

EndTracing is very simple. It closes any window or file and replaces the trap vectors. It is designed to not break even if you call it twice.

How it Really Works

LightSpeed Pascal inserts a Trap8 instruction at the beginning of every procedure or function compiled with the debug option on. Trap7 is inserted similarily at the end. We repoint the vectors for these traps to our code named ReplaceTrap8 and ReplaceTrap7 respectively.

The goal was for the same code to run on my Mac+ and also on my MacII. In order to accomplish this I observed that, even though the exception stacks are different on the 68000 and the 68020, the value of the PC is in the same place relative to A7. So, I move the return PC to D0, and replace the return PC with the address of 88tmp. Then when the RTE is executed control is returned to 88tmp and not to the routine where the Trap8 was encountered (the target procedure). It is then a simple matter (having the old return PC in D0) to reformat the stack like a normal procedure call and jump to TraceProcTop. From the point of view of TraceProcTop it is as if TraceProcTop was called directly from the target procedure. ie. TraceProcTop will return (RTS) directly to the target procedure.

Since the stack is set for TraceProcTop to return directly to the target routine it is a simple matter to get this vector by simply fetching it from 4(A6). I use the inline function GetRTS to do this.

Having an address in the beginning of the target procedure does not automatically give us the information we need, the name of the target procedure! Assuming that the names option is on, we look for the end of the target procedure from which we can BlockMove the name to a string.

In order to find the end of the target procedure we scan down looking to recognize the return sequence. This is the function of the procedure ScanForRTS. I have incorporated into ScanForRTS every return sequence I have ever seen. A summary follows.

Procedure and Function end sequences. This list was compiled by looking at the endings produced by LightSpeed Pascal v1.11, LightSpeed C v2.0, MPW Pascal, and MPW C. I think it represents all of the possible endings produced by just about any compiler. TML and Turbo users: If you see any different endings produced by your compiler, then please let me know.

1)    No parameter ending
 UNLK
 RTS    ;2 words
2) Longint parameter ending
 UNLK
 MOVE.L (SP)+,(SP)
 RTS    ;3 words
4) General ending #1
 UNLK
 MOVE.L (SP)+,A0
 LEA    X(SP),SP
 JMP    (A0);5 words
5) Wasteful general ending
 UNLK
 MOVE.L (SP)+,A0
 ADD.L  #X,SP
 JMP    (A0);6 words
6) General ending #2
 UNLK
 MOVE.L (SP)+,A0
 ADD.W  #X,SP
 JMP    (A0);5 words
7) Wasteful no parameter ending.
 UNLK
 MOVE.L (SP)+,A0
 JMP    (A0)

Xtra for Xperts

Please note that the name at the end of the target procedure has the high bit of the first byte set. In some cases, notably MacApp method calls, a 16 byte name is supplied by the compiler. I have noticed that when 16 byte names are supplied then the high bit of the first two bytes are set. GetTheName takes this into account.

OK, so you’re wondering why I make allowances for all these other compilers when this article is for LightSpeed Pascal (works with 1.11 and 2.0). This is because I have used this code to trace code from other compilers, and I didn’t want to change it back.

MPW Pascal has an option (D++) where the compiler inserts a call to %_BP at the beginning of every procedure, and a call to %_EP at the end. If you modify StartTraceTime to hack the A5 jump table instead of the trap vectors then you can use this to trace your MPW and MacApp programs.

Furthermore, if you start a timer at StartTraceTime and pause it during TraceProcTop and TraceProcBot it is possible to get a real useful performance analysis at the same time as you get a trace.

Many other possibilities manifest themselves, given a little thought. Anyone want to do a heap scramble? How about a gadget that measures the maximum amount of stack that is used?

The Example

The code that follows is an example, for LightSpeed Pascal, of how to trace the execution of two procedures.

Listing 1 is the text of the unit ‘LightTrace’.

Listing 2 is the source for LightTrace.a. Note that if you get the disks from MacTutor you will not need an assembler. You can just use the library LightTrace.a.lib.

Listing 3 is a short sample program. This is the way that your program should invoke tracing.

Listing 4 is the ‘Echo File’ produced by the WriteLn’s in the sample. The Text window shows the same.

Listing 5 is the trace output produced by setting the third parameter in StartTracing.

Listing 6 is the project for this example in LSP 1.11 format. This code works in the new (2.0) version also but the project is different. You will also need to convert the .lib to the new format.

Listing 1  
This is the file LightTrace.p.  It, along with LightTrace.a, contain all the code necessary 
for tracing procedure and function calls in LightSpeed Pascal. 

unit LightTrace;
interface

 procedure InitTracing;
{ call this to init Trace vars to safe values }

 procedure StartTracing 
 (toText, toScreen, toFile: boolean);
{ Start tracing all Subroutines }

 procedure EndTracing;
{ return to normal operation }

 procedure WriteComment (Str: str255);
{ write Str to Trace output }

{ The ‘Of’ functions are useful when formatting output }  
{ that must be a Str255.  Also, try them in your observe } { window. 
eg. Rectof(ThePort^.ClipRgn^^.RgnBBox) }

 function IntOf (Int: longint): str255;
{ convert the Int to a Decimal Str255 }

 function HexOf (lll: longint): str255;
{ convert the Int to a Hex Str255 }

 function PointOf (fff: Point): str255;
{ convert the Point to a Str255 }

 function RectOf (R: Rect): str255;
{ convert the Rect to a Str255 }

 procedure TraceProcTop;{ don’t call this yourself}

 procedure TraceProcBot;{ don’t call this yourself}

implementation

 type
 intsArr = array[0..16000] of integer;
 intsArrP = ^intsArr;

 var
 indent: integer;
 OldTrap7, OldTrap8: longint;
 doText, doScreen, doFile, started: boolean;
 ScreenWindow: grafPtr;
 TracePB: paramBlockRec;

 procedure ScanForRts (var iP: intsArrP);
 forward;

{ see  LightTrace.asm } 
 procedure ReplaceTrap7;
 external;
 procedure ReplaceTrap8;
 external;

{ retreive the addesss of the calling routine }
 function GetRTS: longint;
 inline
 $2EAE, $0004;{ move.l 4(A6),(sp) }

 function IntOf; {(int : longint) : str255}
 var
 str: str255;
 begin
 NumToString(int, str);
 IntOf := str;
 end;

 function HexOf; { (lll : longint) : str255}
 var
 str, str2: str255;
 i: integer;
 c: char;
 begin
 if lll = 0 then
 str := ‘$0’
 else
 begin
 str := ‘’;
 while (lll <> 0) do
 begin
 i := lll mod 16;
 if i < 10 then
 c := chr(ord(‘0’) + i)
 else
 c := chr(ord(‘A’) + (i - 10));
 str2 := ‘x’;
 str2[1] := c;
 str := concat(str2, str);
 lll := BitShift(lll, -4);
 end;
 str := concat(‘$’, str)
 end;
 HexOf := str;
 end;

 function Pointof;{  (fff :  Point) : str255}
 var
 str: str255;
 begin
 str := concat(IntOf(fff.h), ‘ ‘, IntOf(fff.v));
 Pointof := str;
 end;


 function RectOf; {(R : Rect) : str255}
 var
 str: str255;
 begin
 with R do
 str := concat(Intof(left), ‘ ‘, Intof(top), ‘ ‘,              
 Intof(right), ‘ ‘, Intof(bottom), ‘ ‘);
 RectOf := str;
 end;

{ set the globals to safe values }
 procedure InitTracing;
 begin
 started := false;
 doText := true;
 doScreen := false;
 doFile := false;
 indent := 0;
 end;

{ Open a small window in the back to see our output }
{ set ScreenWindow to point to this window }
 procedure MakeScreenWindow;
 var
 r: rect;
 OldPort: GrafPtr;
 begin
 getPort(OldPort);
 setRect(r, 4, 40, 156, 140);
 ScreenWindow := NewWindow(nil, r, ‘Trace Info’,
 true, 0, nil, false, 0);
 setport(ScreenWindow);
 textmode(srccopy);
 textFont(1);
 textsize(9);
 textFont(4);{monaco}
 moveto(4, 16);
 setport(OldPort);
 end;

{ remove ScreenWindow from the screen and from}
{memory }
 procedure RemoveScreenWindow;
 begin
 if doScreen then
 DisposeWindow(ScreenWindow);
 end;

{ do this to write a cr to the screen }
 procedure ScreenLn;
 var
 ThePen, poi: point;
 r: rect;
 aRgn: RgnHandle;
 OldPort: GrafPtr;
 begin
 GetPort(OldPort);
 SetPort(ScreenWindow);
 r := ScreenWindow^.PortRect;
 GetPen(ThePen);
 ThePen.h := r.left + 4;
 ThePen.v := ThePen.v + 12; { move thePen down }
 if (ThePen.v + 12) > r.bottom then
 begin { scroll up if necessary }
 aRgn := NewRgn;
 ScrollRect(r, 0, -12, aRgn);
 DisposeRgn(aRgn);
 ThePen.v := ThePen.v - 12;
 setorigin(0, 0);
 repeat { pause feature }
 GetMouse(poi);
 until not ptInRect(poi, r);
 end;
 moveto(ThePen.h, ThePen.v);
 setPort(OldPort);
 end;

{ This does a Write to our window }
 procedure WriteScreen (str: str255);
 var
 ThePen: point;
 r: rect;
 OldPort: GrafPtr;
 cr: str255;
 begin
 cr := ‘x’;
 cr[1] := chr(13);
 GetPort(OldPort);
 SetPort(ScreenWindow);
 r := ScreenWindow^.PortRect;
 GetPen(ThePen);
 if (ThePen.h + stringwidth(str) > r.right) or
 (pos(cr, str) > 0) then
 ScreenLn;
 DrawString(str);
 SetPort(OldPort);
 end;

{ This does a WriteLn to our window }
 procedure WriteScreenLn (str: str255);
 var
 i: integer;
 ThePen: point;
 r: rect;
 OldPort: GrafPtr;
 begin
 GetPort(OldPort);
 SetPort(ScreenWindow);
 WriteScreen(str);
 ScreenLn;
 SetPort(OldPort);
 end;

 procedure MakeTraceFile;
 var
 err: integer;
 str: str255;
 begin
 str := ‘Trace_File’;
 with TracePB do
 begin
 ioCompletion := nil;
 ioNamePtr := @str;
 ioVRefNum := 0;
 ioVersNum := 0;
 ioPermssn := 0;
 ioMisc := nil;
 err := PBOpen(@TracePB, false);
 if err = fnfErr then
 begin
 err := PBCreate(@TracePB, false);
 if err = 0 then
 err := PBOpen(@TracePB, false);
 end;
 if err = 0 then
 begin
 ioMisc := pointer(0);
 err := PBSetEOF(@TracePB, false);
 if err = 0 then
 begin
 err := PBGetFInfo(@TracePB, false);
 if err = 0 then
 with TracePB.ioFlFndrInfo do
 begin
                         { we’ll make this an MPW text file }
 fdType := ‘TEXT’;
 fdCreator := ‘MPS ‘;
 err := PBSetFInfo
 (@TracePB, false);
 end;{ with finder info }
 end;{ if getFinfo OK }
 end { if open OK }
 else
 doFile := false;
 end; { with TracePB }
 end;{ proc MakeTraceFile }

 procedure CloseTraceFile;
 var
 err: integer;
 begin
 if doFile then
 err := PBClose(@TracePB, false);
 end;

{ same as write except the str goes to the file }
 procedure WriteFile (str: str255);
 var
 err: integer;
 eof: longint;
 begin
 if length(str) > 0 then
 if doFile then
 with TracePB do
 begin
 err := PBGetEof(@TracePB, false);
 { ever Fail??}
 if err <> 0 then
 repeat
 sysbeep(1)
 until button;

 eof := ord(ioMisc);
 ioMisc := pointer(eof + length(str));
 err := PBSetEof(@TracePB, false);
 if err = 0 then
 begin
 ioBuffer := pointer(ord(@str) + 1);
 ioReqCount := length(str);
 ioPosMode := fsFromstart;
 ioPosOffset := eof;
 err := PBWrite(@TracePB, false);
 end;{ setEof OK }
 end;{ with TracePB }
 end;{ proc WriteFile }

{ same as writeLn(str) except output is to the file}
 procedure WriteFileLn (str: str255);
 begin
 writeFile(str);
 str := ‘x’;
 str[1] := chr(13);
 writeFile(str);
 end;

{ These two proc’s are our output bottleneck }

 procedure WriteStr (str: str255);
 begin
 if doText then
 Write(str);
 if doScreen then
 WriteScreen(str);
 if doFile then
 WriteFile(str);
 end;

 procedure WriteStrLn (str: str255);
 begin
 if dotext then
 WriteLn(str);
 if doScreen then
 WriteScreenLn(str);
 if doFile then
 WriteFileLn(str);
 end;

{ Call ScanForRTS to find the end of a procedure.  iP is}
{pointed past the end (at the name) Copy the name into}
{the str.  If it is a MacApp name (16 char), then add}
{more.}
 procedure GetTheName (var iP: intsArrP;
 var str: str255);
 begin
 str := ‘12345678’;
 ScanForRts(iP);
 blockMove(@iP^, pointer(ord(@str) + 1), 8);
 if ord(str[1]) >= 128 then
 str[1] := chr(ord(str[1]) - 128);
 if ord(str[2]) >= 128 then
 begin
 str[2] := chr(ord(str[2]) - 128);
 str := concat(str, ‘12345678’);
 blockMove(pointer(ord(@iP^) + 8),
  pointer(ord(@str) + 9), 8);
 end;
 end;

 procedure TraceProcTop;
 var
 str: str255;
 iP: intsArrP;
 i: integer;
 begin
 iP := pointer(GetRTS);
 GetTheName(iP, str);
 for i := 1 to indent do
 writeStr(‘ . ‘);
 indent := indent + 1;
 writeStr(‘BEGIN ‘);
 writeStrLn(str);
 end;

 procedure TraceProcBot;
 var
 str: str255;
 iP: intsArrP;
 i: integer;
 begin
 iP := pointer(GetRTS);
 GetTheName(iP, str);
 indent := indent - 1;
 for i := 1 to indent do
 writeStr(‘ . ‘);
 writeStr(‘END   ‘);
 writeStrLn(str);
 end;

 procedure WriteComment;{ (str : str255)}
 var
 i: integer;
 begin
 for i := 1 to indent do
 writeStr(‘ . ‘);
 writeStr(‘REM ‘);
 writeStrLn(str);
 end;

{ This routine moves the pointer , iP, down in memory}
{until the end sequence of a procedure or function is}
{found.  Or, it quits after 16000 bytes.  If an end }
{sequence is found then the pointer if set past the last}
{word.  If not, then the pointer is pointed at the text}
{‘unknown ‘ (8 char, including the space). See article}
{text for a list of end sequences.}

 procedure ScanForRts; { (var iP : intsArrP)}
 var
 count, size: longint;
 str: str255;
 begin
 count := 8000;{ max size of any procedure ??? }
 size := 0;
 while size = 0 do
 begin
 if iP^[0] = $4E5E then   { UNLK }
 begin
 if (iP^[1] = $2E9F) and (iP^[2] = $4E75) then
 size := 6
     {   MOVE.l (A7)+,A7  RTS}
 else if iP^[1] = $4E75 then
 size := 4{ RTS }
 else if iP^[1] = $205F then
 { MOVEA.L (A7)+,A0 }
 begin
 if (iP^[2] = $4FEF) and 
 (iP^[4] = $4ED0) then
 { LEA x(A7),A7  JMP (A0) }
 size := 10
 else if (iP^[2] = $DFFC) and 
 (iP^[5] = $4ED0) then
    { ADD.l #x,A7  JMP (A0) }
 size := 12
 else if (iP^[2] = $DEFC) and
  (iP^[4] = $4ED0) then
  { ADD.w #x,A7  JMP (A0) }
 size := 10
 else if iP^[3] = $4ED0 then
 size := 8  {JMP (A0) }
 end;
 end;
 count := count - 2;
 if count <= 0 then
 size := 22222;
 if size <> 0 then
 iP := pointer(ord(iP) + size)
 else
 iP := pointer(ord(iP) + 2)
 end;{ while size=0 }
 if count <= 0 then
 begin
 str := ‘unknown ‘;
 iP := pointer(ord(@str) + 1);
 end;
 end; { proc scan for Rts }

 procedure StartTracing; 
 { (toText, toScreen, toFile : boolean)}
 var
 lP: ^longint;
 begin
 InitTracing;{ initialize global vars }
 { save the options as globals }
 doText := toText;
 doScreen := toScreen;
 doFile := toFile;
 started := true;
 if doscreen then
 MakeScreenWindow;
 if doFile then
 MakeTraceFile;
 if doText then
 ShowText;

 lP := pointer($80 + 4 * 7);{ trap 7}
 OldTrap7 := lP^;
 lP^ := ord(@ReplaceTrap7);
 lP := pointer($80 + 4 * 8);{ trap 8}
 OldTrap8 := lP^;
 lP^ := ord(@ReplaceTrap8);
 end;{ proc StartTracing }

 procedure EndTracing;
 var
 lP: ^longint;
 begin
 if Started then
 begin
 lP := pointer($80 + 4 * 7);{ trap 7}
 lP^ := OldTrap7;
 lP := pointer($80 + 4 * 8);{ trap 8}
 lP^ := OldTrap8;
 RemoveScreenWindow;
 CloseTraceFile;
 end;
 InitTracing;
 end;{ proc EndTracing }
end.{ unit lightTrace }
Listing 2  
This is the file LightTrace.a.  It makes the two code fragments ReplaceTrap8, and ReplaceTrap7 
available to the pascal code.  What these fragments do is convert the stack from exception 
(RTE or interrupt) format to subroutine (RTS) format, and then jump to TraceProcTop 
(or TraceProcBot).  We take advantage of the fact that in all 680XX machines the program 
counter that we RTE to is at 2(a7).  Note that the amount of stack used by the RTE can be 
different depending upon processor type.

 import TraceProcTop:CODE 
 import TraceProcBot:CODE 

ReplaceTrap8 proc export

 move.l 2(sp),d0 ;address to RTS to
  lea    @88tmp,a0
 move.l a0,2(sp) ;fake address to RTE to
 RTE

@88tmp
 move.l  d0,-(sp);setup stack for rts
 jmp     TraceProcTop(a5)
 
ReplaceTrap7 proc export

 move.l 2(sp),d0 ;address to RTS to
  lea    @77tmp,a0
 move.l a0,2(sp) ;fake address to RTE to
 RTE

@77tmp
 move.l  d0,-(sp);setup stack for rts
 jmp     TraceProcBot(a5)
 
 end ; of file LightTrace.a
Listing 3  
This is the file Test_LightTrace.p.  Here we exercise the LightTrace routines with 
two short numerical  algorithms.

program test_LightTrace;
 uses
 LightTrace;
 var
 i, j: integer;

 procedure Decompose (Num: integer);
  { factor Num, report smallest factors first }
 var
 i, factor: integer;

 function check (factor: integer): boolean;
 begin
 if (Num div factor) * factor = Num then
 check := true
 else
 check := false;
 end;

 begin
 write(IntOf(Num), ‘=1’);
 while check(2) and (Num > 1) do
 begin
 write(‘*2’);
 Num := Num div 2;
 end;
 factor := 3;
 while (Num > 1) do
 begin
 while check(factor) and (Num > 1) do
 begin
 write(‘*’, IntOf(factor));
 Num := Num div factor;
 end;
 factor := factor + 2;
 end;
 writeln;
 end;

 procedure Reduce
  (var numerator, denominator: integer);
 {reduces fraction to lowest terms}
 var
 commonDivisor: integer;

 function GCD (m, n: integer): integer;
 var
 r: integer;
 begin
 writeComment(
 concat(‘GCD ‘, IntOf(m), ‘ ‘, IntOf(n)));
 r := m mod n;
 if r = 0 then
 GCD := n
 else
 GCD := GCD(n, r)
 end;{ funct GCD }

 begin { proc reduce }
 commonDivisor := GCD(numerator, denominator);
 numerator := numerator div commonDivisor;
 denominator := denominator div commonDivisor;
 end; { proc reduce }

begin
 showtext;
 InitTracing;
 StartTracing(false, true, true);
 decompose(1 * 2 * 3 * 4 * 5);
 i := 1 * 2 * 3 * 4 * 5;
 j := 6 * 7 * 8 * 9;
 write(IntOf(i), ‘/’, IntOf(j), ‘=’);
 reduce(i, j);
 writeln(IntOf(i), ‘/’, IntOf(j));
 EndTracing;
end.{ of main program test_LightTrace }
Listing 4  
This is the file ‘Echo File’.  This is what is sent to the text window by writeln’s. It 
is only two lines long and shows the result of factoring a number and reducing a fraction.

120=1*2*2*2*3*5
120/3024=5/126
Listing 5  
This is the file ‘Trace_File’.  It was created automatically because the third argument 
to StartTracing was true.  It contains the trace output, including comments.  It does not 
contain any writeln’s. Please notice that there are two sections.  The first is the trace of 
the procedure Decompose. The second is the trace of the procedure Reduce, which recurses.

 BEGIN DECOMPOS
 . BEGIN CHECK   
 . END   CHECK   
 . BEGIN CHECK   
 . END   CHECK   
 . BEGIN CHECK   
 . END   CHECK   
 . BEGIN CHECK   
 . END   CHECK   
 . BEGIN CHECK   
 . END   CHECK   
 . BEGIN CHECK   
 . END   CHECK   
 . BEGIN CHECK   
 . END   CHECK   
 . BEGIN CHECK   
 . END   CHECK   
END   DECOMPOS
BEGIN REDUCE  
 . BEGIN GCD     
 .  . REM GCD 120 3024
 .  . BEGIN GCD     
 .  .  . REM GCD 3024 120
 .  .  . BEGIN GCD     
 .  .  .  . REM GCD 120 24
 .  .  . END   GCD     
 .  . END   GCD     
 . END   GCD     
END   REDUCE  

Listing 6

This is the project window for the example presented here. Note that the procedures to be traced must have the debug (D) and names (N) selected. Also note, The LightTrace unit must not have the debug (D) option selected.

Answers to problems 4b and 4c being, of course:127 and 6

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

coconutBattery 3.9.14 - Displays info ab...
With coconutBattery you're always aware of your current battery health. It shows you live information about your battery such as how often it was charged and how is the current maximum capacity in... Read more
Keynote 13.2 - Apple's presentation...
Easily create gorgeous presentations with the all-new Keynote, featuring powerful yet easy-to-use tools and dazzling effects that will make you a very hard act to follow. The Theme Chooser lets you... Read more
Apple Pages 13.2 - Apple's word pro...
Apple Pages is a powerful word processor that gives you everything you need to create documents that look beautiful. And read beautifully. It lets you work seamlessly between Mac and iOS devices, and... Read more
Numbers 13.2 - Apple's spreadsheet...
With Apple Numbers, sophisticated spreadsheets are just the start. The whole sheet is your canvas. Just add dramatic interactive charts, tables, and images that paint a revealing picture of your data... Read more
Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Fantastical 3.8.2 - Create calendar even...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
iShowU Instant 1.4.14 - Full-featured sc...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more

Latest Forum Discussions

See All

The iPhone 15 Episode – The TouchArcade...
After a 3 week hiatus The TouchArcade Show returns with another action-packed episode! Well, maybe not so much “action-packed" as it is “packed with talk about the iPhone 15 Pro". Eli, being in a time zone 3 hours ahead of me, as well as being smart... | Read more »
TouchArcade Game of the Week: ‘DERE Veng...
Developer Appsir Games have been putting out genre-defying titles on mobile (and other platforms) for a number of years now, and this week marks the release of their magnum opus DERE Vengeance which has been many years in the making. In fact, if the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 22nd, 2023. I’ve had a good night’s sleep, and though my body aches down to the last bit of sinew and meat, I’m at least thinking straight again. We’ve got a lot to look at... | Read more »
TGS 2023: Level-5 Celebrates 25 Years Wi...
Back when I first started covering the Tokyo Game Show for TouchArcade, prolific RPG producer Level-5 could always be counted on for a fairly big booth with a blend of mobile and console games on offer. At recent shows, the company’s presence has... | Read more »
TGS 2023: ‘Final Fantasy’ & ‘Dragon...
Square Enix usually has one of the bigger, more attention-grabbing booths at the Tokyo Game Show, and this year was no different in that sense. The line-ups to play pretty much anything there were among the lengthiest of the show, and there were... | Read more »
Valve Says To Not Expect a Faster Steam...
With the big 20% off discount for the Steam Deck available to celebrate Steam’s 20th anniversary, Valve had a good presence at TGS 2023 with interviews and more. | Read more »
‘Honkai Impact 3rd Part 2’ Revealed at T...
At TGS 2023, HoYoverse had a big presence with new trailers for the usual suspects, but I didn’t expect a big announcement for Honkai Impact 3rd (Free). | Read more »
‘Junkworld’ Is Out Now As This Week’s Ne...
Epic post-apocalyptic tower-defense experience Junkworld () from Ironhide Games is out now on Apple Arcade worldwide. We’ve been covering it for a while now, and even through its soft launches before, but it has returned as an Apple Arcade... | Read more »
Motorsport legends NASCAR announce an up...
NASCAR often gets a bad reputation outside of America, but there is a certain charm to it with its close side-by-side action and its focus on pure speed, but it never managed to really massively break out internationally. Now, there's a chance... | Read more »
Skullgirls Mobile Version 6.0 Update Rel...
I’ve been covering Marie’s upcoming release from Hidden Variable in Skullgirls Mobile (Free) for a while now across the announcement, gameplay | Read more »

Price Scanner via MacPrices.net

New low price: 13″ M2 MacBook Pro for $1049,...
Amazon has the Space Gray 13″ MacBook Pro with an Apple M2 CPU and 256GB of storage in stock and on sale today for $250 off MSRP. Their price is the lowest we’ve seen for this configuration from any... Read more
Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more

Jobs Board

Optometrist- *Apple* Valley, CA- Target Opt...
Optometrist- Apple Valley, CA- Target Optical Date: Sep 23, 2023 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 796045 At Target Read more
Senior *Apple* iOS CNO Developer (Onsite) -...
…Offense and Defense Experts (CODEX) is in need of smart, motivated and self-driven Apple iOS CNO Developers to join our team to solve real-time cyber challenges. Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Child Care Teacher - Glenda Drive/ *Apple* V...
Child Care Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Share on Facebook Apply Read more
Machine Operator 4 - *Apple* 2nd Shift - Bon...
Machine Operator 4 - Apple 2nd ShiftApply now " Apply now + Start apply with LinkedIn + Apply Now Start + Please wait Date:Sep 22, 2023 Location: Swedesboro, NJ, US, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.