Changes

Jump to: navigation, search

Programming:Filling memory with a byte

61 bytes removed, 23:15, 17 March 2018
/* Using the stack */
; Define the region of RAM to be filled
ld hhl,YOUR_BYTE YOUR_BYTE_1*&100 + YOUR_BYTE_2 ; Use HL or whichever 16-bit register you preferld l,YOUR_BYTE ; For 0, you can save 1 byte of memory by instead using XOR A:LD H,A:LD L,Ald de,LENGTH_TO_FILL/2 ; divided by 2 because DE is 2 bytes wide and PUSH pushes both
; Set up a fast 16-bit loop counter
dec de ld b,e ; This method takes advantage of the fact that DJNZ leaves B as 0, which subsequent DJNZs see as 256ld b,e dec de ; B = (length/2) MOD 256, so 0 = a 512-byte blockinc b d ; D = the number of 512-byte blocks to write, or just = 1 if the length is <512inc d ; Of course, if you know the length ahead of run-time, you can set B and D directly in your ASM
; Fill the memory
PUSHLOOP:
push hl ; Writes DE HL to (SP-2) and DECs SP by 2.
; For even more speed, use multiple PUSH HLs in a row (I like 8, = &10 bytes) and adjust counters to match
djnz PUSHLOOP
14
edits