SELF MODIFYING PROGRAM FOR MCS-85 - [80C85]
The task is very simple. There are 8 LEDs in an array. Assume one end of the array is LSB and other end is MSB. We need to lit one LED at a time from LSB to MSB. After reaching MSB, we have to reverse or change the direction.
Before we start, let us understand Self-modifying code first.
This task can be achieved by many ways. Easiest way is having a look-up table with desired pattern. Other popular options are load the LSB then left shift till reaching MSB. After reaching MSB change the shifting direction. This is also equal to Multiply by 2 and Divide by 2.
This can be detected and controlled by monitoring the MSB bit by numerical number 0X80H hex value (1000 0000) or binary value. Or use the Carry bit to make the jump decision.
SOURCE CODE:
;#############################################################
; MCS-85 SELF MODIFYING PROGRAM FOR RUNNING 8 LEDs at PORT 04H
;#############################################################
ORG 2000H
XRA A
STC
RAL
OUT 04
CALL DELAY
CALL DELAY
CC XOR8
JMP 2002
XOR8: LDA 2002
XRI A,08
STA 2002
XRA A
STC
RET
; DELAY is part of MONITOR program and not Listed Here
After assembly of the code starting from 2000H will look like this

After loading the code in to the RAM starting from 2000H we are sure, at location 2002H will have 0x17 which is the opcode for RAL. We can also have RAR as the starting value. It works.
But once we give command to execute starting from 2000H, the content of 2002H will change to 0x1F if there is a CARRY FLAG and again changes back to 0x17 if the CARRY occurs again. This process repeats forever.
So at any point of time, no one knows the program instruction located at 2002H. It may be RAR or RAL instruction opcode.
MCS-85 ROTATE INSTRUCTIONS:

There are 4 ROTATE instructions available for MCS-85 family.
- RLC(0x07)
- RRC(0x0F)
- RAL(0x17)
- RAR(0x1F)
We are using RAL and RAR instructions in our program

- RAL = 0001 0111 = 0x17
- RAR = 0001 1111 = 0x1F

So if the D3 bit is 0 then Left rotate and if the D3 bit is 1 then Right rotate operation performed.


SO RAL XOR 00001000 will generate the RAR instruction opcode at 2002H location
similarly RAR XOR 00001000 will generate the RAL instruction opcode at 2002H location
Same logic is applicable and true for RLC and RRC instruction of MCS-85 family. There are many instructions with similar properties which produces different actions by flipping one or two bits.
This demonstrates the MCS-85 family program code changes by itself depends on the CARRY flag status.
Demonstration with 80C85 kit running 8 LEDs at PORT 04H [4:55]
ONLY PROGRAMMING AND RESULTS CAN BE SEEN HERE [51 seconds]
mit41301
Michael Wessel
land-boards.com
HummusPrince
aaron
I suggest adding a brief explanation of all the instructions used. You don't need to go into depth, just tell us what "XRA", STA", "CC", etc. mean.