TweetFollow Us on Twitter

June 93 - KON & BAL'S PUZZLE PAGE

KON & BAL'S PUZZLE PAGE

FINDER++

KONSTANTIN OTHMER AND BRUCE LEAK

[IMAGE 127-132_Puzzle_Page_rev1.GIF]

See if you can solve this programming puzzle, presented in the form of a dialog between Konstantin Othmer (KON) and Bruce Leak (BAL). The dialog gives clues to help you. Keep guessing until you're done; your score is the number to the left of the clue that gave you the correct answer. These problems are supposed to be tough. If you don't get a high score, at least you'll learn interesting Macintosh trivia.


KON I'm trying out this new C compiler to see what we can do to make the system and Finder smaller and faster.

BAL Wait. Since when has the Finder been written in C?

KON It's better than that! It's actually C++, with some assembly routines so that we can claim the copyright goes back to 1983.

BAL Oh, that explains how the System 7 Finder got so much bigger. I thought it was just the About box. I'm running System 6 on my PowerBook 100. I'd sure like to get a smaller and faster version of System 7. Are you making any progress?

KON Well, yes and no. The compiler output is certainly smaller but I haven't nailed down how much faster it is. When I boot up and the Finder launches, the machine restarts, which relaunches the Finder, which causes the machine to restart, and so on. It all happens pretty fast but doesn't seem all that useful.

BAL What machine is this on?

KON Macintosh Classic -- the original, not the Macintosh Classic II.

BAL Somehow the compiler is generating bogus code that causes the system to restart. So I compare the code from the new compiler to the code from the old compiler, look at the differences, and see if they make sense.

KON Everything's different: 42,000 bytes went away and the rest is totally different. This isn't a minor compiler revision. We're talking Advanced Technology here. Where are you going to look?

BAL OK, OK. Let's debug it. I set an ATB on _Launch and then another on _InitGraf.

KON OK. You break at _Launch and then after you Go you break at _InitGraf.

BAL I set an ATB on _WaitNextEvent.

KON You break at _WaitNextEvent.

BAL I say Go and see if I get back to _WaitNextEvent again.

100 KON The machine reboots almost immediately.

BAL I go back to the same place and instead of saying Go I trace over _WaitNextEvent.95

KON The machine crashes into MicroBug. But it's not your ordinary crash into MicroBug. The screen is trashed and you can't type anything. But it looks as though MicroBug is trying to come up.

BAL Can I hit the NMI button?

90 KON You can press it all you want, but it doesn't do anything. And, by the way, G -1 doesn't work either.

BAL Hmmm. It seems as if something is seriously wrong with _WaitNextEvent. Did you recompile the Process Manager, any DAs, or other stuff?

85 KON Nope, I only recompiled the Finder. When I get that working, I'll get around to the rest.

BAL So you didn't recompile the Finder extensions? Since the C++ virtual function tables are different, all your existing Finder extensions are incompatible and maybe that's what's hosing you.

80 KON None of the extensions are active, and even if they were, the Finder verifies their versions. What do you expect? They're object oriented. Of course it works.

BAL Of course. Well, since we couldn't make it across _WaitNextEvent, let's step into it.

75 KON As soon as you step, you get the same weird crash into MicroBug.

BAL I just step into it?

KON Yes.

BAL As soon as I step, pending interrupts come in and kill me. So I disable interrupts with an SR = 27000000 and try stepping again.

70 KON Same crash.

BAL Seems like there might be something wrong with MacsBug.

KON Let me make sure I'm following you here. Only the Finder is recompiled and you blame the strange crashes on MacsBug? I'm going to have trouble selling that one.

BAL Clearly there's something wrong with the recompiled Finder. It's probably trashing MacsBug memory.

65 KON Come on. MacsBug does some sort of a checksum on itself and tells you if it's been altered. When you break at _WaitNextEvent, you don't get any messages to that effect.

BAL You got me there, KON. So you're saying that MacsBug is in perfect working order at this point. I can do an IL or whatever, but if I step I'm dead?

KON Perfect working order? Same as it ever was. But the Surgeon General has determined that stepping or tracing at this point causes ill effects.

BAL This is not my beautiful MacsBug. If I trace after I hit _InitGraf, is everything fine?

60 KON No problem.

BAL So I do an

ATB ';t ;g'

which breaks on every trap, traces over it, and then continues. That way I can see what the last trap I hit was.

55 KON The machine runs for a while, but when you crash and burn into MicroBug, you lose your MacsBug screen.

BAL Fine. I set up another screen, put MacsBug on that screen using the Monitors control panel, and use the SWAP command so that MacsBug is always visible. That way when I crash I can see what just happened.

