-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmath.asm
73 lines (70 loc) · 2.12 KB
/
math.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;===================================================================================================
; Math Functions
;
; Written By: Oded Cnaan ([email protected])
; Site: http://odedc.net
; Licence: GPLv3 (see LICENSE file)
; Package: UtilLib
;
; Description:
; Several math related procedures
;
; For Linear congruential generator see
; https://en.wikipedia.org/wiki/Linear_congruential_generator#c.E2.89.A00
;
;===================================================================================================
LOCALS @@
DATASEG
_SeedVal dw (0)
CODESEG
;----------------------------------------------------------
; Creates a seed for calculating rand numbers
;----------------------------------------------------------
PROC RandomSeed
push ax
push dx
mov ah, 00h ; interrupt to get system timer in CX:DX
int 1AH
mov [_SeedVal], dx
pop dx
pop ax
ret
ENDP RandomSeed
;----------------------------------------------------------
; Gets a WORD random number
; Return result in ax
;----------------------------------------------------------
PROC RandomWord
push dx
mov ax, 25173 ; LCG Multiplier
mul [WORD PTR _SeedVal] ; DX:AX = LCG multiplier * seed
add ax, 13849 ; Add LCG increment value
; Modulo 65536, AX = (multiplier*seed+increment) mod 65536
mov [_SeedVal], ax ; Update seed = return value
pop dx
ret
ENDP RandomWord
;----------------------------------------------------------
; Gets a BYTE random number
; Return result in al
;----------------------------------------------------------
PROC RandomByte
call RandomWord
and ax,00ffh
ret
ENDP RandomByte
;=========================================================================
; Other math functions
;=========================================================================
;----------------------------------------------------------
; Calculates the abs value of a register
;
; gr_absolute cx
;----------------------------------------------------------
MACRO gr_absolute a
local absolute_l1
cmp a, 0
jge absolute_l1
neg a
absolute_l1:
ENDM