Rich Mellor wrote, on 02/11/2010 19:53:
I was just reading that 99/100 job applicants for programming jobs apparently cannot write a short "fizzbuzz" program when given 10 minutes to do so in an interview.

The rules are simple -
write out the values 1 to 100 on screen
If the value is divisble by 3 write 'FIZZ' after the number
If the value is divisble by 5 write 'BUZZ' after the number
If the value is divisble by 3 and 5 write 'FIZZBUZZ' after the number

My inelegant machine code program (untested) is:

Sorry - made an error when typing the 68000 assembly language in - I hit a key which submitted the form!

start MOVEQ #-1,D1 ;Open a screen channel
      LEA screen,A0
      MOVEQ #2,D3
      MOVEQ #1,D0
      TRAP #3 ;Uses built in ROM routine to open screen
      BNE.S end
      MOVEQ #6,D1
      MOVE.L A1,$58(A6) ; make room on maths stack for 6 bytes
      MOVE.W $11A,A2
      JSR (A2)
      MOVE.L $58(A6),A1
      MOVEQ #1,D6 ;Initial Loop value
loop  MOVE.L (A6),A1
      LEA 2(A1),A0
      MOVE.W D6,0(A6.A1.L) ;store value on stack
      MOVE.W $F2,A2
      JSR (A2)   ;convert number to ASCII
      MOVE.W D1,D2      ; grab length of number
      MOVEQ #7,D0
      MOVEQ #-1,D3
      TRAP #3
      MOVE.L D6,D7
      DIVU #3,D7
      SWAP D7
      CMPI.W #0,D7
      BEQ.S testbuzz
      LEA fizz,A1
      MOVE.W $D0,A2 ; print string
      JSR (A2)
testbuzz MOVE.L D6,D7
      DIVU #5,D7
      SWAP D7
      CMPI.W #0,D7
      BEQ.S nwline
      LEA buzz,A1
      MOVE.W $D0,A2
      JSR (A2)
nwline MOVEQ #10,D1 ;Print a new line
      BSR.S printchr
      CMPI.B #100,D6
      BEQ.S end
      ADDQ.B #1,D6
      BRA.S loop
end   MOVEQ #0,D0
      RTS
printchr MOVEQ #-1,D3
         MOVEQ #5,D0
         TRAP #3
         MOVEQ #0,D0
         RTS
screen DC.W 17
       DC.B 'con_448x200a32x16',0
fizz   DC.W #4
       DC.B 'FIZZ'
buzz   DC.W #4
       DC.B 'BUZZ'

I am sure plenty can write this in SuperBASIC, but is there a simpler solution in machine code?

sB is my limit (8-)# I would love to see Laurence Reeves' version. He was always looking for how to reduce code. He, for instance, did a *very* tiny Forth for the QL. There were loads of examples in Hermes and (especially) superHermes code. One though from his extension code springs to mind - bp_init? To save a byte(?), instead of doing an RTS on the final extension he did a RTN. I probably have that wrong though (8-)#
Are you lurking Lau?

Tony

_______________________________________________
QL-Users Mailing List
http://www.q-v-d.demon.co.uk/smsqe.htm

Reply via email to