You are on page 1of 2

;

;
;
;
;
;
;
;
;
;
;

This is the SKELETON of an 8051 program that should solve any maze.
In its present form it employs the algorithm:
1) If you are on the cheese, then stay there.
2) Otherwise, if you can move forward, then move forward.
3) Otherwise, if you can move left, then move left.
4) Otherwise, if you can move right, then move right.
5) Otherwise, if you can move backwards, then move backwards.
6) Otherwise, complain about being placed in a defective maze.
By running the program you will be able to observe that this
is not the best algorithm. Your job is to find and implement
a better algorithm.

;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;

Whereas my MazeSolver.txt program employs the ANL and CJNE

instructions, this MazeSolver2.txt program employs the JB
instruction. The operand specified with the JB instruction
must either be a bit located in the "bit addressable data memory"
(which is that section of data memory from address 0x20 thru 0x2F)
or else a bit within one of the bit addressable special function
registers (these are the special function registers whose hex
address ends in 0, such as the P0 register with a hex address of
0x80). There are 128 bits within the 16 byte section of
bit addressable data memory and these are given the bit
addresses from 0 to 0x7F = 127. There are another 128
bit addresses ranging from 128 = 0x80 to 0xFF = 255
which correspond to bits in the bit-addressable SFR registers.
The easiest way to refer to a bit in the bit-addressable
section of data memory is to use notation such as:
The easiest way to refer to a bit in a bit-addressable special
function register is to use notation such as:

;
;
;
;
;
;
;

There are potentially 4 directions we can move: left, right,

forward, or backward.
To move left we write 0x00 to P0.
"
forward "
0x01 "
"
right
"
0x02 "
"
backward "
0x03 "
Writing any other value to P0 causes no motion.

; In order to avoid having these 4 numbers "hard coded" in

; our 8051 program, we employ the EQU assembler directive
; to assign more convenient symbols for these values:
MoveLeft
MoveForward
MoveRight
MoveBackward
;
;
;
;
;
;
;

EQU
EQU
EQU
EQU

0x00
0x01
0x02
0x03

Any of these directions might be blocked by a wall.

To learn which walls currently surround us we read from P1.
If one of the following bits in P1 is a 1 then there is a
wall in that direction:
MSBit 7
6
5
4
3
2
1
0 LSBit
cheese not not not rear right front left
used used used wall wall wall wall

; In order to avoid having the bit locations "hard coded" in

; our 8051 program, we employ the EQU assembler directive
; to assign more convenient symbols for these values:

LeftWall
FrontWall
RightWall
RearWall
Cheese

EQU
EQU
EQU
EQU
EQU

0
1
2
3
7

Loop:
JNB P1.Cheese, Move ; check for the presence of cheese
Eat:

SJMP Eat

; don't move, we're on the cheese

Move:
JNB P1.FrontWall, NoFrontWall

; If we get to here we are trapped by 4 walls and hence the

; maze was defective!
Trapped: SJMP Trapped
NoRearWall:
MOV P0,#MoveBackward
LJMP Loop
NoRightWall:
MOV P0,#MoveRight
LJMP Loop
NoFrontWall:
MOV P0,#MoveForward
LJMP Loop
NoLeftWall:
MOV P0,#MoveLeft
LJMP Loop