#### 3.4.3 Defining new functions ¶

Scheme is a powerful language, and you can use it inside `mixguile` to easily extend the MIX interpreter’s capabilities. For example, you can easily define a function that loads a file, prints its name, executes it and, finally, shows the registers contents, all in one shot:

    guile> (define my-load-and-run  RET
             (lambda (file)   RET
               (mix-load file)   RET
               (display "File loaded: ")   RET
               (mix-pprog)   RET
               (mix-run)   RET
               (mix-preg)))   RET
    guile>

and use it to run your programs:

    guile> (my-load-and-run "hello")
    Program loaded. Start address: 3000
    File loaded: hello.mix
    Running ...
    MIXAL HELLO WORLD
    ... done
    Elapsed time: 11 /Total program time: 11 (Total uptime: 33)
    rA: + 00 00 00 00 00 (0000000000)
    rX: + 00 00 00 00 00 (0000000000)
    rJ: + 00 00 (0000)
    rI1: + 00 00 (0000)	rI2: + 00 00 (0000)
    rI3: + 00 00 (0000)	rI4: + 00 00 (0000)
    rI5: + 00 00 (0000)	rI6: + 00 00 (0000)
    guile>

Or, maybe, you want a function which sets a breakpoint at a specified line number before executing it:

    guile> (define my-load-and-run-with-bp
             (lambda (file line)
               (mix-load file)
               (mix-sbp line)
               (mix-run)))
    guile> (my-load-and-run-with-bp "samples/primes" 10)
    Program loaded. Start address: 3000
    Breakpoint set at line 10
    Running ...
    ... stopped: breakpoint at line 10 (address 3001)
    Elapsed time: 1 /Total program time: 1 (Total uptime: 45)
    guile>

As a third example, the following function loads a program, runs it and prints the contents of the memory between the program’s start and end addresses:

    guile> (define my-run
             (lambda (file)
               (mix-load file)
               (let ((start (mix-loc)))
                 (mix-run)
                 (mix-pmem start (mix-loc)))))
    guile> (my-run "hello")
    Program loaded. Start address: 3000
    Running ...
    MIXAL HELLO WORLD
    ... done
    Elapsed time: 11 /Total program time: 11 (Total uptime: 11)
    3000: + 46 58 00 19 37 (0786957541)
    3001: + 00 00 00 02 05 (0000000133)
    3002: + 14 09 27 01 13 (0237350989)
    guile>

As you can see, the possibilities are virtually unlimited. Of course, you don’t need to type a function definition each time you start `mixguile`. You can write it in a file, and load it using Scheme’s `load` function. For instance, you can create a file named, say, `functions.scm` with your definitions (or any Scheme expression) and load it at the `mixguile` prompt:

    guile> (load "functions.scm")

Alternatively, you can make `mixguile` to load it for you. When `mixguile` starts, it looks for a file named `mixguile.scm` in your MDK configuration directory (`~/.mdk`) and, if it exists, loads it before entering the REPL. Therefore, you can copy your definitions in that file, or load the `functions.scm` file in `mixguile.scm`.