Data update
This commit is contained in:
parent
796d366b97
commit
35bcdeebf8
504 changed files with 7045 additions and 610 deletions
|
|
@ -2,59 +2,63 @@
|
|||
; useful equates
|
||||
;-------------------------------------------------------
|
||||
bdos equ 5 ; BDOS entry
|
||||
cr equ 13 ; ASCII carriage return
|
||||
lf equ 10 ; ASCII line feed
|
||||
space equ 32 ; ASCII space char
|
||||
cr equ 13 ; ASCII carriage return
|
||||
lf equ 10 ; ASCII line feed
|
||||
space equ 32 ; ASCII space char
|
||||
conout equ 2 ; BDOS console output function
|
||||
putstr equ 9 ; BDOS print string function
|
||||
putstr equ 9 ; BDOS print string function
|
||||
nterms equ 20 ; number of terms (max=24) to display
|
||||
;-------------------------------------------------------
|
||||
; program code begins here
|
||||
; main code begins here
|
||||
;-------------------------------------------------------
|
||||
org 100h ; load point under CP/M
|
||||
lxi h,0 ; save command processor's stack
|
||||
dad sp
|
||||
org 100h
|
||||
lxi h,0 ; save CP/M's stack
|
||||
dad sp
|
||||
shld oldstk
|
||||
lxi sp,stack ; set our own stack
|
||||
lxi d,signon ; print signon message
|
||||
mvi c,putstr
|
||||
lxi sp,stack ; set our own stack
|
||||
lxi d,signon ; print signon message
|
||||
mvi c,putstr
|
||||
call bdos
|
||||
mvi a,1 ; test by displaying first 20
|
||||
mloop: push a ; save our count (putdec will trash)
|
||||
mvi a,0 ; start with Fib(0)
|
||||
mloop: push a ; save our count
|
||||
call fib
|
||||
call putdec
|
||||
mvi a,space ; separate the numbers
|
||||
mvi a,space ; separate the numbers
|
||||
call putchr
|
||||
pop a ; get our count back
|
||||
inr a ; increase it by one
|
||||
cpi 20+1 ; have we reached our limit?
|
||||
jnz mloop ; not yet; do another
|
||||
lhld oldstk ; we're done - get CP/M's stack back
|
||||
pop a ; get our count back
|
||||
inr a ; increment it
|
||||
cpi nterms+1 ; have we reached our limit?
|
||||
jnz mloop ; no, keep going
|
||||
lhld oldstk ; all done; get CP/M's stack back
|
||||
sphl ; restore it
|
||||
ret ; back to command processor
|
||||
ret ; back to command processor
|
||||
;-------------------------------------------------------
|
||||
; calculate nth Fibonacci number
|
||||
; calculate nth Fibonacci number (max n = 24)
|
||||
; entry: A = n
|
||||
; exit: HL = Fib(n)
|
||||
;-------------------------------------------------------
|
||||
fib: mov c,a ; C holds n
|
||||
lxi d,0 ; initial value for Fib(n-2)
|
||||
lxi h,1 ; intiial value for Fib(n-1)
|
||||
lxi d,0 ; DE holds Fib(n-2)
|
||||
lxi h,1 ; HL holds Fib(n-1)
|
||||
ana a ; Fib(0) is a special case
|
||||
jnz fib2 ; n > 0 so calculate normally
|
||||
xchg ; otherwise return with HL=0
|
||||
ret
|
||||
fib2: dcr c
|
||||
jz fib3 ; we're done
|
||||
push h ; save Fib(n-1)
|
||||
dad d ; HL holds Fib(n)
|
||||
pop d ; Former Fib(n-1) now Fib(n-2)
|
||||
jmp fib2 ; loop until done
|
||||
dad d ; HL = Fib(n), soon to be Fib(n-1)
|
||||
pop d ; DE = old F(n-1), now Fib(n-2)
|
||||
jmp fib2 ; ready for next pass
|
||||
fib3: ret
|
||||
;-------------------------------------------------------
|
||||
; console output of char in A register
|
||||
;-------------------------------------------------------
|
||||
putchr:
|
||||
push h
|
||||
putchr: push h
|
||||
push d
|
||||
push b
|
||||
mov e,a
|
||||
mvi c,conout
|
||||
mov e,a
|
||||
mvi c,conout
|
||||
call bdos
|
||||
pop b
|
||||
pop d
|
||||
|
|
@ -67,29 +71,29 @@ putchr:
|
|||
putdec: push b
|
||||
push d
|
||||
push h
|
||||
lxi b,-10
|
||||
lxi d,-1
|
||||
lxi b,-10
|
||||
lxi d,-1
|
||||
putdec2:
|
||||
dad b
|
||||
inx d
|
||||
jc putdec2
|
||||
lxi b,10
|
||||
dad b
|
||||
dad b
|
||||
inx d
|
||||
jc putdec2
|
||||
lxi b,10
|
||||
dad b
|
||||
xchg
|
||||
mov a,h
|
||||
ora l
|
||||
cnz putdec ; recursive call
|
||||
mov a,e
|
||||
adi '0'
|
||||
mov a,h
|
||||
ora l
|
||||
cnz putdec ; recursive call
|
||||
mov a,e
|
||||
adi '0'
|
||||
call putchr
|
||||
pop h
|
||||
pop d
|
||||
pop b
|
||||
ret
|
||||
;-------------------------------------------------------
|
||||
; message and data area
|
||||
; data area
|
||||
;-------------------------------------------------------
|
||||
signon: db 'First 20 Fibonacci numbers:',cr,lf,'$'
|
||||
signon: db 'Fibonacci number sequence:',cr,lf,'$'
|
||||
oldstk: dw 0
|
||||
stack equ $+128 ; 64-level stack
|
||||
;
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ public FibonacciGenerator
|
|||
long n_2 := 1l;
|
||||
long n_1 := 1l;
|
||||
|
||||
yield:n_2;
|
||||
yield:n_1;
|
||||
$yield n_2;
|
||||
$yield n_1;
|
||||
|
||||
while(true)
|
||||
{
|
||||
long n := n_2 + n_1;
|
||||
|
||||
yield:n;
|
||||
$yield n;
|
||||
|
||||
n_2 := n_1;
|
||||
n_1 := n
|
||||
|
|
|
|||
11
Task/Fibonacci-sequence/Grain/fibonacci-sequence-1.grain
Normal file
11
Task/Fibonacci-sequence/Grain/fibonacci-sequence-1.grain
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import String from "string"
|
||||
import File from "sys/file"
|
||||
let rec fib = n => if (n < 2) {
|
||||
n
|
||||
} else {
|
||||
fib(n - 1) + fib(n - 2)
|
||||
}
|
||||
for (let mut i = 0; i <= 20; i += 1) {
|
||||
File.fdWrite(File.stdout, Pervasives.toString(fib(i)))
|
||||
ignore(File.fdWrite(File.stdout, " "))
|
||||
}
|
||||
17
Task/Fibonacci-sequence/Grain/fibonacci-sequence-2.grain
Normal file
17
Task/Fibonacci-sequence/Grain/fibonacci-sequence-2.grain
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import File from "sys/file"
|
||||
let fib = j => {
|
||||
let mut fnow = 0, fnext = 1
|
||||
for (let mut n = 0; n <= j; n += 1) {
|
||||
if (n == 0 || n == 1) {
|
||||
let output1 = " " ++ toString(n)
|
||||
ignore(File.fdWrite(File.stdout, output1))
|
||||
} else {
|
||||
let tempf = fnow + fnext
|
||||
fnow = fnext
|
||||
fnext = tempf
|
||||
let output2 = " " ++ toString(fnext)
|
||||
ignore(File.fdWrite(File.stdout, output2))
|
||||
}
|
||||
}
|
||||
}
|
||||
fib(20)
|
||||
23
Task/Fibonacci-sequence/Grain/fibonacci-sequence-3.grain
Normal file
23
Task/Fibonacci-sequence/Grain/fibonacci-sequence-3.grain
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import Buffer from "buffer"
|
||||
import String from "string"
|
||||
let fib = j => {
|
||||
// set-up minimal, growable buffer
|
||||
let buf = Buffer.make(j * 2)
|
||||
let mut fnow = 0, fnext = 1
|
||||
for (let mut n = 0; n <= j; n += 1) {
|
||||
if (n == 0 || n == 1) {
|
||||
Buffer.addChar(' ', buf)
|
||||
Buffer.addString(toString(n), buf)
|
||||
} else {
|
||||
let tempf = fnow + fnext
|
||||
fnow = fnext
|
||||
fnext = tempf
|
||||
Buffer.addChar(' ', buf)
|
||||
Buffer.addString(toString(fnext), buf)
|
||||
}
|
||||
}
|
||||
// stringify buffer and return
|
||||
Buffer.toString(buf)
|
||||
}
|
||||
let output = fib(20)
|
||||
print(output)
|
||||
Loading…
Add table
Add a link
Reference in a new issue