TweetFollow Us on Twitter

Avoiding traps
Volume Number:2
Issue Number:10
Column Tag:Advanced Macing

Reduce Your Time in the Traps!

By Mike Morton, Senior Software Engineer, Lotus Development Corp., Cambridge, MA

Life in the fast lane

The Macintosh ROM subroutines are called with “trap” instructions, intercepted by dispatching software which interprets the trap and calls the routine. This method is very general, providing compatibility with future ROMs and allowing buggy routines to be replaced.

It's also slow, taking about 45 microseconds for the dispatch process. This article tells you a way to avoid the dispatcher without losing its generality. Since the timing differences are measured in microseconds, there's also a discussion of techniques for measuring the time consumed by a piece of code. Also, a program is included to show the alternate way to call the ROM and how to measure the times used by different methods.

Avoiding traps

When a program executes a trap instruction, the 68000 detects the “error” and transfers control to the trap dispatcher pointed to by the longword at $0028. The dispatching software must, among other things:

• preserve some registers on the stack

• fetch the trap instruction from the code

• decide if the trap is a Toolbox or OS call

• look up the trap number to find whether the routine is in RAM or ROM, and what its address is

• handle the “auto-pop” and “pass A0” bits

• call the routine

• restore registers from the stack

Most of this work can be avoided if you know the routine's address and call it directly, but this is a bad idea for two reasons. First, the address may change in future ROMs. Second, Apple distributes “patches” to ROM routines by changing the dispatch table to call new versions in RAM -- if your program “knows” the address, it'll call the old, buggy ROM routines, ignoring the new RAM-based ones.

There is a balance between hardwiring the address and using the trap dispatcher for every call. The Toolbox “GetTrapAddress” function decodes a trap instruction for you and returns the address of the routine, just as the dispatcher does. You can do this decoding just once in your program, save the address, and repeatedly call it later.

The main reason not to bypass the dispatcher is that it saves a few registers across each call. If you're working in assembler, this is no problem -- just save registers yourself, as needed. In most high-level languages, it also won't be a problem, since the registers lost are typically scratch registers: D1, D2, and A2.

Fig. 1 Our TrapTime Utility shows the difference!

A high-level example

First, let's look at the normal way of calling a Toolbox routine: the simple “SetPt” procedure, which sets the coordinates of a Quickdraw “point”. The following example and the timing program are in TML Pascal; they should be easy to convert to other languages.

Most programs include the Quickdraw unit, which declares “setPt” with

procedure SetPt(VAR pt: point; h, v: integer); INLINE $A880;

When you call the routine with the statement

 setPt (myPt, x, y); { set the point }

it pushes the parameters on the stack and executes the instruction $A880 to trap to the dispatcher, which calls the routine. If you want to skip the cost of repeatedly decoding the trap, you can do it once like this:

 var setPtAddr:longint; { addr of setPt }
 setPtAddr := getTrapAddress ($A880);

To call this address, declare a new routine like SetPt, but which produces different in-line 68000 code:

procedure mySetPt
 (VAR pt: point; h, v: integer;
 addr: longint);
 INLINE $205F, $4E90;

Note the extra parameter to this routine: the address of the routine to be called. The instructions given in hex after the “INLINE” do a JSR to that address. The result is nearly the same as executing a trap, but faster.

Calling with this interface is almost like a normal call; pass the address as a parameter:

 mySetPt (myPt, x, y, setPtAddr);

This can be used for most Toolbox calls - just declare your own routine (choose any name) with the same parameters plus the address parameter, and include the exact same “INLINE” code after it. Don't forget to initialize the address with GetTrapAddress before calling, or awful things will happen.

Other high-level languages

You should be able to use this method with almost any language which allows you to insert assembler code in your high-level program. Some languages may have trouble calling the ROM directly -- for instance, many C compilers pass parameters differently than ROM routines do. Some C compilers allow you to choose the method of parameter passing; this will allow you to dispense with assembler altogether and just call the routine through a pointer (ask your nearest C guru how to do this).

More straightforward approaches

This approach assumes that “SetPt” is too slow. If you actually need Toolbox operations to be faster, consider writing the code yourself. You can write a procedure or function to assign two integers to the coordinates of a point -- or just do the assignment yourself. For a simple operation, this approach is preferable to spending lots of effort avoiding the trap dispatcher. (The “K.I.S.S.” rule applies here: “Keep It Simple, Stupid.”)

Speed improvements: hard data

Let's get quantitative. Consider four ways to assign to a point:

• the usual trap

• calling the ROM directly with INLINE

