; Some Math Functions for Gabasic
; --- SGN
; EAX - Value
; RETS: EAX - s

SGN:
  CMP EAX, 0
  JG NEAR positive
  JL NEAR negative
    mov eax, 0
  RET
positive:
  mov eax, 1
RET
negative:
  mov eax, -1
RET

; --- ABS
; EAX, Value

ABS:
  CMP EAX, 0
  JNLE skipabs
    NEG EAX
skipabs:
  RET

; --- Random
; EAX, Range
; I just took this routine from BASM
RND:
Jmp    _RndB

_RndB:
  Mov  [Rand4], ax
  Xor  AX, AX
  Int  1AH
  Add  AX, DX
  Mov  AX, [Rand1]
  Mov  BX, [Rand2]
  Mov  CX, [Rand3]
  Add  DL, AH
  Add  DH, CL
  Add  AX, BX
  Add  AX, CX
  Add  AX, DX
  Mov  [Rand1], AX
  Add  AX, BX
  Add  AX, CX
  Add  AX, DX
  Mov  [Rand2], AX
  Shl  AX, 1
  Inc  BX
  Add  DX, AX
  Add  AX, BX
  Add  AX, CX
  Add  AX, DX
  Mov  [Rand3], AX
  Mov  AH, DL
  Cmp  AX, 0
  Jge  R1
  Not  AX
R1:
  Cmp  AX, [Rand4]
  Jle  R2
  Sub  AX, [Rand4]
  Jmp  R1
R2:
RET

