Monkey Kong: a machine code analysis

Software and Games for the Lambda 8300 / Power 3000 Computer
Post Reply
David G
Posts: 548
Joined: Thu Jul 17, 2014 7:58 am
Location: 48 North

Monkey Kong: a machine code analysis

Post by David G »

Monkey Kong: a machine code analysis

Wow, game music is vastly improved over SQUEZEE PLAY (another SBS title). Now it has clear tones instead of buzzing tones. This is using the MUSIC feature of Lambda BASIC. Oh wait, after the introductory song, no MUSIC during game play, only buzz sounds

Another very nice game from Denmark's Scan-Bit Software. It's a clone of Donkey Kong but with a compelling storyline of "gold guarded by the fearsome MONKEY KONG, who will roll large boulders down towards you. You have to deftly jump over them."

The game details can be found in the Software Preservation topic

The delay on first running, while you see a blank screen, is the music playing ...

Then you see a menu screen with for options. Choose the game level (1-4) and there is a lengthy delay before the screen appears. This is due to configuring all four levels before the chosen level starts (configuration begins at Line 90)


Some comments on the machine code:

Uses no ROM calls

For such a slick game it uses a lot of BASIC code. I'd classify it as a Machine Code/BASIC hybrid, instead of just using BASIC to launch the game. Many games use BASIC to set up menus, etc. before the game starts, but Monkey Kong goes in and out of BASIC while the game is running (for example, every time the player dies). It is not obvious that this is occurring as it goes back into machine code


MKONG has four level and four corresponding Display Files in REM 1. It doesn't switch to these, instead it copies them to the standard Display File and in a couple of places it refers back to them looking for special "marker" characters. Used more like templates
SCREEN1 (1st level/lane) template
SCREEN1 (1st level/lane) template
KEYBOARD CONTROL

KEYS
7 left
F right
4 up
R down
U jump [aka fire]

Any idea why these keys? They are not layed out on the keyboard in a particularly easy-to-use pattern.

Code: Select all

4     7
 R     U
  F
However, in code, these correspond to sequential bits of LAST_K. Also in SQUEZEE they are all BIT 4 of the IN FE results. These keys also match the Lambda's joystick port configuration, and the game uses the same ones for keyboard control

Speaking of joystick, the game defaults to having one. In the OPTIONS you may set JOYSTICK to N, which swaps the left/right keys, resulting in a slightly more logical keyboard usage


DISASSEMBLY

1. Disassemble using ZX Lister 6.5. Use option ZX-IDE with Disassembly. Note that often, the disassembly will not be correct around areas of data. But in the case on Monkey Kong it works right off the bat. For programs that won't compile at first, you can use DISASSEMBLY + BYTE ASM to give a quick compilable ASM file, and uncomment the Assembly one REM at a time until you get it working

(OPTIONAL) missing LET (or THEN LET). Lambda doesn't require them, but the KEYWORD is required by the ZX-IDE. ZX Lister 6.5j BETA (new version 2024/9) provides custom ASM lines for these lines. Or just add the LETs and delete the custom code

2. Identify the entry point of the machine code. Examine the BASIC listing for the USR statements that call into the machine code

220 RAND USR 20498
370 RAND USR 20764

NOTE: If the disassembly does not show code starting at these addresses, fix that up ... adjust a few lines ... But in the case on Monkey Kong it's OK!

3. Then start guessing what the code does, adding labels here and there. The first few tentative guesses will lead to more and more discoveries and eventually it will get easier

4. Determine where the keyboard is being read:
Look for LAST_K or IN A,(0FEH)
Then from this you it may be possible to figure out which subroutines are associated with each key

In this case, MKONG is not waiting for keys, but using the machine code equivalent of INKEY for smooth action. It reads LAST_K for this


RANDOM NUMBERS

MKONG uses FRAMES to get a RND number - as do many programs. But it is does something more complicated, it does a double RAND sort of function. BASIC stuffs RND*2000 into a memory location, and when the m/c needs a random number it adds the current FRAMES to the high byte, then adds the same to the low byte, resulting in a new RND byte. Then it store the modified word back to memory. Does this improve the randomness, or is there some other purpose for it? It uses this number solely to generate random chances for object generation and movement


SOUND EFFECTS

* MUSIC tune played on first start (with a blank splash screen). Also plays after OPTIONS screen, if RESET HI-SCORE is chosen. This option choice re-RUNs the game
* Tick-tick for each frame of the game
* 4-second BUZZ on dying


OBJECT TRACKING

Interestingly there are no variables tracking any objects other than the PLAYER. Instead, MKONG searches the entire display file for certain characters to update them. This is very fast using the machine code instruction CPDR (Compare and Decrement Repeated)

For example, the subroutine LIGHTNG looks for the existing BOLTS and when it finds one, it moves it. In the case of Lightning, it moves it diagonally. Then REPEAT until all the bolts have been updated. The same mechanism is used for the rolling BOULDERS, GHOSTS, ELEVATORS, and VAMPIRES ('>' and '<' characters)


instructions translated
INSTRUCTIONS FOR USE MONKEY KONG 16K LOAD >>SBS<<

You are hunting an ancient chest filled with gold that is hidden in Ghost Mountain. Ghost Mountain teems with monsters and beasts. The gold is guarded by the fearsome MONKEY KONG, who will roll large boulders down towards you. You have to deftly jump over them.

Level 1: Here you meet MONKEY KONG, you have to climb up the mountain and reach the entrance at the top (A). On the way you can collect gold nuggets, they are marked with numbers. Jump up after them, you get the number * 10 points.

Lane 2: If you clear LANE 1 and come through the entrance, you will enter the GREAT HALL. Here you meet MONKEY KONG and GHOSTS who help him. If you are in trouble, you can take a rescue elevator down, it gives good points.

Lane 3: Now it gets hard!!! You are in the LABYRINTH CAVE; MONKEY KONG has been helped by the VAMPIRES, who are chasing you with open arms. Fight your way to the TREASURY CHAMBER.

Lane 4: Now you are finally in the TREASURE CHAMBER, here the fire of the sky flashes down on you. BEWARE!!! MONKEY KONG puts all his efforts into stopping you, and the path to the treasure is difficult: first you have to pass the LIGHTNING, then past the VAMPIRES, then up the ELEVATOR, past the VAMPIRES again, down the ELEVATOR, past the GHOSTS and past MONKEY KONG, before you reach the chest. But then MONKEY KONG IS DEFEATED!!!

The goal on the courses is marked with an A. The game requires 16K. Can be used with and without Joystick (see the instructions in the game, point 3). The game has HI-SCORE leaderboard. Do not use >>reset<< or >>run<<, but restart with >>go to 0<<.
Attachments
SBS.asm
ASSEMBLY & BASIC FILE FOR ZX-IDE
(71.52 KiB) Downloaded 219 times
Post Reply