Nightloader/i386/legacy-boot/src-asm/utility/math.asm

53 lines
1.2 KiB
NASM
Raw Normal View History

; Arguments:
; [Furthest from EBP]
; 1. U32 Stepping
; 0. U32 Affected Value
; [Nearest to EBP]
;
; Returns:
; - EAX: The next higher multiple of the stepping,
; as calculated from affected value.
ceil_to:
.prolog:
push esi
sub esp, 16
mov esi, esp
mov [esi + (16 - 4)], ecx
mov [esi + (16 - 8)], edx
.check_values:
mov eax, [ebp - 4]
cmp eax, 0
jne .affected_value_is_valid
xor eax, eax
jmp .epilog
.affected_value_is_valid:
mov ecx, [ebp - 8]
cmp ecx, 0
jne .calculate
xor eax, eax
jmp .epilog
.calculate:
; Calculate how much needs ot be added to the
; affected value for it to become a multiple of
; the stepping using the remainder part of the division.
div ecx
; EDX now contains how much the affected value
; 'goes over' the last multiple of the stepping.
; Now, it can be calculated how much has to be added
; to the affected value to make it the next multiple
; of the stepping.
mov ecx, [ebp - 8]
sub ecx, edx
mov eax, [ebp - 4]
add eax, ecx
.epilog:
mov edx, [esi + (16 - 8)]
mov ecx, [esi + (16 - 4)]
add esp, 16
pop esi
ret