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:
;#################################################### TITLE "Self modifying LED program for MCS-85" ;#################################################### ; depends on CARRY flag, the program itself changes ; if the content is RAL, it changes itself to RAR ; if the content is RAR, it changes itself to RAL ORG 2000H ; RAM starts HERE START LXI H, 4000h ; load HL pair with RAM end location SPHL ; init Stack Pointer XRA A ; clear ACCumulator STC ; set CARRY bit AGAIN RAL ; rotate RAL or RAR OUT 61H ; send it to output port 61H PUSH PSW ; store the ACC content in stack CALL DELAY ; call time delay CALL DELAY ; call more time delay POP PSW ; restore the ACC content back CC XOR8 ; If CARRY bit set change direction JMP AGAIN ; do the shift again XOR8 LDA AGAIN ; load the current rotate instruction XRI 08H ; this will change the direction STA AGAIN ; store the modified instruction XRA A ; clear the ACC STC ; set CARRY flag RET ; return ; --- 16-bit Delay Routine --- ; Input: HL pair holds the 16-bit count (up to FFFFH) DELAY: LXI H, 0FFFFH ; [10 T-states] Load HL with 65,535 LOOP_16: DCX H ; [6 T-states] Decrement HL pair MOV A, L ; [4 T-states] Move Low byte to A ORA H ; [4 T-states] OR with High byte (Sets Zero flag) JNZ LOOP_16 ; [10/7 T-states] Jump if result not zero RET ; [10 T-states] END ; Added for A85
After assembly of the code starting from 2000H will look like this
;####################################################
TITLE "Self modifying LED program for MCS-85"
;####################################################
; depends on CARRY flag, the program itself changes
; if the content is RAL, it changes itself to RAR
; if the content is RAR, it changes itself to RAL
2000 ORG 2000H ; RAM starts HERE
2000 21 00 40 START LXI H, 4000h ; load HL pair with RAM end location
2003 f9 SPHL ; init Stack Pointer
2004 af XRA A ; clear ACCumulator
2005 37 STC ; set CARRY bit
2006 17 AGAIN RAL ; rotate RAL or RAR
2007 d3 61 OUT 61H ; send it to output port 61H
2009 f5 PUSH PSW ; store the ACC content in stack
200a cd 22 20 CALL DELAY ; call time delay
200d cd 22 20 CALL DELAY ; call more time delay
2010 f1 POP PSW ; restore the ACC content back
2011 dc 17 20 CC XOR8 ; If CARRY bit set change direction
2014 c3 06 20 JMP AGAIN ; do the shift again
2017 3a 06 20 XOR8 LDA AGAIN ; load the current rotate instruction
201a ee 08 XRI 08H ; this will change the direction
201c 32 06 20 STA AGAIN ; store the modified instruction
201f af XRA A ; clear the ACC
2020 37 STC ; set CARRY flag
2021 c9 RET ; return
; --- 16-bit Delay Routine ---
; Input: HL pair holds the 16-bit count (up to FFFFH)
2022 DELAY:
2022 21 ff ff LXI H, 0FFFFH ; [10 T-states] Load HL with...
Read more »
mit41301
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.