• calling your own procedure

• doing the assignment in-line

I wrote all four in Lisa Pascal and found these times on a Mac, and on a Lisa running MacWorks:

Table: Time to assign to a point

(all times in microseconds)

Mac Lisa/MacWorks

Normal “SetPt” trap 67.7 84.9

Pre-decoded call 22.8 25.6

Roll-your-own 34.5 35.2

Assign in-line 4.8 4.8

Writing your own procedure is slower than using the trap routine's address! The ROM is so fast, compared to compiled Pascal, that it's worth the slightly more complicated call. Part of the speed is because the ROM is tightly-coded; part is because the Mac's video refresh slows down code in RAM.

The fastest method is to forget about writing a procedure and do the assignment normally. This is fourteen times faster than using traps to call the ROM! (There's something to be said for the do-it-yourself approach.)

I tried running the program on a Mac Plus, since its ROM dispatch table has been expanded for faster trap calls. The time for a normal trap is 58.9 microseconds, instead of 67.7 microseconds. All the other times are nearly the same.

Speed improvements: summary

First, all this isn't worthwhile for most traps. If you want to speed up disk I/O, resource operations, etc., the microseconds saved at trap time are dwarfed by the amount of time for a disk transfer or to search a large resource. This trick is appropriate only in some situations.

Second, some routines are best done by hand in simple code in your program. ROM tools such as “SetPt” exist for your convenience, not because they're hard to code. If you find they're taking too much time, change them to a few lines of your own code.

But suppose you're trying to draw lines at top speed with repeated “LineTo” calls? Or use one of the simple bit manipulators in a loop? You may find that you can't easily write it yourself, but you can save 45 microseconds by calling into the ROM using a previously determined address. My estimate is that if a trap takes between 200 and 800 microseconds, you should consider skipping the dispatcher.

The timing program

The program “traptime” found the times given in the table. It has four procedures to time methods, and a “getbasetime” procedure to find the overhead of a loop with no calls. You can write a similar program using the same design in nearly any language.

Note that the program prints its results in ticks (60ths of a second) and doesn't compute the time for a loop iteration; I did the conversions to microseconds-per-iteration by hand, rather than trying to get Pascal to do fractional arithmetic.

Timing methods

Unfortunately, doing accurate timings is fraught with problems. This program tries to avoid these. Some points on timings:

• Repeat your measurements to help detect “random” factors. Small discrepancies should be averaged; large ones should be found and removed.

• Be careful when comparing routines: the four timing routines (and the “overhead” routine) are identical except for one section. Keeping this parallel structure makes your program a controlled experiment, helping you time only the differences between procedures.

• Vary the loop size; make sure that your time per iteration converges as your loop gets bigger.

• When waiting for the program, don't move the mouse or fiddle with the keyboard. This causes interrupts and affects the timings.

• I suspect you shouldn't have the disk spinning, nor have a debugger active while timing. (In practice, I can't detect any timing differences due to either of these factors.)

In short, timing is a scientific experiment and is easy to ruin by not controlling the environment carefully.


Bypassing the trap dispatcher can be a valuable technique in a limited number of situations, allowing you to cut about 45 microseconds off the time to call the ROM. It has some drawbacks such as losing register contents, and may be hard to implement in some higher-level languages. In addition, many ROM calls take so long that the savings isn't significant.

Whatever technique you're interesting in optimizing and timing, accurate measurement is a matter of a careful, controlled approach.

{ traptime -- A program to time various methods of doing a toolbox trap:
  The usual method, calling a user-written routine to do the work, doing 
the work in-line, and calling the ROM routine directly without going 
through the trap dispatcher. Times for all routines are written on the 
screen in ticks for a given number of calls, then the number of calls 
is varied for improved accuracy.

  Mike Morton, November 1985. Modified for TML Pascal, June 1986. }

program traptime (output);{ "(output)" lets us do writelns }

{$I MemTypes.ipas  }
{$I QuickDraw.ipas } { we use Quickdraw graphics }
{$I OSIntf.ipas }{ and OS definitions }
{$I ToolIntf.ipas }{ and Toolbox calls }

var         { program-wide variables }
  basetime: longint; { constant overhead for the loop }
  loops: longint;         { number of iterations to time }
  start: longint;         { starting tickcount for timing }
  Event:EventRecord; {simple event loop for cmd-3}
  DoIt: Boolean; {getnextevent boolean}
  Finished:Boolean;{event loop terminator}

