diff --git a/src/core.asm b/src/core.asm index bcc78ee..6478ecb 100644 --- a/src/core.asm +++ b/src/core.asm @@ -10,6 +10,8 @@ section .text global minu global max global maxu + global clamp + global clampu ;----- exit(exit_code) -----; ; Exits the program with given exit code @@ -139,3 +141,39 @@ maxu: cmp rdi, rsi cmova rax, rdi ret + +;----- clamp(input, min, max) -----; +; Force input (signed integer) to be atleast min and maximum max +; Return value: input if in range of min,max. Otherwise min if below min or max if below max +; Used registers: +; rax* (ret) input, min or max +; rdi (arg) input +; rsi (arg) min +; rdx (arg) max +clamp: + mov rax, rdi + cmp rax, rsi + cmovl rax, rsi + jl .quit + cmp rax, rdx + cmovg rax, rdx + .quit: + ret + +;----- clampu(input, min, max) -----; +; Force input (unsigned integer) to be atleast min and maximum max +; Return value: input if in range of min,max. Otherwise min if below min or max if below max +; Used registers: +; rax* (ret) input, min or max +; rdi (arg) input +; rsi (arg) min +; rdx (arg) max +clampu: + mov rax, rdi + cmp rax, rsi + cmovb rax, rsi + jb .quit + cmp rax, rdx + cmova rax, rdx + .quit: + ret