Assembly number guessing game

The re-write of the x86 Assembly version of NumGuess was long overdue. 15 years ago when I wrote the original code I didn’t think about standardisation or guidelines, just wanted to make it work. As the NumGuess project took off and received numerous contributions, the old assembly code became the weakest link.

A couple of weeks ago I decided to jump back into 16 bit x86 assembly programming to bring it in line with the rest of the project. It was clear from the start that it needed a complete re-write as the whole code was useless. Nibbling away at it in my free time, it is finally complete. Today I give you not just the guideline compliant 286 DOS code, but an improved 386 DOS version that uses 32 bit registers as well, which pushes the total number of NumGuess implementations to 40.

Random number generator

The original had no such thing. As the limit was hard-coded to 100, it simply queried the current time, which was accurate to 100th of a second. With the guideline’s requirement of a dynamic limit, that was no longer feasible, and an RNG was needed that gave believable randomness.

I chose MINSTD, which is a simple Lehmer RNG with a multiplier of 48271, which conveniently fits into a word register, the largest on the 16-bit platform I was aiming for. Unfortunately I still had to do some arithmetic calculations for up to 47 bits, which needed some extra work-arounds and code branching on 286.

The largest number it generates is 2147483646, one less than the maximum positive value of a 32 bit signed integer. This is the current limitation for all numbers in both versions.

The 386 version

The 386 version is just a simple improvement on the 286 one, taking advantage of the 32 bit registers that are available. To avoid using protected mode and keep it simple, it still uses 16-bit segments, but with the simplified calculations the code is about 140 lines shorter.


To compile the code, you’ll need the DOS versions of either TASM or MASM. The easiest way is the Tasm for Windows project on SourceForge, which bundles it with DOSBox. Compile and link with the following commands to generate an executable:

tasm num_286
tlink num_286

If you prefer MASM, you can download it from here, then:

masm num_286.asm
ml num_286.obj

Enjoy the latest addition to the NumGuess family, comments and suggestions are welcome. If you find a bug, please submit an issue on GitHub.

Share Button
Posted in Blog Tagged with: , ,

Leave a Reply