Skip to content

Instantly share code, notes, and snippets.

Last active Mar 5, 2020
What would you like to do?
Z80 routine to clamp value in HL to -0x100 to +0x100 range
; In: HL = value to be clamped to -0x100 .. +0x100
; Out: HL = clamped value
; Uses: A + flags
; (this is not mathematically correct, values 0x7F?? clamp to -0x100, for performance reasons)
ld a,h
inc a
sra a ; will be 0 for -256..+255 (+256 will get "clamped")
ret z
ld hl,0x100
ret p ; if A was positive, clamp to 0x100
ld h,high -0x100 ; -0x100 = 0xFF00, L is already zero
; "ld h,0xFF" if your assembler doesn't have "high" (sjasmplus has)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment