Close
0%
0%

MCS-85 - Self modifying program

Task is very simple. Just lit a LED and shift it left and right endlessly in a array of 8 LEDs. But NOT by the usual way or Logic you think!

Similar projects worth following
1.2k views
0 followers
This small and simple project explains how to write self modifying code with a 80C85 microprocessor of MCS-85 family. One of the pre-requisite of the program is, the program will RUN only from RAM. We can not RUN the program from ROM, EPROM since the program will dynamically modifty its own CODE while executing. The Demonstration shows a array of 8 LEDs out of which one LED moves Left and Right.

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 »

ral2rar.lst

A85 assembler listing file

lst - 2.52 kB - 10/28/2026 at 15:43

Download

ral2rar.hex

Output Intel HEX file

hex - 127.00 bytes - 10/28/2026 at 15:43

Download

rar2rar.sym

Assembler listing Symbol file by A85

sym - 125.00 bytes - 10/28/2026 at 15:43

Download

RAL2RAR.ASM

8085 assembly source code for A85. This code contains the delay loop and the port address changed from 04H to 61H

asm - 1.45 kB - 10/28/2026 at 15:43

Download

M80C85.pdf

OKI 80C85 CMOS version datasheet with instructions

Adobe Portable Document Format - 170.84 kB - 12/28/2025 at 08:47

Preview

View all 6 files

  • Works with DELAY loop

    mit41301 • 10/28/2026 at 15:56 • 0 comments

    Currently added the delay loop and tested with a different 8085 board with output port located at 061H. The program works well. 

    The instruction at the location 2006H changes itself from RAL->RAR->RAL and the direction changes after every time a CARRY flag is set to 1.

    It changes the direction correctly when counts down and reaches 0. But when goes up, after MSB it goes to 0 then changes the direction.

    There is still a small bug. But the concept works!

    Added the assembler source code. Assembled using A85

    a85 ral2rar.asm -l ral2rar.lst -o ral2rar.hex -s rar2rar.sym

View project log

Enjoy this project?

Share

Discussions

Gravis wrote 01/06/2026 at 06:00 • point

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.

  Are you sure? yes | no

Does this project spark your interest?

Become a member to follow this project and never miss any updates