Stepper Motor Simulation in Emu8086: A Worked Example

Emu8086 ships with a built-in Stepper Motor virtual device: a simple three-phase motor that turns through a precise angle each time it receives a pulse pattern. It is the standard next step after the traffic lights, because it adds direction, speed and a status read to the output-only pattern you already know.

How the device is controlled

Open the device from the emulator’s virtual devices menu before you run your program. The motor is controlled by sending a byte to I/O port 7. Only bits 0, 1 and 2 of the byte are used; they drive the motor’s three magnets, and bits 3 to 7 are unused. When a magnet is energised it is shown in red, and an arrow shows the direction of the last move. The motor turns by varying the rate at which pulse patterns are sent, so speed is controlled by the delay between outputs, exactly as with the traffic lights.

The emulator’s documentation gives the starting sequence for three clockwise half-steps, and it is the right place to begin:

MOV AL, 001b ; initialize.
OUT 7, AL
MOV AL, 011b ; half step 1.
OUT 7, AL
MOV AL, 010b ; half step 2.
OUT 7, AL
MOV AL, 110b ; half step 3.
OUT 7, AL

The same documentation points to the bundled example file stepper_motor.asm in the emulator’s examples folder. Open it alongside this post: it shows the sequence extended into continuous rotation, and it is the reference to check any pattern against.

Reading readiness: the top bit of port 7

The motor also talks back. Reading port 7 with IN returns a byte whose topmost bit is set when the motor is ready for the next pattern. A careful program checks that bit before sending the next step, rather than assuming a fixed delay was long enough:

wait_ready:
IN AL, 7
TEST AL, 10000000b   ; is the top bit set?
JZ wait_ready        ; no: keep waiting

TEST sets the flags without changing AL, so this polling loop is a clean use of the flag behaviour covered in the ADD and CMP post.

Worked example: rotate, then reverse

This example steps through the documented half-step patterns in a loop, then runs the same patterns in reverse order to turn the motor the other way. Reversing the sequence is how direction is controlled: there is no separate direction command, the order of the patterns is the direction.

ORG 100h

MOV CX, 20           ; twenty steps forward
forward:
MOV AL, 001b
OUT 7, AL
CALL delay
MOV AL, 011b
OUT 7, AL
CALL delay
MOV AL, 010b
OUT 7, AL
CALL delay
MOV AL, 110b
OUT 7, AL
CALL delay
LOOP forward

MOV CX, 20           ; twenty steps back, patterns reversed
backward:
MOV AL, 110b
OUT 7, AL
CALL delay
MOV AL, 010b
OUT 7, AL
CALL delay
MOV AL, 011b
OUT 7, AL
CALL delay
MOV AL, 001b
OUT 7, AL
CALL delay
LOOP backward
RET

delay PROC
PUSH CX
MOV CX, 0FFFFh
wait_loop:
LOOP wait_loop
POP CX
RET
delay ENDP

Two details matter here. First, the delay procedure saves and restores CX, because the outer loop also uses CX as its counter; without the PUSH and POP, the delay would destroy the step count. Second, speed is the delay length: a longer delay means slower, more visible steps, and a shorter delay means faster rotation, up to the point where the motor cannot keep up and steps are missed.

Common mistakes

  • Sending patterns too fast without a delay, so the motor appears not to move at all.
  • Changing only one bit at a time in the wrong order, which produces vibration instead of rotation. Follow the documented half-step order.
  • Letting a delay routine overwrite CX, SI or another register the caller is using. Save what you use.

For the wider picture, compare this output-only-plus-status device with the traffic light simulation, and read from simulation to real hardware for what changes when a real motor, driver circuit and power supply replace the simulated window.

Further reading

New to assembly? Start with Your First 8086 Program: Hello World, then return here.

Similar Posts