{ getbasetime -- Find the time for the loop when nothing is done inside 
it.This tells us the overhead which should be subtracted from other timings. 

function getbasetime: longint;
var count: longint;        { loop counter }
  start := tickcount;        { snapshot starting time }
  for count := 1 to loops do        { loop a bunch of times... }
    ;           { ...doing nothing each time }
  getbasetime := tickcount-start;       { calculate elapsed time }
end;            { function "getbasetime" }

{ usualtime -- Find the time used to call the ROM the usual way.  This, 
and all timing routines, should look as much as possible like "getbasetime". 

function usualtime: longint;
  count: longint;        { loop counter }
  pt: point;        { point to assign to }
  x, y: integer;         { coordinates to assign to the point }
  start := tickcount;        { snapshot starting time }
  for count := 1 to loops do        { this time, inside the loop... }
    setpt (pt, x, y);        { ...we do the ROM call }
  usualtime := tickcount-start;          { calculate elapsed time }
end;            { function "usualtime" }

{ setmypt -- This isn't a timing function like the others; it's a replacement 
for the ROM's "setpt" routine, to see how fast we can do it ourselves. 
procedure setmypt (VAR pt: point; x, y: integer);
  pt.h := x; pt.v := y; { assign to the coordinates; easy! }
end;    { procedure "setmypt" }

{ myowntime -- Time assignment using our own procedure. }

function myowntime: longint;
  count: longint;        { loop counter }
  pt: point;        { point to assign to }
  x, y: integer;         { coordinates to assign to point }
  start := tickcount;        { snapshot starting time }
  for count := 1 to loops do        { this time, inside the loop... }
    setmypt (pt, x, y);           { ...we call our own routine }
  myowntime := tickcount-start;          { calculate elapsed time }
end;            { function myowntime }

{ inlintime -- The most straightforward way: we do the assignment in 
the loop. }

function inlintime: longint;
  count: longint;        { loop counter }
  pt: point;        { point to assign to }
  x, y: integer;         { coordinates to assign to point }
  start := tickcount;        { snapshot starting time }
  for count := 1 to loops do        { this time, inside the loop... }
    begin; pt.h := x; pt.v := y; end;   { ...we do assignment here }
  inlintime := tickcount-start;          { calculate elapsed time }
end;            { function inlintime }

{ setptx -- This is another replacement for "setpt".  It takes an extra 
parameter, the previously determined address of "setpt", and calls that 
address, leaving the other parameters for "setpt".  Unfortunately, TMLPascal 
doesn't mimic Lisa Pascal closely enough to allow us to generate more 
than one word of code in a single declaration.  So we have two procedures 
-- these MUST always be used together!  TML says their 2.0
 release of the compiler will be Lisa-compatible on this score, so this 
unsightly workaround won't be needed any more. }

procedure setptx1 (var pt: point; h, v: integer; addr: longint);
      INLINE   $205F; { MOVE.L   (A7)+,A0  
 ; pop routine's address into A0  }
procedure setptx2;
      INLINE   $4E90;{ JSR(A0);  and call that address }

{ gettrtime -- The last and most complicated way of calling the routine. 
 We use the trap address to call it directly. }

function gettrtime: longint;
  addr: longint;         { actual address of "setpt" }
  count: longint;        { loop counter }
  pt: point;        { point to assign to }
  x, y: integer;         { coordinates to assign to point }
  addr := gettrapaddress ($a880);    { find where routine lives }
  start := tickcount;         { snapshot starting time }
  for count := 1 to loops do begin { inside the loop... }
    setptx1 (pt, x, y, addr);          { ...we call on ROM  }
    setptx2;{ (kludge to sneak in 2nd instruction }
  gettrtime := tickcount-start;              { calculate elapsed time 
end;             { function gettrtime }

begin;          { *** main program *** }
  writeln ('If launching from a floppy, wait for it to stop and click 
to begin...');
  while not button do; while button do;      { wait for a click }

  loops := 10000;          { start with a small loop size... }
  while loops <= 1000000 do  { and go through several sizes}
    basetime := getbasetime;        { find constant overhead }

    writeln ('number of loops:', loops, '; base time is:', basetime);
    writeln ('time for usual method is..........: ', usualtime - basetime);
    writeln ('time for calling my own routine is: ', myowntime - basetime);
    writeln ('time for doing it in-line is......: ', inlintime - basetime);
    writeln ('time for doing it with gettrapaddr: ', gettrtime - basetime);

    loops := loops * 10;   { loop sizes increase exponentially }

   writeln ('click to exit or take snapshot ');
 if DoIt then
 Case Event.what of
  KeyDown: begin end;
  Mousedown: begin Finished:=true; end;
Until Finished;
end.            { of main program "traptime"  }



Community Search:
MacTech Search:

Software Updates via MacUpdate

BusyContacts 1.6.4 - Fast, efficient con...
BusyContacts is a contact manager for OS X that makes creating, finding, and managing contacts faster and more efficient. It brings to contact management the same power, flexibility, and sharing... Read more
Steam 4.0 - Multiplayer and communicatio...
Steam is a digital distribution, digital rights management, multiplayer and communications platform developed by Valve Corporation. It is used to distribute a large number of games and related media... Read more
OmniGraffle Pro 7.19.3 - Create diagrams...
OmniGraffle Pro helps you draw beautiful diagrams, family trees, flow charts, org charts, layouts, and (mathematically speaking) any other directed or non-directed graphs. We've had people use... Read more
OmniGraffle 7.19.3 - Create diagrams, fl...
OmniGraffle helps you draw beautiful diagrams, family trees, flow charts, org charts, layouts, and (mathematically speaking) any other directed or non-directed graphs. We've had people use Graffle to... Read more
Hopper Disassembler 5.3.3- - Binary disa...
Hopper Disassembler is a binary disassembler, decompiler, and debugger for 32- and 64-bit executables. It will let you disassemble any binary you want, and provide you all the information about its... Read more
calibre 5.35.0 - Complete e-book library...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital librarian... Read more
Sound Studio 4.10.0 - Robust audio recor...
Sound Studio lets you easily record and professionally edit audio on your Mac. Easily rip vinyls and digitize cassette tapes, or record lectures and voice memos. Prepare for live shows with live... Read more
Sparkle Pro 4.0 - Visual website creator...
Sparkle Pro will change your mind if you thought building websites wasn't for you. Sparkle is the intuitive site builder that lets you create sites for your online portfolio, team or band pages, or... Read more
Dropbox 140.4.1951 - Cloud backup and sy...
Dropbox for Mac is a file hosting service that provides cloud storage, file synchronization, personal cloud, and client software. It is a modern workspace that allows you to get to all of your files... Read more
FotoMagico 6.0.5 - Powerful slideshow cr...
FotoMagico lets you create professional slideshows from your photos and music with just a few, simple mouse clicks. It sports a very clean and intuitive yet powerful user interface. High image... Read more

Latest Forum Discussions

See All

Best iPhone Game Updates: ‘Garena Free F...
Hello everyone, and welcome to the week! It’s time once again for our look back at the noteworthy updates of the last seven days. I got busted last week for not including the obligatory free-to-play matching puzzle game update of the week, and my... | Read more »
‘Horizon Chase’ China Spirit DLC Release...
Following the release of the excellent reveal of the Horizon Chase Senna Forever expansion, the game will be getting a new DLC on mobile platforms today. Today, the Horizon Chase China Spirit DLC pack will release on iOS and Android bringing in 9... | Read more »
‘PUZZLED’ from SNK and Hamster Is Out No...
Following ZED BLADE ACA NeoGeo earlier this month, SNK has brought over another game in the ACA NeoGeo series to both iOS and Android in the form of PUZZLED. SNK and Hamster originally brought the series to mobile with Samurai Shodown IV, Alpha... | Read more »
A House Full of Covid – The TouchArcade...
It’s been a rough week as both of our young children tested positive for Covid, and since recording this early on Friday my wife has tested positive now too. Thankfully the kids seemed to recover fairly quickly and are mostly back to normal, and I... | Read more »
TouchArcade Game of the Week: ‘Krispee S...
Krispee Street is a new hidden object game from Frosty Pop that is based on their popular and almost painfully sweet webcomic Krispee. This is one of the latest titles to be added to the Netflix Games catalog, which means you’ll need to log into... | Read more »
SwitchArcade Round-Up: ‘Escape Lala’, ‘B...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for January 21st, 2022. In today’s article, we’ve got a lot of new releases. A lot. There were eight on the schedule when I went to bed last night. There were twenty-four when I woke up... | Read more »
Beta Testers Needed for Huge Version 2.0...
Ya’ll remember Dungeon Raid, right? The phenomenal matching RPG hybrid that launched on mobile more than a decade ago, but was more or less abandoned by its developer only to die a slow death on the App Store before the 32-bit Appocalypse finally... | Read more »
‘Ark Legends’ Gives Players a Chance to...
It’s Airpods and Amazon gift cards galore as Melting Games opens pre-registration for Ark Legends. The upcoming mobile RPG is giving away tons of in-game goodies such as gold, energy, iron core, hero summon chest and rare iron core to players who... | Read more »
‘Nickelodeon Extreme Tennis’ Out Now on...
Nickelodeon Extreme Tennis () from Old Skull Games and Nickelodeon is this week’s new Apple Arcade release. Nickelodeon Extreme Tennis features characters from old and new Nickelodeon shows including SpongeBob, TMNT, and many more. The tennis game... | Read more »
SwitchArcade Round-Up: ‘RPGolf Legends’,...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for January 20th, 2022. In today’s article, we’ve got a massive amount of new releases to check out. We’ve got summaries of all of them, from heaven to hell. We also have the lists of... | Read more »

Price Scanner via

Verizon’s 2022 iPad promo: $100-$310 off any...
Verizon has cellular-capable iPads on sale for $100-$310 off MSRP when purchased with an Unlimited service plan. Sale price is applied to your account monthly over a 24 or 30 month period, depending... Read more
Sunday Sale: Apple AirPods are on sale for up...
Amazon has Apple AirPods on sale for $10-$100 off MSRP today, depending on the model. All are in stock today with free delivery: – AirPods Max headphones (Blue): $449 $100 off MSRP – AirPods Max... Read more
These Apple resellers are offering 13″ M1 Mac...
Apple resellers are offering discounts on 13″ MacBook Pros with M1 Apple Silicon processors ranging up to $150 off MSRP. Here’s where to get one today: (1): Apple’s 13″ MacBook Pros with M1 Apple... Read more
Amazon lowers prices on select 13″ M1 MacBook...
Amazon has select Apple 13″ M1 MacBook Airs on sale for $150 off MSRP this weekend, starting at only $849. Their prices are the lowest available for new MacBook Airs today. Stock may come and go, so... Read more
Apple has 13″ M1 MacBook Airs back in stock s...
Apple has restocked a full line of 13″ M1 MacBook Airs, Certified Refurbished, starting at only $849 and up to $190 off original MSRP. These are the cheapest M1-powered MacBooks for sale today at... Read more
In stock and on sale! 16″ 10-Core M1 Pro MacB...
Amazon has new 16″ 10-Core/512GB M1 Pro MacBook Pros in stock today and on sale for $50 off MSRP including free shipping. Their prices are the lowest available for new M1 Pro 16″ MacBook Pro from any... Read more
Deal Alert!: 14″ M1 Pro with 10-Core CPU in s...
Amazon has the new 14″ M1 Pro MacBook Pro with a 10-Core CPU and 16-Core GPU in stock today and on sale for $2299.99 including free shipping. Their price is $200 off Apple’s standard MSRP, and it’s... Read more
Apple has 24-inch M1 iMacs (8-Core CPU/8-Core...
Apple has restocked a wide array of 24-inch M1 iMacs with 8-Core CPUs and 8-Core GPUs in their Certified Refurbished store. Models are available starting at only $1269 and range up to $260 off... Read more
Select 24″ M1 iMacs are on sale for $100 off...
Sales of Apple’s new 24″ M1 iMacs have been rare since its introduction, perhaps due to global supply issues. However, B&H is offering a $100 discount on select 24″ iMacs, and they’re in stock... Read more
M1 Mac minis are back in stock today at Apple...
Apple has M1-powered Mac minis available in their Certified Refurbished section starting at only $589 and up to $140 off MSRP. Each mini comes with Apple’s one-year warranty, and shipping is free: –... Read more

Jobs Board

Registered Nurse (RN) Employee Health PSJH -...
…is calling for a Registered Nurse (RN) Employee Health PSJH to our location in Apple Valley, CA.** We are seeking a Registered Nurse (RN) Employee Health PSJH to be Read more
Systems Administrator - Pearson (United State...
…and troubleshoot Windows operating systems (workstation and server), laptop computers, Apple iPads, Chromebooks and printers** + **Administer and troubleshoot all Read more
IT Assistant Level 1- IT Desktop Support Anal...
…providing tier-1 or better IT help desk support in a large Windows and Apple environment * Experience using IT Service Desk Management Software * Knowledge of IT Read more
Human Resources Business Partner PSJH - Provi...
…**is calling a** **Human Resources Business Partner, PSJH** **to our location in Apple Valley, CA.** **Applicants that meet qualifications will receive a text with Read more
Manager Community Health Investment Programs...
…is calling a Manager Community Health Investment Programs PSJH to our location in Apple Valley, CA.** **Qualified candidates will be invited to do a self-paced video Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.