MAIN 	START
	CALL MAIN1
	CALL PN100
	CALL VIET
	CALL LIST792J
	CALL REVN
	RET
	END
;-------------------------
MAIN1 START
  LADあ GR1,DATA
LP LD GR2,0,GR1
  ADDA GR3,GR2
  LAD GR1,1,GR1
  CPL GR1,LAST
  JMI LP
  ST GR3,GOUKEI
  RET
DATA DC 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
LAST DC LAST
GOUKEI DS 1
  END
; DATA の中の数値をひとつひとつ取り出しながら
; 加算を行い 合計値を　GOUKEI　へ格納する。
; LAST DC LAST とは、ラベル LAST のアドレスを
; 定数値として格納するの意味になります。
;--------------------------
PN100   START
  LAD GR1,1
  LAD GR4,2
  ST GR4,ALL
  ST GR4,PN
  LAD GR0,3
  ST GR0,PN,GR1
  LAD GR2,5
  LD GR5,GR2
  XOR GR3,GR3
  JUMP DIV2
DIV CPL GR4,GR7
  JPL NZ8
DIV2 XOR GR6,GR6
  XOR GR7,GR7
CL2 LD GR0,GR4
  ADDL GR6,GR1
  AND GR0,MSK
  JNZ CL4
  SLL GR4,1
  JUMP CL2
CL4 CPL GR2,GR4
  JPL PLS
  JMI MIS
  JZE ZER
PLS OR GR7,GR1
  SUBL GR2,GR4
  SRL GR4,1
  SUBL GR6,GR1
  JZE PM
  SLL GR7,1
  JPL CL4
MIS SRL GR4,1
  SUBL GR6,GR1
  JZE PM 
  SLL GR7,1
  JUMP CL4
ZER OR GR7,GR1
  SLL GR7,-1,GR6
  JUMP NL
; XOR GR2,GR2
; JUMP PM
PM LD GR2,GR2
  JNZ NZ8
NL LD GR2,GR5
  JUMP NPN
NZ8 LD GR2,GR5
  ADDL GR3,GR1
  CPL GR3,ALL
  JNZ PNX
  LD GR7,GR2
  ST GR2,PN,GR3
  ADDL GR3,GR1
  ST GR3,ALL
  CPL GR3,LIM
  JZE FIN
NPN ADDL GR2,GR1
  ADDL GR2,GR1
  LD GR5,GR2
  LD GR3,GR1
PNX LD GR4,PN,GR3
  JUMP DIV
FIN RET
MSK DC #4000
ALL DS 1
LIM DC       100
PN DS        100
  END
LIST792J START
  CALL REVN
  RET
  END
;------------------------------
; ハノイの塔を解くプログラム
VIET   START
       LD      GR0,N
       LD      GR1,A
       LD      GR2,B
       LD      GR3,C
       CALL    HANOI       ; hanoi(3, A, B, C)
       RET
; hanoi(N, X, Y, Z)
HANOI  CPA     GR0,=1      ; if N == 1 then
       JZE     DISP        ;  move it, return
       SUBA    GR0,=1      ; N - 1
       PUSH    0,GR2       ; swap GR2 GR3
       LD      GR2,GR3
       POP     GR3
       CALL    HANOI       ; hanoi(N-1, X, Z, Y)
       ST      GR1,MSG1
       ST      GR2,MSG2    ; now GR2 holds Z
       OUT     MSG,LNG     ; 'from X to Z'
       PUSH    0,GR2       ; rotate registers
       LD      GR2,GR1
       LD      GR1,GR3
       POP     GR3
       CALL    HANOI       ; hanoi(N-1, Y, X, Z)
       PUSH    0,GR2       ; restore registers
       LD      GR2,GR1
       POP     GR1
       ADDA    GR0,=1      ; also N has restored
       RET

DISP   ST      GR1,MSG1    ; 'from X to Z'
       ST      GR3,MSG2
       OUT     MSG,LNG
       RET

N      DC      3        ; 輪の総数
LNG    DC      11       ; メッセージの長さ
A      DC      'A'
B      DC      'B'
C      DC      'C'
MSG    DC      'from '
MSG1   DS      1
       DC      ' to '
MSG2   DS      1
       END

;----------------------------
;１０語の 語の並び と
;ビット列を逆転する。
;----------------------
REVN START
  LAD GR1,0  
  LAD GR6,9
LP LD GR0,TBL,GR1
  CALL REVERS
  ST GR0,REV,GR6
  LAD GR1,1,GR1
  LAD GR6,-1,GR6
  CPA GR6,=0
  JMI FIN
  LD GR0,TBL,GR1  
  JUMP LP
FIN RET
TBL DC #9368,5179,#F7A3
    DC #16C9,4079,8877
    DC 5566,3333,2222
    DC 1111, 7788, 'Rhino S. Yamase'
; DC #93AF,'12345678901234567890',-876,+777
; DC #93AF,'12345678901234567890',-876,+777,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,-1 
REV DS 10
  END
;------------------
REVERS START
  PUSH 0,GR1
  PUSH 0,GR2
  PUSH 0,GR5
  PUSH 0,GR6

;  LD GR0,MAE
;  ST GR0,MAE
  LAD GR6,0
  LAD GR3,16
LP SRL GR0,0
  JZE FIN 
  LAD GR3,-1,GR3
  SLL GR6,1
  SRL GR0,1
  JOV ONE
  JUMP LP
ONE OR GR6,=1
  JUMP LP
FIN SLL GR6,0,GR3
  LD GR0,GR6
  ST GR0,ATO
  POP GR6
  POP GR5
  POP GR2
  POP GR1
  RET
 DC #9368
ATO DS 1
  END
:-----------------------------