KON Great strategy for a modular Macintosh, but this is on a Macintosh Classic. I'd let you figure it out that way except you used up your whole budget flying to North Dakota a few puzzles ago.

BAL I was hoping you'd forget that. OK, fine. Someone must be trashing low memory, so I'll use Bo3b Johnson's totally awesome Blat dcmd. It'll catch any read or write from memory locations $0-$100.

50 KON You're on a Macintosh Classic, which doesn't have an MMU. That dcmd works via the MMU.

BAL KON! Those correspondence classes are finally paying off. So I'll narrow down the area that's causing the problem by doing an ATB 10 to skip over 16 ($10) traps at a time until the machine crashes into MicroBug. If it takes five times to crash, the next time I'll do an ATB 40, and then an ATB 4, until it crashes. After I do this enough times I'll know what was the last trap that was successfully executed, and I can go from there.

45 KON Rather than crashing, the machine is now rebooting.

BAL OK, so what's the last trap called before the machine reboots?

40 KON _WaitNextEvent.

BAL Fabulous. Déjà vu. Is this a Never Ending Story? And when I'm at _WaitNextEvent I can't step or trace or anything?

KON Well, you can't step or trace. That's all you've tried so far.

BAL So I set a breakpoint on the first instruction of _WaitNextEvent and say Go.

35 KON You crash into MicroBug, just like before.

BAL OK, what's the current score? Can we call it quits?

KON I wouldn't say you aced this one. Luckily we're getting paid per word, so let's keep going.

BAL But when I was at _InitGraf, I could trace. So something's hosing MacsBug between _InitGraf and _WaitNextEvent. I'll do the ATB 10 trick like before, but this time I'll try tracing after every break. That way I can figure out where MacsBug is getting mauled.

30 KON You figure out that you can trace over a call to _InitWindows, but when you trace over the next trap, a call to _GetResource, you crash into MicroBug.

BAL So I go to _InitWindows and trace until I get to the call to _GetResource. If it's a long way, I do a T 1000. If that crashes, I reboot and do a T 500, then a T 250, and so on, until I find the offending instruction.

25 KON The offending instruction is a

MOVE.L d0,20(a2)

BAL What's in A2?

20 KON $100.

BAL Writing to low memory like this sounds like a bad idea. My guess is that A2 is trashed and we're pounding an important vector. What's at $120?

15 KON That's MacJmp.

BAL Aha! MacJmp is the vector that exception code uses to go to the debugger. Once you trash that, all bets are off.

KON Yeah, setting ATBs works because MacsBug patches the trap dispatcher and looks for the A-traps you have breaks on. If it encounters one, it just drops into MacsBug directly. Other breakpoints are set by replacing the existing instruction with a trap instruction. When these instructions are processed, they go through MacJmp. When MacJmp gets trashed, tracing and stepping and setting breakpoints no longer works, as we found out.

BAL Nasty.

KON Don't try to finish up so fast! You still haven't figured out why the machine is rebooting.

BAL The new compiler must do a better job of register allocation and actually use them all in its optimizations. Some Finder glue routine you called must have trashed A2.

KON Exactly. An easy problem to fix, though. The Finder was calling an assembly routine that hammered A2. After you fix the bug and build a new Finder, the machine still restarts.

BAL So I set an ATB on _WaitNextEvent, since that was as far as we got last time, and try to trace over it.

10 KON OK. No problem.

BAL Whew! Finally I get past that _WaitNextEvent. Let's go for two. I say Go and see if we hit _WaitNextEvent again.

KON Nope. The machine restarts.

BAL After the first _WaitNextEvent I do the trick with T 1000, T 500, T 250, and so on, until I find the offending instruction or subroutine. If the problem is occurring in a subroutine, I go into it and do the same thing. At some point this process has to stop and I'll find the problem instruction.

5 KON The offending instruction is an

LEA 13(a7),a7

BAL Well, that's bogus. Using an odd address on a 68000 will cause an address error.

KON Yeah, but the machine is rebooting.

BAL I get it. It's an odd address in the stack pointer. The Macintosh gets an address error because of the odd address. When it goes to process the exception, the exception handler gets an address error trying to push the exception frame onto the stack. If the Macintosh ran in user mode, it wouldn't have this problem, since it could switch to supervisor mode -- essentially a clean machine with a properly aligned stack pointer -- to handle the exception. But since it runs in supervisor mode, hosing the stack pointer messes the machine up to the point where it can't even handle an exception, so it reboots.

KON Yeah. We were working on cleaning up the stack after function calls in the compiler and had a small problem with the way Booleans are handled. Since a Boolean is only a char, which is one byte, the compiler thought it needed to clean up an odd amount of space from the stack. Once we explained to the compiler that stacks must be word aligned, the problem went away.

BAL Two bugs in one Puzzle Page!

