Sie sind auf Seite 1von 2

; This 8051 program solves any maze.

; It employs the algorithm:


; 1) If you are on the cheese, then stay there.
; 2) Otherwise, if you can move left, then move left.
; 3) Otherwise, if you can move forward, then move forward.
; 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.
;
;
;
;
;
;
;

This algorithm is what you would do if you stuck out your


left hand and walked the maze by always keeping your left
hand pressed against the left wall. As long as the walls
are no wider than your outstretched hands, this simple algorithm
guarantees that you will eventually explore the entire maze
and hence find the cheese (obviously, this algorithm will
not explore the center of a large cavernous area).

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

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:
JB 0x20.3, Ahead
The easiest way to refer to a bit in a bit-addressable special
function register is to use notation such as:
JB P0.3, Ahead

;
;
;
;
;
;
;

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


forwards, and backwards.
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.LeftWall, NoLeftWall

; check for presence of left wall

JNB P1.FrontWall, NoFrontWall ; check for presence of front wall


JNB P1.RightWall, NoRightWall ; check for presence of right wall
JNB P1.RearWall, NoRearWall

; check for presence of rear wall

; 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

Das könnte Ihnen auch gefallen