Fills up a complete byte-array in memory with a given 8-bit value (as std memset)

C Definition

void cpct_memset (void* array, u8 value, u16 size);

Input Parameters (5 Bytes)

(2B DE) arrayPointer to the first byte of the array to be filled up (starting point in memory)
(1B A ) value8-bit value to be set
(2B BC) sizeNumber of bytes to be set (>= 2)

Assembly call (Input parameters on registers)

call cpct_memset_asm

Parameter Restrictions

  • array could theoretically be any 16-bit memory location.  However, take into account that this function does no check at all, and you could mistakenly overwrite important parts of your program, the screen, the firmware...  Use it with care.
  • size must be greater than 1.  It represents the size of the array, or the number of total bytes that will be set to the value.  As this function starts moving 1 first byte and then cloning it, the minimum amount size should be 2.  Beware!  Sizes 0 and 1 can cause this to overwrite the entire memory.
  • value could be any 8-bit value, without restrictions.


Sets all the bytes of an array in memory to the same given value.  This is the same operation as std memset, from the standard C library does.  However, this function is much faster than std C memset, so it is recommended for your productions.  The technique this function uses to be so faster is as follows:

1Sets up the first byte of the array to the value
2Makes HL point to the first byte and DE to the second
3BC has the total bytes to copy minus 1 (the first already set)
4LDIR copies first byte into second, then second into third...

This function works for array sizes from 2 to 65535 (it does not work for 0 or 1).  However, it is recommended that you use it for values greater than 2.  Depending on your code, using memset for values in the range [2-8] could underperform simple variable assignments.

Destroyed Register values


Required memory

C-binding14 bytes
ASM-binding8 bytes

Time Measures

  Case     | microSecs (us) | CPU Cycles |
  Any      |   28 + 6*S     | 112 + 24*S |
Asm saving |     -18        |    -72     |

S = size (Number of total bytes to set)

Fills up a complete byte-array in memory with a given 8-bit value (as std memset)
unsigned char (u8 = unsigned 8-bits, 1 byte )
unsigned int (u16 = unsigned 16-bits, 2 bytes)