KON Nasty.

BAL Yeah.

KONSTANTIN OTHMER AND BRUCE LEAK Long-time Puzzle Page fan Al Gore recently invited KON and BAL to upgrade the White House situation room to BALKON-4, the latest in networked Spaceward Ho! technology. On his lunch hour, KON debugged Clinton's economic plan and found the memory leak that was causing that $50 billion Medicare shortfall. BAL is now working on an audio-animatronic Silicon Valley executive so that the President can always have one at his side.*

Bo3b Johnson's Blat dcmd can be found on this issue's CD and on the E.T.O. disc. (The "3" in Bo3b's name is silent.) Blat is written up in the Macintosh Debugging article in Issue 13 of develop.*

SCORING

  • 75-100 How long have you been a member of the Liar's Club?
  • 50-70 Sharpshooter. You win the (virtual) kewpie doll.
  • 25-45 A valiant effort. These puzzles are hard!
  • 5-20 Brush up for Issue 15's Puzzle Page.*
Thanks to Gary Davidian, scott douglass, and Jean-Charles Mourey for reviewing this column. *
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Scrivener 3.0 - Project management and w...
Scrivener is a project management and writing tool for writers of all kinds that stays with you from that first unformed idea all the way through to the first - or even final - draft. Outline and... Read more
SuperDuper! 3.0.1 - Advanced disk clonin...
SuperDuper! is an advanced, yet easy to use disk copying program. It can, of course, make a straight copy, or "clone" -- useful when you want to move all your data from one machine to another, or do... Read more
Parallels Desktop 13.2.0 - Run Windows a...
Parallels allows you to run Windows and Mac applications side by side. Choose your view to make Windows invisible while still using its applications, or keep the familiar Windows background and... Read more
VueScan 9.5.92 - Scanner software with a...
VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more
iFinance 4.3.4 - Comprehensively manage...
iFinance allows you to keep track of your income and spending -- from your lunchbreak coffee to your new car -- in the most convenient and fastest way. Clearly arranged transaction lists of all your... Read more
jAlbum Pro 15.0 - Organize your digital...
jAlbum Pro has all the features you love in jAlbum, but comes with a commercial license. You can create gorgeous custom photo galleries for the Web without writing a line of code! Beginner-friendly... Read more
jAlbum 15.0 - Create custom photo galler...
With jAlbum, you can create gorgeous custom photo galleries for the Web without writing a line of code! Beginner-friendly, with pro results - Simply drag and drop photos into groups, choose a design... Read more
Duet 1.6.9.3 - Use your iPad as an exter...
Duet is the first app that allows you to use your iDevice as an extra display for your Mac using the Lightning or 30-pin cable. Note: This app requires a $14.99 iOS companion app. Version 1.6.9.3:... Read more
Duet 1.6.9.3 - Use your iPad as an exter...
Duet is the first app that allows you to use your iDevice as an extra display for your Mac using the Lightning or 30-pin cable. Note: This app requires a $14.99 iOS companion app. Version 1.6.9.3:... Read more
iExplorer 4.1.10 - View and transfer fil...
iExplorer is an iPhone browser for Mac lets you view the files on your iOS device. By using a drag and drop interface, you can quickly copy files and folders between your Mac and your iPhone or... Read more

Latest Forum Discussions

See All

The mobile gamer's guide to Black F...
We're starting to catch wind of some exciting deals in the mobile gaming space for Black Friday. There are big discounts on mobile phones and accessories cropping up already, so you might want to get a move on things ahead of the big day. It's... | Read more »
The best pre-Black Friday deals - Novemb...
Black Friday will soon be upon us, but online retailers are already getting a headstart on the steep discounts. Don't wait until Friday—you'll find some pretty good deals all over the internet without waiting in lines or competing with other... | Read more »
Mighty Battles guide - how to build a so...
Mighty Battles, the latest title from Hothead Games, is set to take the App Store by storm. The game puts a welcome twist on lane battlers, adding FPS elements to spice things up a bit. You'll collect cards to put your own military unit to gether,... | Read more »
Rules of Survival guide - how to be the...
The PUBG craze makes its way to mobile, with more and more battle royale games debuting on iOS and Android. Rules of Survival joins the ranks of mobile PUBG-likes, offering a classic battle royale experiences that doesn't vary too much from its... | Read more »
The best new games we played this week -...
The weekend is upon us friends, and it's time to take a look back and reflect on all of the wonderful games we've played over the past few days. This week was jam packed with new releases. There were some big, long awaited launches, some fun... | Read more »
Lineage II: Revolution guide - tips and...
At long last, Lineage II: Revolution has now come to western shores, bring Netmarble's sweeping MMORPG to mobile devices. It's an addictive, epic experience, but some of the systems in the game can be a bit overwhelming. Here are a few tips to help... | Read more »
A Boy and His Blob (Games)
A Boy and His Blob 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: | Read more »
Fight terrible monsters and collect epic...
Released on Western markets early last month, Dragon Project, created by Japanese developer COLOPL, brings epic monster hunting action to mobile for the very first time. Collect a huge array of weapons and armor, and join up with friends to fight... | Read more »
I Am The Hero (Games)
I Am The Hero 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: I Am The Hero is a pixel art, beat 'em up, fighting game that tells the story of a "Hero" with a glorious but mysterious past.... | Read more »
Kauldron (Music)
Kauldron 1.0 Device: iOS Universal Category: Music Price: $3.99, Version: 1.0 (iTunes) Description: Kauldron is our warmest sounding, punchiest synth yet! A completely new modeling technology, combined with carefully designed... | Read more »

Price Scanner via MacPrices.net

Save up to $180 with Apple Certified Refurbis...
Apple has Certified Refurbished 2017 13″ MacBook Airs available starting at $849. An Apple one-year warranty is included with each MacBook, and shipping is free: – 13″ 1.8GHz/8GB/128GB MacBook Air (... Read more
Black Friday deals on Apple Macs now live at...
Amazon has MacBook Pros, MacBook Airs, MacBooks, and iMacs on sale for up to $200 off MSRP for Black Friday week. Shipping is free. Note that stock of some Macs may come and go during the week, so... Read more
Black Friday pricing on Macs and iPads now av...
B&H Photo has lowered prices on many Macs, iPads, and iPad Pros as part of their Black Friday week sale. Save up to $200 on MacBooks and iMacs and up to $150 on iPads. B&H charges sales tax... Read more
Best Apple iPad deals this weekend, up to $80...
Apple resellers are offering 9.7″ iPads and 10.5″ iPad Pros for up to $80 off MSRP this weekend as part of their early Holiday and Black Friday sales: Adorama is offering new 2017 9.7″ 32GB WiFi... Read more
Early Black Friday sale: Apple iMacs for up t...
B&H Photo has 27-inch iMacs in stock and on sale for up $130-$150 off MSRP including free shipping. B&H charges sales tax in NY & NJ only: – 27″ 3.8GHz iMac (MNED2LL/A): $2149 $150 off... Read more
Apple restocks refurbished Mac minis starting...
Apple has restocked Certified Refurbished Mac minis starting at $419. Apple’s one-year warranty is included with each mini, and shipping is free: – 1.4GHz Mac mini: $419 $80 off MSRP – 2.6GHz Mac... Read more
Save on 12″ MacBooks, Apple refurbished model...
Apple has Certified Refurbished 2017 12″ Retina MacBooks available for $200-$240 off the cost of new models. Apple will include a standard one-year warranty with each MacBook, and shipping is free.... Read more
Early Holiday sale: 12″ iPad Pros for up to $...
B&H Photo has 12″ iPad Pros on sale today for up to $130 off MSRP. Shipping is free, and B&H collects no sales tax outside NY & NJ: – 12″ 64GB WiFi iPad Pro: $749, save $50 – 12″ 256GB... Read more
Holiday sale prices on Apple 13″ MacBook Pros...
B&H Photo has 2017 13″ MacBook Pros in stock today and on sale for $100-$150 off MSRP, each including free shipping plus NY & NJ sales tax only: – 13-inch 2.3GHz/128GB Space Gray MacBook Pro... Read more
Sale: 13″ MacBook Airs starting at $899, $100...
B&H Photo has 2017 13″ MacBook Airs on sale today for $100 off MSRP including free shipping. B&H charges NY & NJ sales tax only: – 13″ 1.8GHz/128GB MacBook Air (MQD32LL/A): $899, $100 off... Read more

Jobs Board

*Apple* Retail - Multiple Positions - Apple,...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Product Manager - *Apple* Pay on the *Appl...
Job Summary Apple is looking for a talented product manager to drive the expansion of Apple Pay on the Apple Online Store. This position includes a unique Read more
*Apple* Pro/Consumer Apps Support Engineer -...
…exemplify AppleCare's expert technical support paired with exceptional customer service for Apple 's software apps. This person is a problem solver, who understands Read more
Partner Marketing Manager, *Apple* Pay - Ap...
Job Summary The Apple Pay partner marketing team is looking for a Marketing Manager to develop and drive US programs. The right candidate will be passionate about Read more
*Apple* Solution Consultant - Apple (United...
# Apple Solution Consultant - Rochester, MN Job Number: 113037950 Rochester, MN, Minnesota, United States Posted: 19-Sep-2017 Weekly Hours: 40.00 **Job Summary** Are Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.