sábado, 5 de diciembre de 2026

Reversing a DOS Game (3/3)

This is a series of 3 posts explaining the experience of reverse engineering a DOS game. The method used is far from sophisticated. Basically disassembling and inspecting the instructions. While live debuggers or live memory inspection through some emulator would bring you faster to these same conclusions, in this exercise we are only using ndiasm, grep and an hex editor to make modifications.

So if you read the previous entry, we discovered the potential adddress that represents the Energy Bar. It is one of the most interesting features to hack, and the address is 0x3ef:

$ cat Indy.asm | grep -i \\[0x3ef

00005A36  C606EF0337        mov byte [0x3ef],0x37
00005BAA  8A0EEF03          mov cl,[0x3ef]
00005BC9  A0EF03            mov al,[0x3ef]
00007E8E  803EEF0300        cmp byte [0x3ef],0x0
00008135  803EEF0300        cmp byte [0x3ef],0x0
0000813C  802EEF030A        sub byte [0x3ef],0xa
00008143  C606EF0300        mov byte [0x3ef],0x0
00008659  2806EF03          sub [0x3ef],al
0000865F  C606EF0300        mov byte [0x3ef],0x0
0000866B  803EEF0300        cmp byte [0x3ef],0x0
0000A630  A0EF03            mov al,[0x3ef]
0000A63A  A0EF03            mov al,[0x3ef]
0000A64E  A0EF03            mov al,[0x3ef]

Here we can see the initial life of Indy: 0x37(55):

00005A36  C606EF0337        mov byte [0x3ef],0x37 

Again as in previous post, an easy one would be replacing 0x37 by 0xFF(255) that is still 5 times more energy, but not infinite. Empiricaly, it creates a problem in the dash, as the UI does not know how to draw such a long Energy Bar(notice the black rectangle filled in the same row of the actual bar):

Several places we can see how this value gets compared to 0 to check if Indy has to die, and subtract one LIFE..

0000866B  803EEF0300        cmp byte [0x3ef],0x0

A bit harder to understand is this one:

00008143  C606EF0300        mov byte [0x3ef],0x0

There are some actions in the game where you immediately die, touching an opponent, or falling in an uncontrolled area of the level are some examples. Latter I saw that this instructions are also used to avoid a subtraction leaving the memory in a negative/overflown number.

Ok, but we want to avoid decreasing/subtraction, so the ones we are looking for are:

0000813C  802EEF030A        sub byte [0x3ef],0xa
[...]
00008659  2806EF03          sub [0x3ef],al

While first instruction decreases a fixed amount of life 0xA(10), the second is defined by the lower part of the AX register. Clearly Energy is not decreased by fixed chunks.

This time if you try to inhibit the instructions by replacing with NOPs the following happens:

Indy dies at any first touch, quite the opposite we wanted, but quite related at the same time...

We need to check what is going on around this instructions (this is not a grep output but an actual code sequence, with my best interpretation):

00008135  803EEF0300        cmp byte [0x3ef],0x0 # Are we dead?
0000813A  7411              jz 0x814d            # Go handleDead()  
0000813C  802EEF030A        sub byte [0x3ef],0xa # Subtract energy
00008141  7305              jnc 0x8148           # if not negative go Ahead()
00008143  C606EF0300        mov byte [0x3ef],0x0 # energy := 0
00008148  56                push si            
00008149  E8D324            call 0xa61f          # Ahead()
0000814C  5E                pop si
0000814D  C6440AFE          mov byte [si+0xa],0xfe # handleDead()

The problem is the JNC (jump not carry) instruction, which is directly affected by us inhibiting the previous SUB instruction, as there is a Carry flag not being updated.

The fact of Indy's death at the first touch is represented by the instruction at 00008143,whatever happened, or whatever got subtracted, energy := 0

Therefore, we need to understand what are doing those instructions, and their updates to the system flags. The target is that the JNC branch, needs to happen. There could be different ways to force it. 

Note that the instructions we replace need to take the same amount of space. Therefore the one I chose was to still inhibit the SUB operation, but ensure the Carry flag is unset with the instruction CLC(0xF8) so JNC will always take the same decision. Other possibility is to force the JNC to a JMP, but it would still require to NOP the SUB instruction.

Therefore the 802EEF030A becomes 90909090F8: 4xNOP + 1xCLC.
Similarly 2806EF03 needs to become 909090F8: 3xNOP + 1xCLC.

And placing everything together discussed in the previous posts:



I wonder if there are more useful things that could be tweaked, A summary of the patterns to be changed, if someone beats it, please place a screenshot in the comments, I forgot already...

PS: Always wondered what were those red tiles on the bottom right...