\def\date{12 Nov 2011}\def\source{V1F1, p.\ 48}\def\author{Udo Wermuth}\input mmix =-3 !!\clearline{\tenbf Josephus problem}\smallskip\startnumbering N IS 24 !! initial population $n < 256$ M IS 11 !! every $m$th person is executed; $m>1$ t IS $255 tt GREG 0 !! two additional temporary registers ttt GREG 0 sno IS$0 !! Sequence number cnt IS $1 !! A counter cur IS$2 !! Current element b IS $3 !! Number of empty slots in \mb{out} out IS$4 !! Output 8 characters _mn GREG -N+1 _ones GREG #0101010101010101 LOC Data_Segment _buf GREG @ LOC @+920 !! maximal $2*9+3*90+4*157=916$\thinspace bytes _ctop GREG @+N-1 !! top of circle for \m N bytes LOC #100 Main SET cnt,_mn !1! Store distance to next man in circle. 0H STO _ones,_ctop,cnt !\lceil n/8\rceil! Link each byte with next byte. ADD cnt,cnt,8 !\lceil n/8\rceil! 8 links done. PBNP cnt,0B !\lceil n/8\rceil!\bad1\bad STB _mn,_ctop,0 !1! Last byte jumps back to the first. SET sno,0 !1! Start execution; no one excluded so far. SET cur,_mn !1! Start with first person. 1H SET cnt,M-2 !n-1! Count down $m-2$ links. 2H LDB tt,_ctop,cur !P! Get distance from \mb{cur} to next person ADD cur,cur,tt !P! \quad and follow the link to this person. SUB cnt,cnt,1 !P! PBNZ cnt,2B !P!\bad n-1\bad LDB tt,_ctop,cur !n-1! \mb{cur} survives the round, ADD cnt,cur,tt !n-1! \quad but the next man \mb{cnt} is executed. LDB t,_ctop,cnt !n-1! Get his neighbor. ADD tt,tt,t !n-1! Add it to previous distance for \mb{cur}. STB tt,_ctop,cur !n-1! Store the new value for \mb{cur}. ADD cur,cnt,t !n-1! Move to next man for next round. ADD sno,sno,1 !n-1! Increase the sequence number STB sno,_ctop,cnt !n-1! \quad and store it for the executed man. CMP t,sno,N-1 !n-1! Is only one person left? PBN t,1B !n-1!\bad1\bad If not start count down again. SET sno,N !1! He is the last man STB sno,_ctop,cur !1! \quad and gets the number $n$. Output SET sno,_mn !1! Start the output with first man. SET b,8 !1! 8 chars in \mb{out} are empty. SET cnt,0 !1! Offset in \mb{\l\_buf} to store \mb{out}. 0H LDB t,_ctop,sno !n! Load next sequence number. 2H DIV t,t,10 !D! Extract GET tt,rR !D! \quad the digits ADD tt,tt,'0' !D! \quad from right SL ttt,ttt,8 !D! \quad to left OR ttt,ttt,tt !D! \quad and store them in the register \mb{ttt}. 4H PBNZ t,2B !D!\bad n\bad SET t,' ' !n! Add a space in front of each sequence number. JMP 9F !n! 1H AND t,ttt,#ff !D! Move \mb{ttt} in reverse order to \mb{out}. SR ttt,ttt,8 !D! 9H SL out,out,8 !D+n! ADD out,out,t !D+n! SUB b,b,1 !D+n! PBNZ b,3F !D+n!\bad \lfloor(D+n)/8\rfloor\bad STO out,_buf,cnt !\lfloor(D+n)/8\rfloor! \quad Store the characters into \mb{\l\_buf}. ADD cnt,cnt,8 !\lfloor(D+n)/8\rfloor! SRU out,out,64 !\lfloor(D+n)/8\rfloor! SET b,8 !\lfloor(D+n)/8\rfloor! 3H PBNZ ttt,1B !D+n!\bad n\bad ADD sno,sno,1 !n! Next sequence number. PBNP sno,0B !n!\bad1\bad SL out,out,8 !1! Add null byte to the final octabyte. STO out,_buf,cnt !1! LDA t,_buf !1! Output sequence numbers. TRAP 0,Fputs,StdOut !1! TRAP 0,Halt,0 !1! \eop !!\endwAoA\bye