                        ;####################################################
                        	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 65,535
   2025                 LOOP_16:
   2025   2b                	DCX H           ; [6 T-states] Decrement HL pair
   2026   7d                	MOV A, L        ; [4 T-states] Move Low byte to A
   2027   b4                	ORA H           ; [4 T-states] OR with High byte (Sets Zero flag)
   2028   c2 25 20          	JNZ LOOP_16     ; [10/7 T-states] Jump if result not zero
   202b   c9                	RET             ; [10 T-states]
   202c                 	END 		; Added for A85
Self modifying LED program for MCS-85

2006  AGAIN         2022  DELAY         2025  LOOP_16       2000  START     
2017  XOR8          
