Changes

Programming:Filling memory with a byte

1,628 bytes added, 23:15, 17 March 2018
/* Using the stack */
==Using LDIR==
 
From the '''The Unofficial Amstrad WWW Resource'''
;;
;; etc....
</pre>
 
==Using the stack==
 
This method write bytes at a considerably faster rate by exploiting the speed of the stack pointer (SP) moving through RAM and its 2-byte steps. It is especially good for very rapidly clearing the screen.--[[User:Db6128|Db6128]] ([[User talk:Db6128|talk]]) 06:29, 13 December 2012 (EET)
 
<pre>
; Save the stack pointer
ld (SAVE_SP),sp ; Only remove if you're sure you don't need the stack
ld sp,FINAL_ADDRESS+1 ; e.g. to fill from &C000 to &FFFF (&4000 bytes), set SP to &FFFF+1 = &0000
 
; Define the region of RAM to be filled
ld hl,YOUR_BYTE_1*&100 + YOUR_BYTE_2 ; Use HL or whichever 16-bit register you prefer
ld 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
ld b,e ; This method takes advantage of the fact that DJNZ leaves B as 0, which subsequent DJNZs see as 256
dec de ; B = (length/2) MOD 256, so 0 = a 512-byte block
inc d ; D = the number of 512-byte blocks to write, or just = 1 if the length is <512
; 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 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
dec d
jr nz,PUSHLOOP
 
; Restore the stack pointer
SAVE_SP equ $+1
ld SP,&9999 ; &9999 will be replaced by the actual previous value
</pre>
[[Category:Programming]]
14
edits