Kerr-Mudd,John
2020-04-13 12:01:19 UTC
John Horton Conway left us 2 days ago;
https://en.wikipedia.org/wiki/John_Horton_Conway#Conway's_Game_of_Life
I've been looking at this tiny life implementation in asm:
I think I've figured out how it works; the new life/death is very clever.
(I can't recall where I got it from now; I may have changed some comments
)
; Life simulator, 72 bytes - Vladislav Kaipetsky and Tenie Remmel
; 65 bytes - Mark Andreas
; If no args, regs on startup are:
; AX = BX = 0000h
; SI = IP = 0100h
; DI = SP = FFFEh
;IDEAL
;MODEL TINY
;P386
;CODESEG
;; MJ nasm Feb 2020
cpu 186
ORG 100h
Start: int 1ah ; ah=00: cx=hours, dx=tic counter
mov al,13h ; Set mode 13h
int 10h
push 09000h ; DS = last 64K segment
pop ds
push 0A000h ; ES = video memory
pop es
xchg ax,dx ; tick in ax
RandLoop:
rol ax,1 ; Generate random number
adc [bx],al ;;; set starting life pattern
dec bx
jnz RandLoop
; BX will not be equal to 3 the first time this loop is executed, but
; it will be for all other times. As SI = 0100h and DI = FFFEh on
; startup, SI - DI will be equal to 258.
LifeLoop:
xchg cx,ax ; reset acc
AccLoop:
add cl,[di+bx-64] ; Add in this column
add cl,[si+bx-2]
add cl,[si+bx+318]
dec bx ; Loop back
jnz AccLoop
mov al,[si] ; Get center cell
stosb ;;; to screen
stc ; 3 = birth, 4 = stay (tricky):
rcr al,cl ; 1.00?0000x --> 0.0x100?00 (rcr 3)
and al,20h ; ^carry | ^
; +---> 0.00x100?0 (rcr 4)
or [si],al ; Add in new cell ^
shr byte [di-65],5 ; Shift value in prev row
mov bl,3 ; 3 iterations in AccLoop
inc si ; Loop while not zero
jnz LifeLoop
mov ah,1 ; Check for key
int 16h
jz LifeLoop ; Loop if no key
xchg ax,bx ; Set text mode
int 10h
ret ; Return
;End Start
;Note from John:
;You may replace the key check with the following snippet:
; in al,60h
; das
; jc LifeLoop
https://en.wikipedia.org/wiki/John_Horton_Conway#Conway's_Game_of_Life
I've been looking at this tiny life implementation in asm:
I think I've figured out how it works; the new life/death is very clever.
(I can't recall where I got it from now; I may have changed some comments
)
; Life simulator, 72 bytes - Vladislav Kaipetsky and Tenie Remmel
; 65 bytes - Mark Andreas
; If no args, regs on startup are:
; AX = BX = 0000h
; SI = IP = 0100h
; DI = SP = FFFEh
;IDEAL
;MODEL TINY
;P386
;CODESEG
;; MJ nasm Feb 2020
cpu 186
ORG 100h
Start: int 1ah ; ah=00: cx=hours, dx=tic counter
mov al,13h ; Set mode 13h
int 10h
push 09000h ; DS = last 64K segment
pop ds
push 0A000h ; ES = video memory
pop es
xchg ax,dx ; tick in ax
RandLoop:
rol ax,1 ; Generate random number
adc [bx],al ;;; set starting life pattern
dec bx
jnz RandLoop
; BX will not be equal to 3 the first time this loop is executed, but
; it will be for all other times. As SI = 0100h and DI = FFFEh on
; startup, SI - DI will be equal to 258.
LifeLoop:
xchg cx,ax ; reset acc
AccLoop:
add cl,[di+bx-64] ; Add in this column
add cl,[si+bx-2]
add cl,[si+bx+318]
dec bx ; Loop back
jnz AccLoop
mov al,[si] ; Get center cell
stosb ;;; to screen
stc ; 3 = birth, 4 = stay (tricky):
rcr al,cl ; 1.00?0000x --> 0.0x100?00 (rcr 3)
and al,20h ; ^carry | ^
; +---> 0.00x100?0 (rcr 4)
or [si],al ; Add in new cell ^
shr byte [di-65],5 ; Shift value in prev row
mov bl,3 ; 3 iterations in AccLoop
inc si ; Loop while not zero
jnz LifeLoop
mov ah,1 ; Check for key
int 16h
jz LifeLoop ; Loop if no key
xchg ax,bx ; Set text mode
int 10h
ret ; Return
;End Start
;Note from John:
;You may replace the key check with the following snippet:
; in al,60h
; das
; jc LifeLoop
--
Bah, and indeed, Humbug.
Bah, and indeed, Humbug.