#include <stdlib.h>

int16_t min(int16_t a, int16_t b) __naked
{
    (void)a; (void)b;
    __asm
        ld    a, h
        cp    d
        jr    c, min_less
        jr    nz, min_greater
        ld    a, l
        cp    e
        jr    c, min_less
    min_greater:
        ret         /* DE уже содержит b */
    min_less:
        ex    de, hl
        ret
    __endasm;
}

int16_t max(int16_t a, int16_t b) __naked
{
    (void)a; (void)b;
    __asm
        ld    a, h
        cp    d
        jr    c, max_less
        jr    nz, max_greater
        ld    a, l
        cp    e
        jr    c, max_less
        ret   /* a >= b, return a in HL */
    max_less:
        /* a < b, return b in DE */
        ret   /* DE already contains b */
    max_greater:
        /* a > b, return a in HL */
        ex    de, hl
        ret
    __endasm;
}
