Task

Select certain elements from an Array into a new Array in a generic way.



To demonstrate, select all even numbers from an Array.

As an option, give a second solution which filters destructively,
by modifying the original Array rather than creating a new Array.




TestArray_Metadata:
byte 4,4   ;4 rows, 4 columns.
TestArray:
byte 0,1,2,3
byte 4,5,6,7
byte 8,9,10,11
byte 12,13,14,15
 
OutputArray_Metadata:
byte 2,4
OutputArray:
ds 8,0              ;16 bytes each equaling zero
 
FilterEvenValues:
ld hl,TestArray_Metadata
ld a,(hl)
inc hl
ld b,(hl)
inc hl               ;LD HL,TestArray
call mul_A_times_B   ;unimplemented multiplication routine, multiplies A by B and returns product in A.
ld b,a               ;we'll use this product as a loop counter.
 
ld de,OutputArray
 
loop_filterEvenValues:
ld a,(hl)
ld c,a
rrc c        ;destructively test if odd or even. (That's why it was copied into C first.)
jr c,skipThis
ld (de),a
inc de
 
skipThis:
inc hl
djnz loop_filterEvenValues
 
ret   ;return to basic