Learn Multi platform 8086 Assembly Programming... For World Domination!

Multi-Platform Series

The Multiplatform series covers general 8086 code which will work on any system. Each lesson will cover a concept, and provide code which you can use in your project if you wish

Lesson M1 - Random Numbers and Ranges
Let's write some simple routines we may need in a game!

We'll write a psuedo random number generator, which can create repeatable 16 bit random numbers.
We'll also create a 'Range checker' which can be used as a collision detection routine for unsigned 8 bit numbers


The Random number generator shown today is far from the 'Best'... but it does work!
It's used in these tutorials by all the games as it's been tested without any problems, and has been ported to many different systems and CPUs, and given the same random seed, it gives the same results... meaning it can be used for generating random levels in your game, and the level will be the same on every system.


Random number generation
To allow our random number generator to create good varied results, we use a pair of 16 byte lookup tables
Our random number generator produces a 16 bit result (DX) from a 16 bit random seed (BX)

The 'High byte' of the 16 bit pair is created by bitshifting and combining the two.
We need the 'Low byte' to be very different, or the 16 bit values won't be very random!

We use the lookup tables to get some new data, and combine these to produce the second value
We have two routines we can use to get values

"DoRandomWord" will get a 16 bit value zHL from seed zBC, this can be used to produce 'psuedorandom' data for times we want repeatable results.

"DoRandom" is an easy function to return an 8 bit byte in the Accumulator... it automatically updates its seed every time.

