Robot Arm Simulation in Emu8086: A Worked Example

Emu8086 ships with a built-in Robot virtual device, and it is the device students usually mean when they search for a robot simulation in the emulator. A note on the name first: the built-in device is called Robot, and it is a small mobile robot moving around a map, turning, examining objects and switching lamps on and off. It is not a jointed arm with shoulder and elbow joints. This post covers the device the emulator actually provides, with its real commands, so the code here runs as written.

How the device is controlled

Open the device from the emulator’s virtual devices menu before you run your program. You can edit its map from the device’s toolbox: place the robot, walls and lamps, and the map is saved automatically when the device closes. The robot uses three ports:

Port Role
9 Command register: write a command byte here.
10 Data register: read the result of an examine command here.
11 Status register: read the robot’s state here.

The command set

Value Command
0 Do nothing.
1 Move forward one step.
2 Turn left.
3 Turn right.
4 Examine: sense the object in front and report it in the data register.
5 Switch on a lamp.
6 Switch off a lamp.

The examine results reported in the data register are 255 for a wall, 0 for nothing, 7 for a switched-on lamp, and 8 for a switched-off lamp. The status register reports three things in its low bits: bit 0 is set when new data is waiting in the data register, bit 1 is set while the robot is busy, and bit 2 is set when the last command ended in an error, for example trying to move into a wall.

The rule you must not skip: wait until it is ready

The robot is a mechanical creature in the simulation and takes time to finish each task. Always check bit 1 of the status register before sending the next command, or the robot rejects the command and reports busy. The standard wait loop polls port 11:

wait_ready PROC
wait_loop:
IN AL, 11
TEST AL, 00000010b   ; bit 1: 1 means busy
JNZ wait_loop        ; still busy: keep waiting
RET
wait_ready ENDP

Worked example: move, turn, examine

This example moves forward, turns right, moves forward again, turns left, and then examines what is in front of the robot. After the examine command, it reads the data register; a program that went further would compare that value with 255, 0, 7 or 8 and branch accordingly, using the comparison and jump techniques from the ADD and CMP and JMP and LOOP posts.

ORG 100h

CALL wait_ready
MOV AL, 1            ; move forward
OUT 9, AL
CALL wait_ready
MOV AL, 3            ; turn right
OUT 9, AL
CALL wait_ready
MOV AL, 1            ; move forward
OUT 9, AL
CALL wait_ready
MOV AL, 2            ; turn left
OUT 9, AL
CALL wait_ready
MOV AL, 4            ; examine what is in front
OUT 9, AL
CALL wait_ready

; The examine result is now in the data register.
IN AL, 10
; AL = 255 (wall), 0 (nothing), 7 (lamp on) or 8 (lamp off).
RET

wait_ready PROC
wait_loop:
IN AL, 11
TEST AL, 00000010b
JNZ wait_loop
RET
wait_ready ENDP

If a command is rejected, check the two usual causes: the previous command had not finished and the wait loop was skipped, or the map has no robot placed on it, in which case the device places one at a random position and the robot’s starting direction is not the one you assumed. Place the robot deliberately in the map editor before running, and the example behaves predictably.

For the simpler devices that lead up to this one, see the traffic light simulation and the stepper motor simulation. For what changes when the robot stops being virtual, read from simulation to real hardware.

Further reading

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

Similar Posts