Tutorial 2

Conditional statements

This tutorial builds on tutorial 1 by adding a conditional statement. Conditional statements are extremely powerful in controlling program execution, and there are many different ways to use them.

Some conditions are: beq (branch if equal) bne (not equal) bgi (if higher) blo (if lower) bge (greater or equal) ble (lesser or equal). Conditional branches will often be preceded by a test (tst, btst) or compare (cmp, cmpi, cmpm, etc.) instruction. You will also need a target location for your branch.

What is our goal?

Add a conditional statement to our previous program.

Let's start!

Open up that source.asm and go to that loop with the add instruction.

Logo

We will be adding a cmpi.w (compare 16 bit word) instruction, and a beq (branch if equal) instruction. It's worth mentioning that you can't return from a conditional branch using an "RTS" instruction, as conditional branches don't push the program counter on to the stack.

Logo

The code will now increment D1 each time D0 hits $100 (256 in decimal). You can verify this by running in Kmod Gens and viewing the 68k's registers. D0 will be counting up very quickly, and d1 will be counting up more slowly.

Go back to tutorials