ssm.js

door Mathijs Henquet

ldc 5 ; @a
ldc 12 ; @b
bsr sumsquare
ajs -2 ; verwijder parameters
lds 5 ; resultaat op de stack
bra next

sumsquare:
	link 1 ; 1 locale variabele
	ldl -3 ; a
	ldl -3 ; a
	mul ; a*a
	ldl -2 ; b
	ldl -2 ; b
	mul ; b*b
	add ; a*a + b*b
	stl 1 ; x
	unlink 1
	ret

next:

ldc 1 ; @a
ldc 2 ; @b
ldc 3 ; @c
ldc 4 ; @d

bsr complex_multiply
ldr RR
ldr 5


ldc -1 ; @a
ldc 1 ; @b
bsr complex_square

ldr RR
ldr 5

ldc 3 ; @a
ldc 4 ; @b
bsr pyth
ldr RR ; @c
annote SP -2 0 blue "pythagoras a^2 + b^2 = c^2"

ldc 11; @root
bsr sq
ldr 4 ; @square
bsr sqrt
ldr 4
bra exit

complex_square:
  link
  ldl -3
  ldl -3
  mul
  ldl -2
  ldl -2
  mul
  sub

  str RR

  ldl -3
  ldl -2
  mul
  ldc 2
  mul

  str 5

  unlink
  ret

complex_multiply:
  link
  ldl -5
  ldl -3
  mul
  ldl -4
  ldl -2
  mul
  sub

  str RR

  ldl -5
  ldl -2
  mul
  ldl -4
  ldl -3
  mul
  add

  str 5

  unlink
  ret

pyth:
  lds -2 ; @a
  bsr sq
  ajs -1
  ldr 4

  lds -2 ; @b
  bsr sq
  ajs -1
  ldr 4

  add
  bsr sqrt
  ajs -1
  ret

sqrt:
  link 0
  lds -2 ; @root take the number to sqrt from the stack
  ldc 16 ; @i we will take a maximum of 16 itterations of newtons method
  ldl 1 ; @p

sqrt_loop:
  lds 0 ; take current trice
  lds 0
  mul   ; multiply together
  ldl 1 ; now take the sqrt we want
  add   ; add this and the square together
  lds -1
  ldc 2
  mul
  div

  lds -1 ; @p_old
  lds -1 ; @p_new
  eq
  brt sqrt_exit
  sts -1 ; @p

  ldl 2 ; Load iter count
  ldc 1 ; 1
  sub   ; decrement iter count
  stl 2 ; @i store iter count
  ajs 1 ;
  ldc 0
  eq
  brf sqrt_loop ; jump back if

sqrt_exit:
  str 4 ; @
  unlink
  ret

sq:
  lds -1
  lds 0
  mul
  str 4
  ret

ldc 12 ; @n
bsr factorial
ldr RR
halt

factorial:
  lds -1
  lds 0

factorial_loop:
  lds -1
  ldc 1
  sub

  lds 0
  brt factorial_exit

  lds 0
  sts -3 ; @n

  mul ; @f_n

  bra factorial_loop

factorial_exit:
  ajs -1
  str RR
  ajs -1
  ret

exit: halt

	
adress hex int float comment char (UCS-2) binary
registry hex int float char (UCS-2) binary