module Execute_Computer_Zero{ global byte MEM[31]=0XFF, PC=0 module ZERO (program$, f=-2, showrun, skipreadcode){ Enum Commands { NOP=0X0, LDA=0X20, STA=0X40, ADD=0X60, SUB=0X80, BRZ=0XA0, JMP=0XC0, STP=0XE0 } if skipreadcode then goto cont1 PC<=0 dim code$() var crlf=chr$(13)+chr$(10) ' CRLF var prname$="Program #1" program$=replace$(chr$(9)," ", program$) program$=replace$(",",crlf, program$) program$=ucase$(filter$(program$, "().")) code$()=piece$(program$, crlf) document source$ for I=0 to len(code$())-1 tok$=trim$(LeftPart$(trim$(code$(I))+" ", " ")) part$=trim$(RightPart$(trim$(code$(I))+" 0", " ")) val=val(part$) MOD 32 ch$=left$(tok$,1) c=0 if ch$="#" then prname$=mid$(trim$(code$(I)), 2) continue end if tok$=filter$(tok$, "+-_x!@#$%^&*/|><") if ch$>="0" and ch$<="9" then val=int(val(tok$)) mod 256 source$ =" "+hex$(PC, 1)+" "+hex$(val,1)+crlf else.if tok$="" then continue else.if valid(eval(tok$)) then c=eval(tok$) source$ =" "+hex$(PC, 1)+" "+tok$+" "+hex$(val,1)+crlf end if MEM[PC]<=c+VAL PC++ next Print #f, "Program Name:";prname$ Print #f, source$; Print #f, "Program Length ";PC;"bytes" PC<=0 cont1: byte A=0, ADDR=0 structure Accumulator { { A as Byte, Overflow as Byte } R as Integer } Accumulator AC CM=STP if showrun then Print #f, "PC MEM COM ADDR AC" do if inkey$=" " then Print "Break": exit A=MEM[PC] ADDR=A MOD 32 CM=A-ADDR if showrun then Print #f, Hex$(PC,1);" ";hex$(A,1);" ";EVAL$(CM);" ";hex$(ADDR, 1);" ";Hex$(AC|A, 1) select case CM case NOP PC++ : PC|MOD 32 case LDA AC|R=MEM[ADDR]:PC++ : PC|MOD 32 case STA MEM[ADDR]<=AC|A : PC++ : PC|MOD 32 case ADD AC|R=AC|R+MEM[ADDR]:PC++ : PC|MOD 32 case SUB AC|R=uint(AC|R-MEM[ADDR]): PC++ : PC|MOD 32 case BRZ { if AC|A then PC++ : PC|MOD 32 if showrun then Print #f, "NON ZERO - CONTINUE" else PC<=A MOD 32 if showrun then Print #f, "BRANCH TO ";PC end if } case JMP PC<=A MOD 32:if showrun then Print #f, "JUMP TO ";PC case STP exit end select always if skipreadcode else Print #f, "STOP AT ";hex$(PC,1) Print #f, "RESULT:";AC|A;"|0x";hex$(AC|A, 1) Print #f, "NEGATIVE:"; IF$(AC|A>126->"1","0") Print #f, "OVERFLOW:"; IF$(AC|Overflow>0->"1","0") Print #f end if } Flush Data { #add 2 LDA 3, ADD 4, STP 0, 2, 2 } Data {#fibonacci LDA 14, STA 15, ADD 13, STA 14, LDA 15, STA 13, LDA 16, SUB 17, BRZ 11, STA 16, JMP 0, LDA 14, STP 0, 1, 1, 0 , 8, 1 } Data {#8*7 LDA 12, ADD 10, STA 12, LDA 11, SUB 13, STA 11, BRZ 8, JMP 0, LDA 12, STP 0, 8, 7, 0, 1 } Data {#linkedList LDA 13, ADD 15, STA 5, ADD 16, STA 7, NOP 0, STA 14, NOP 0 , BRZ 11, STA 15, JMP 0, LDA 14, STP 0, LDA 0, 0, 28 , 1, 0, 0, 0, 6, 0, 2, 26 , 5, 20, 3, 30, 1, 22, 4, 24 } Data { #0-255 LDA 3, SUB 4, STP 0, 0, 255 } Data {#0-1 LDA 3, SUB 4, STP 0, 0, 1 } Data {#1+255 LDA 3, ADD 4, STP 0, 1, 255 } filename="run01.txt" open filename for wide output as #d while not empty Zero letter$, d, false, false end while Zero { #prisoner NOP 0, NOP 0, STP 0, 0, LDA 3, SUB 29, BRZ 18, LDA 3 , STA 29, BRZ 14, LDA 1, ADD 31, STA 1, JMP 2, LDA 0, ADD 31 , STA 0, JMP 2, LDA 3, STA 29, LDA 1, ADD 30, ADD 3, STA 1 , LDA 0, ADD 30, ADD 3, STA 0, JMP 2, 0, 1, 3 }, d, false, false Enum Action {cooperate=0UB, defect} byte AcA PM=defect for i=1 to 5 Print #d, "Round ";i;" ";field$(eval$(PM), 10); PC++ mem[PC]<=pm IF PM=cooperate THEN PM++ ELSE PM-- PC++ Zero {}, d, false, true Read AcA Print #d, "Player:";mem[0], " Computer:";mem[1] next close #d if filename<>"" then win dir$+filename } Execute_Computer_Zero