To be usable, a random number generator needs to produce every possible random number (0-255 or 0-65535)... otherwise you could have serious problems with your program (if you're waiting for a result that never happens).

The more 'random' the data the better... that is, if you plotted the values on a graph there should be no patterns present.


Range checking
We need to do 'Collision detection'.

We specify a two targets and a 'range' ... if target 1 is within 'Range' of target 2, we return A=1... if it's out of range we return A=0...
This means we can do a BNE or BEQ on return.


The range check is simple... we test X and Y axis

We test the X axis first.

we subtract the 'RANGE' from each axis of the object, and check if we went below Zero (if so that direction is over the limit)... we then compare to the position... if it's lower we're out of range... if not we need to test more!

Next we add 'RANGE' twice... once to move back to the center and once to move to the right... we then compare to the position... If it's higher we're out of range... if not we need to test the Y axis!

We repeat the same tests for the Y axis.


Lesson M2 - Binary Coded Decimal
Working in HEX is great for programming, but converting 16 or 32 bit hex to numbers for display is a pain!

We'll want to show an 8 digit 'highscore' in our games... and for this we'll use Binary Coded Decimal!

BCDTest_ASCII.asm
BCDTest_PACKED.asm



To pack or not to pack... that is the question!
BCD comes in two forms... Unpacked (AKA ASCII on the 8086) which stores one digit per byte... or Packed which stores 2 (one per nibble)
We'll try both in this lesson.

Unpacked Binary Coded decimal (ASCII)

Unpacked Binary coded decimal uses one digit per byte, so 8 digits require 8 bytes.

As ASCII 0-9 is 30h-39h The 'ASCII' commands will work either with raw numeric values... however after the 'correction commands' (AAA /AAS etc) the top nibble will change to 0, so we need to OR in 30h
To display unpacked BCD it is simply a case of showing each digit to the screen, starting with the most significant digit.

If we're using Unpacked values, we need to OR in 30h (The ASCII for '0')


Here are the results
If we want to compare two BCD strings, it's pretty easy to do so, we move to the most significant digit, and compare each, until we find the first that does not match - returning the flag state when we do.

If we get to the end of the sequence without finding a difference, the two strings are the same.


In this case, the first string was larger than the second (NC)
if we wish to add two binary coded decimal values, we can just use the regular ADD/ADC commands, however after the command we need to 'adjust' the value with AAA (Ascii Adjust for Addition)...

Note this 'removes' the top nibble, so Ascii '0' will be converted to unpacked 0... we need to OR in 30h to fix this before display. 


Here we repeatedly added '104'
Subtraction is the same... we use SUB or SBB  however this time we use AAS to 'adjust' the value (Ascii Adjust for Subtraction)

Here we repeatedly subtracted '104'
Multiplication works differently, we can use the MUL command, followed by AAM, however, only AL is processed so only 2 digits are supported.
Here are the results... notice the values went 4,8,16 fine, but then instead of 16*2 we ended up doing 6*2
For Divide we have to work differently, firts we use AAD, which converts our ASCII value to normal HEX, then use the DIV command as usual
Here are the results!... first AAD converted Ascii value 24 to hex value 18h, then we did our divides

Packed Binary Coded decimal

Packed Binary coded decimal uses one nibble per byte, so 8 digits require 4 bytes.

The top nibble is the top digit in a pair... so 12 in decimal is 12h in BCD
To display Packed BCD We need to split up the two nibbles, and show them to the screen one at a time.


Here are the results
If we want to compare two BCD strings, it's pretty easy to do so, we move to the most significant digit, and compare each, until we find the first that does not match - returning the flag state when we do.

If we get to the end of the sequence without finding a difference, the two strings are the same.


In this case, the first string was larger than the second (NC)
if we wish to add two binary coded decimal values, we can just use the regular ADD/ADC commands, however after the command we need to 'adjust' the value with DAA (Decimal Adjust for Addition)


Here we repeatedly added '104'
Subtraction is the same... we use SUB or SBB  however this time we use DAS to 'adjust' the value (Decimal Adjust for Subtraction)

Here we repeatedly subtracted '104'

Don't go looking for BCD commands for packed Multiplication or Division... there aren't any!

The Ascii ones weren't so useful anyway, and we won't need them for scorekeeping in our games.



 

View Options
Default Dark
Simple (Hide this menu)
Print Mode (white background)

Top Menu
***Main Menu***
Youtube channel
Patreon
Email Newsletter
Merch Store
Amazon Affiliate Link
Forum
AkuSprite Editor
Dec/Bin/Hex/Oct/Ascii Table

Alt Tech
Archive.org
Bitchute
Odysee
Rumble
DailyMotion
Please note: I wlll upload more content to these alt platforms based on the views they bring in

Z80 Content
***Z80 Tutorial List***
Learn Z80 Assembly
Hello World
Advanced Series
Multiplatform Series
Platform Specific Series
ChibiAkumas Series
Grime Z80
Z80 Downloads
Z80 Cheatsheet
Sources.7z
DevTools kit
Z80 Platforms
Amstrad CPC
Elan Enterprise
Gameboy & Gameboy Color
Master System & GameGear
MSX & MSX2
Sam Coupe
TI-83
ZX Spectrum
Spectrum NEXT
Camputers Lynx

6502 Content
***6502 Tutorial List***
Learn 6502 Assembly
Advanced Series
Platform Specific Series
Hello World Series
Grime 6502
6502 Downloads
6502 Cheatsheet
Sources.7z
DevTools kit
6502 Platforms
Apple IIe
Atari 800 and 5200
Atari Lynx
BBC Micro
Commodore 64
Commander x16
Super Nintendo (SNES)
Nintendo NES / Famicom
PC Engine (Turbografx-16)
Vic 20

68000 Content
***68000 Tutorial List***
Learn 68000 Assembly
Hello World Series
Platform Specific Series
Grime 68000
68000 Downloads
68000 Cheatsheet
Sources.7z
DevTools kit
68000 Platforms
Amiga 500
Atari ST
Neo Geo
Sega Genesis / Mega Drive
Sinclair QL
X68000 (Sharp x68k)

8086 Content
Learn 8086 Assembly
Platform Specific Series
Hello World Series
8086 Downloads
8086 Cheatsheet
Sources.7z
DevTools kit
8086 Platforms
Wonderswan
MsDos

ARM Content
Learn ARM Assembly
Platform Specific Series
ARM Downloads
ARM Cheatsheet
Sources.7z
DevTools kit
ARM Platforms
Gameboy Advance
Nintendo DS
Risc Os

Risc-V Content
Learn Risc-V Assembly
Risc-V Downloads
Risc-V Cheatsheet
Sources.7z
DevTools kit

PDP-11 Content
Learn PDP-11 Assembly
PDP-11 Downloads
PDP-11 Cheatsheet
Sources.7z
DevTools kit

TMS9900 Content
Learn TMS9900 Assembly
TMS9900 Downloads
TMS9900 Cheatsheet
Sources.7z
DevTools kit
TMS9900 Platforms
Ti 99

6809 Content
Learn 6809 Assembly
6809 Downloads
6809/6309 Cheatsheet
Sources.7z
DevTools kit
6809 Platforms
Dragon 32/Tandy Coco
Fujitsu FM7
TRS-80 Coco 3
Vectrex

65816 Content
Learn 65816 Assembly
65816 Downloads
65816 Cheatsheet
Sources.7z
DevTools kit
65816 Platforms
SNES

eZ80 Content
Learn eZ80 Assembly
eZ80 Downloads
eZ80 Cheatsheet
Sources.7z
DevTools kit
eZ80 Platforms
SNES

Work in Progress
ChibiAndroids

Misc bits
Ruby programming









Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book

Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!

























































































グルテンフリー Gluten-free








Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book

Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!
























































































































































グルテンフリー Gluten-free