[comp.arch] Self-modifying codeREAD/NEW/FOLLOWUP

don@gp.govt.nz (Don Stokes, GPO) (10/15/89)

In article <15000@bloom-beacon.MIT.EDU>, tada@athena.mit.edu (Michael J Zehr) writes:
> I found an example of self-modifying code back in the middle ages (c.
> 1981) in the operating system on a Z-80 chip.  As i recall, the Z-80 had
> bit set, clear, and test op-codes which needed an assembly-time bit
> number rather than using a register to determine which bit to
> set/clear/test.  it took less space to determine the right bit and load
> the opcode (i think a shift would give you the opcode for a bit, or
> maybe shift and add an offset) into the right place in memory and then
> execute it.  the other option was to have a 8 branch statements.
> 

I once saw a neat technique to get a bit mask from a bit number on a 6502 
(eg plug in 4, get 16 out) - the routine went:

                sta jump+1      ; Modify branch instruction offset
                lda #$80        ; Bit 7 set
        jump:   bne jump        ; This gets modified
                lsr A           ; Number of shifts depends on branch
                lsr A
                lsr A
                lsr A
                lsr A
                lsr A
                lsr A
                rts

Notes: BNE is byte-relative, LSR A is a single byte.  The nice thing 
about this is that it does not require any registers other than the 
accumulator (now *there's* an outdated concept - do any really modern
processors have accumulators?), unlike the more conventional method of 
using a lookup table, which requires the use of an index (X or Y) 
register on a 6502.

Don Stokes ZL2TNM   /  /                                vuwcomp!windy!gpwd!don
Systems Programmer /GP/ Government Printing Office      PSI%0530147000028::DON
__________________/  /__Wellington, New Zealand_________don@gp.govt.nz________
It's not a bug, its an undocumented feature.