
uLisp – ARM Assembler in Lisp - eggy
http://www.ulisp.com/show?2XZH
======
siraben
I find Lisps to be very well suited towards this task, because of its
homoiconicity and features like quasiquotation, effectively you have access to
the best macro system possible.

Related: Z80 assembler in Scheme

[https://github.com/siraben/zkeme80/blob/master/src/assembler...](https://github.com/siraben/zkeme80/blob/master/src/assembler.scm)

~~~
speps
You might have an idea on this but how would you approach the opposite
operation? Assembly to an in-memory representation, how does Lisp/Scheme
handle that kind of manipulation?

~~~
siraben
Do you mean how opcodes are represented in Lisp-based assemblers?

~~~
speps
No I mean a disassembler, I can see how an assembler is nice to write in a
language like that but I've never seen much byte manipulation examples. For
example going from machine code to the list of opcodes for example.

~~~
lispm
Every Common Lisp has a DISASSEMBLE function built-in. Compile a function and
call the disassembler.

For example SBCL

[https://github.com/sbcl/sbcl/blob/master/src/compiler/disass...](https://github.com/sbcl/sbcl/blob/master/src/compiler/disassem.lisp)

and its ARM64 instructions:

[https://github.com/sbcl/sbcl/blob/master/src/compiler/arm64/...](https://github.com/sbcl/sbcl/blob/master/src/compiler/arm64/insts.lisp)

~~~
dreamcompiler
Here's the times13 function in CCL on x86-64. (Note that #x68 is 13 shifted
left 3 times to allow tag bits in the LS 3 bit positions. Also the below could
be optimized a lot more but this is the default compilation.)

    
    
      ? (defun times13 (x) (* 13 x))
      TIMES13
      ? (disassemble 'times13)
      ;;; (defun times13 (x) (* 13 x))
          (recover-fn-from-rip)                   ;     [0]
          (cmpl ($ 8) (% nargs))                  ;     [7]
          (jne L41)                               ;    [10]
          (pushq (% rbp))                         ;    [12]
          (movq (% rsp) (% rbp))                  ;    [13]
          (pushq (% arg_z))                       ;    [16]
    
      ;;; (* 13 x)
          (movl ($ #x68) (% arg_y.l))             ;    [17]
          (movq (@ -8 (% rbp)) (% arg_z))         ;    [22]
          (movq (% rbp) (% rsp))                  ;    [26]
          (popq (% rbp))                          ;    [29]
          (jmp (@ .SPBUILTIN-TIMES))              ;    [30]
    
      ;;; #<no source text>
    
          (:align 3)
      L41                                         ;   [@56]
          (uuo-error-wrong-number-of-args)        ;    [41]
      NIL

------
lebuffon
Nice move by uLisp.

It's powerful to re-code time critical snippets of code in Assembler, while
still having access to a high level language like Lisp for the complex stuff.

It might interest people to know that Forth always offered this functionality
and the ':' compiler lets one build ASM macros as well.

~~~
bordercases
I was wondering why one person on Hackaday thought Forth was uniquely suited
for exploration in embedded systems programming other than also being a stack
language like Assembly. Thanks for pointing this out.

------
monocasa
Neat reminds me of Game Oriented Assembly Lisp, the language used for a bunch
of the Naughty Dog games. It included inline assemblers for the various
processors in the PS2, and was used in a very procedural, macro heavy style
that this seems to be encouraging.

Neat!

Here's an example of GOAL code with inline code for the vector processors.

[https://web.archive.org/web/20070127022728/http://lists.midn...](https://web.archive.org/web/20070127022728/http://lists.midnightryder.com/pipermail/sweng-
gamedev-midnightryder.com/2005-August/003804.html)

------
exdsq
I asked a question on HN but didn't get any answers - this might be an easier
forum to get one.

I'm working on a hobbyist rocketry project where I'm helping build the flight
computer - basically an Arduino Nano connected to modules like a GPS system,
accelerator, gyroscope, etc. Are there any Lisp dialects that someone would
recommend for an Arduino? The Nano is technically replaceable so if there are
better boards that can use Lisp I'd be interested too!

~~~
reaperducer
According to recent articles on HN, NASA used to use Macintosh Common Lisp for
many of its projects.

I'm not an Arduino person, but maybe there's a Common Lisp variant available
to you.

~~~
gte525u
They also had several megabytes of RAM.

~~~
exdsq
Yep this is an important point - I had 2 KB of RAM available and uLisp leaves
me with 518 bytes.. I need to go smaller!

~~~
gte525u
GP was talking about common lisp. Even CLISP requires ~4MB for the heap.

------
tieze
Ah yea, I did something like this on top of Common Lisp, but not as
interactive yet:
[https://github.com/stuij/armish](https://github.com/stuij/armish)

My plan at the time was to use a serial protocol and interface with a Nintendo
GBA/DS for this kind of interactive Lisp programming.

It feels a bit dirty mixing implementation and target language like this, but
it's great fun to program for.

------
jimbokun
Knowing nothing about uLisp beyond glancing at the code snippets, this seems
like it would be a fun tool to use for a CS class on low level programming and
compilers.

------
eggy
The author also has a uLisp to C converter and sensor library in uLisp. Very
useful and fun!

~~~
eggy
I came upon uLisp over a few years ago, and was glad to see it show up for the
ESP8266 chips. It makes programming these tiny controllers with WiFi
enjoyable. Simple things like leaving the parameters blank in a WiFi connect
function disconnects it from any current connection. It also blends in with my
love of the minimal. I had been playing with Wasp Lisp [1,2] at the time when
I found uLisp.

[1] [https://github.com/swdunlop/WaspVM](https://github.com/swdunlop/WaspVM)

[2] [https://github.com/doublec/WaspVM](https://github.com/doublec/WaspVM)

------
cmrdporcupine
Can anybody speak to how hard uLisp would be to port to RISC-V? It looks
pretty nifty. I have a little hobby computer I'm building around a RISC-V
core, it might be nice to have it boot to this.

~~~
lispm
I don't think it's too difficult. It's written in C and it has also been
ported to ARM...

~~~
cmrdporcupine
Thanks, just read through the source
([https://github.com/technoblogy/ulisp/blob/master/ulisp.ino](https://github.com/technoblogy/ulisp/blob/master/ulisp.ino))
and it looks do-able.

------
kristianp
The call function has no parameters. Does this mean you can only have one
assembled function at a time?

~~~
johnsondavies
Yes, it currently provides one assembler function, but I plan to extend it to
support multiple named functions.

