BBC BASIC for Windows
General >> General Board >> A place for little code snippets?
http://bb4w.conforums.com/index.cgi?board=general&action=display&num=1421977828

A place for little code snippets?
Post by David Williams on Jan 23rd, 2015, 12:50am

I think it would be nice if this forum had a section dedicated - not necessarily to "discussion" of someone's code, but for (typically small and usually finished) programs that BB4W users write that can be included in a post (between the code tags) that others can simply copy, paste and run for their own amusement or perhaps education. A general 'code dump' as it were - in the spirit of "Here's a little program I wrote - copy, paste & run it - no comments required or invited but if you must or want to comment then do go ahead".

Does anyone else agree with this idea?

I notice the LB forum has a section called "Public Domain Snippets". Its description states: "Original authors may place code into the public domain here. Members cannot reply to posts on this board."

As for not being able to reply to such posts, I'm not sure what the rationale behind that is.


David.
--

PS. For example, the following two programs (one BASIC, the other assembler code) for decomposing positive integers into sums of four squares. They are insubstantial, I'm not necessarily inviting discussion about them (although I'm not averse to that), but they are BB4W programs, and may be of interest to someone.

(Copy, paste & run):


Code:
      REM Lagrange's four-square theorem
      REM (a.k.a. Bachet's conjecture)
      REM Inefficient naive/brute force algorithm
      REM BBC BASIC version

      FOR N% = 10000 TO 1000000
        PROC_FourSquares( N%, I%, J%, K%, L% )
        PRINT ;N%;" = ";I%;"^2 + ";J%;"^2 + ";K%;"^2 + ";L%;"^2"
      NEXT N%
      END

      DEF PROC_FourSquares( N%, RETURN I%, RETURN J%, RETURN K%, RETURN L% )
      LOCAL S%, X%, Y%, Z%
      S% = 1+SQR(N%)
      FOR I% = 0 TO S%
        X% = I%^2
        FOR J% = I% TO S%
          Y% = J%^2
          FOR K% = J% TO S%
            Z% = X% + Y% + K%^2
            FOR L% = K% TO S%
              IF Z% + L%^2 = N% THEN
                ENDPROC
              ENDIF
            NEXT
          NEXT
        NEXT
      NEXT
      VDU 7 : PRINT '"Wasn't able to decompose "; N%;" !"
      END
      ENDPROC
 



Assembler code version (copy, paste & run):

Code:
      REM Lagrange's four-square theorem
      REM (a.k.a. Bachet's conjecture)
      REM Inefficient naive/brute force algorithm
      REM Assembly language version

      code% = FNassemble

      FOR N% = 10000 TO 1000000
        CALL code%
        IF I% <> -1 THEN
          PRINT ;N%;" = ";I%;"^2 + ";J%;"^2 + ";K%;"^2 + ";L%;"^2"
        ELSE
          PRINT "Couldn't decompose "; N%; " !!!"
          VDU 7 : STOP
        ENDIF
      NEXT N%
      END

      DEF FNassemble
      LOCAL code%, I%, P%
      LOCAL i_lp, j_lp, k_lp, l_lp, failure, success
      DIM code% 1023
      FOR I% = 0 TO 2 STEP 2
        P% = code%
        [OPT I%
  
        ; N% = number to decompose
  
        pushad
  
        mov ebp, [^N%]
  
        push ebp
        finit
        fild DWORD [esp]
        fsqrt
        fistp DWORD [^S%]
        add esp, 4
        add DWORD [^S%], 1
  
        mov [^E%], esp
  
        xor eax, eax  ; I%
        .i_lp
        mov edi, eax
        imul edi, edi ; X% = I%^2
  
        mov ebx, eax  ; J%
        .j_lp
        mov esi, ebx
        imul esi, esi ; Y% = J%^2
  
        mov ecx, ebx  ; K%
        .k_lp
        mov ebp, ecx
        imul ebp, ebp ; Z% = K%^2
        add ebp, edi  ; Z% = X% + K%^2
        add ebp, esi  ; Z% = X% + Y% + K%^2
  
        mov edx, ecx  ; L%
        .l_lp
        mov esp, edx
        imul esp, esp
        add esp, ebp  ; Z% + L%^2
  
        cmp esp, [^N%]
        je success
  
        add edx, 1
        cmp edx, [^S%]
        jle l_lp
  
        add ecx, 1
        cmp ecx, [^S%]
        jle k_lp
  
        add ebx, 1
        cmp ebx, [^S%]
        jle j_lp
  
        add eax, 1
        cmp eax, [^S%]
        jle i_lp
  
        .failure
        mov esp, [^E%]
        mov DWORD [^I%], -1
        popad
        ret
  
        .success
        mov esp, [^E%]
        mov [^I%], eax
        mov [^J%], ebx
        mov [^K%], ecx
        mov [^L%], edx
        popad
        ret
  
        ]
      NEXT I%
      = code%
 

Re: A place for little code snippets?
Post by Stripey on Jan 23rd, 2015, 08:03am

Appeals to me.

I like to see and learn from other people's programs.

User level: tinkerer

Stripey
Re: A place for little code snippets?
Post by sveinioslo on Mar 30th, 2015, 1:15pm

Demo of a self modifying program.

How to, for those who would like to experiment with these things.
Use module viewer in the utilities menu to watch your creations. (for those who didn't know, if any)

Practical example:
I am working on a program who accepts a comma separated list of commands.
If the split/trim and parsing takes too long, i can choose to make a macro out of it.
At the lowest level, there is one last choice:
Code:
IF macro% THEN PRINT#fnr%,"some_bbc_command" : BPUT#fnr%,"" ELSE some_bbc_command
 

The end result can execute several! times faster.

Be creative :)
Svein

Ps. It's also possible to fiddle with the main program, but i think Richard has advised against it.



(Copy, paste & run):
Code:
      REM Demo of how to create,install and run a program (proc) from within a program
      REM Svein Svensson 30.Mar.2015

      REM First example: static proc_name
      A$="Hello"
      PROCmake(A$)
      PROC(Hello%) : REM name known at runtime

      REM Second example: dynamic proc_name
      B$="Hello"+STR$(RND(100000))
      PROCmake(B$)
      A%=EVAL(B$+"%") : PROC(A%) : REM unknown name at runtime
      REM you can also use PROC(EVAL(B$+"%")) , but that is slower if in a loop
      END
      PROCmake("delete_macro_folder") : REM if you wish to erase everything !
      END

      REM ------------------------------------------------------------------------------------

      DEF PROCmake(proc$)
      LOCAL I%,fnr%,fnr2%,a$,dir$,fnr$

      REM create dir and file
      dir$=@tmp$+"Selfmade" : fnr$=dir$+"\"+proc$
      IF proc$="delete_macro_folder" THEN PROCdel_folder(dir$) : ENDPROC
      PROCmakedir(dir$)
      fnr%=OPENOUT(fnr$+".txt")
      IF fnr%=0 THEN ERROR 0,"could not open textout"

      REM create basic text file
      READ a$ : a$+=" "+TIME$
      PRINT#fnr%,a$ : BPUT#fnr%,""
      PRINT#fnr%,"DEF PROC"+proc$ : BPUT#fnr%,""
      READ a$
      WHILE a$<>".."
        PRINT#fnr%,a$ : BPUT#fnr%,""
        READ a$
      ENDWHILE
      CLOSE#fnr%

      REM convert from text to tokens
      fnr%=OPENIN(fnr$+".txt")
      IF fnr%=0 THEN ERROR 0,"could not open textin"
      fnr2%=OPENOUT(fnr$+".bbc")
      IF fnr2%=0 THEN ERROR 0,"could not open bbcout"

      REM tokenise
      REPEAT
        a$=GET$#fnr% TO 10
        I%=EVAL("0:"+a$)
        a$=$(!332+2)
        BPUT#fnr2%,LENa$+4 : BPUT#fnr2%,0 : BPUT#fnr2%,0 : PRINT#fnr2%,a$
      UNTIL EOF#fnr%
      BPUT#fnr2%,0 : BPUT#fnr2%,&FF : BPUT#fnr2%,&FF
      CLOSE#fnr% : CLOSE#fnr2%

      REM delete text file, install bbc file, get proc_adr into proc_name%, create name% if not exist
      OSCLI "del "+fnr$+".txt"
      PROCinstall(fnr$+".bbc")
      !EVAL("^"+proc$+"%")=EVAL("^"+"PROC"+proc$)
      ENDPROC

      DEF PROCmakedir(a$)
      ON ERROR LOCAL : ENDPROC
      OSCLI "MKDIR """+a$+""""
      ENDPROC

      DEF PROCinstall(a$)
      ON ERROR LOCAL : ERROR 0,"macro/library not found."
      INSTALL a$
      ENDPROC

      DEF PROCdel_folder(dir$)
      LOCAL I%,err$
      ON ERROR LOCAL : WAIT 10 : I%+=1 : IF I%>10 THEN err$="Failed to delete "+dir$ : ERROR 0,err$
      OSCLI "*del /Q "+dir$
      OSCLI "rmdir "+dir$
      RESTORE ERROR
      ENDPROC

      DATA REM Automatically generated
      DATA FOR A%=1 TO 10
      DATA PRINT "Hello world ";STR$A%
      DATA NEXT A%
      DATA ENDPROC
      DATA ..
      DATA REM Automatically generated
      DATA INSTALL @lib$+"winlib2"
      DATA ON CLOSE PROC_closedialog(dlg%)
      DATA "dlg%=FN_newdialog("""",100,100,200,250,8,2000)"
      DATA FOR A%=0 TO 20
      DATA "PROC_static(dlg%,""Hello world ""+STR$A%,100+A%,10,10+A%*10,180,10,0)"
      DATA NEXT A%
      DATA PROC_showdialog(dlg%)
      DATA ENDPROC
      DATA ..

 


Re: A place for little code snippets?
Post by dfeugey on Jul 3rd, 2015, 10:18am

I'm a bit late, but that's exactly what I plan for a new website, dedicated to BBC Basic (RISC OS, Windows, portable).

Last year, www.riscos.fr organized a contest around BBC Basic programming, with a Pi to win. I will launch another one in September, for the new website.