Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Abusing LuaJIT as a REPL for assembly language
-- warning: this is hacky. run at your own risk.
-- before you run this, put sum_list.asm in a dynamic library.
-- on OSX that means
-- $ nasm -f macho64 sum_list.asm
-- $ libtool -dynamic sum_list.o -o libsum_list.dylib
-- on Linux it means
-- $ nasm -f elf64 sum_list.asm
-- $ ld -shared sum_list.o -o
-- when you run it use -i, like so:
-- $ luajit -i asmrepl.lua
-- this dumps you into LuaJIT's REPL after evaluating the file is done
ffi = require("ffi")
struct number_list {
uint64_t number;
struct number_list *next;
uint64_t sum_list(struct number_list*);
asm = ffi.load("sum_list")
function sum_list(list)
dont_gc = {} -- such hacky. many bad idea.
function from (min,max)
function range (min,max,cdr)
if max<min then return cdr end
dont_gc[max] ="struct number_list",{max, cdr})
return range (min,max-1,dont_gc[max])
return range(min,max,nil)
bits 64
; macros for readability
%define list rdi ; by calling convention, argument shows up here
%define accum rax ; accumulator (literally!)
%define sublist rdx
global _sum_list ; OSX likes pre-underscores
global sum_list ; Linux doesn't
mov accum, 0 ; these are the let-bindings!
mov sublist, list
cmp sublist, 0 ; is it NULL?
jnz .else ; if not, goto else
ret; accum (because return value is rax by calling convention)
add accum, [sublist] ; accum+=sublist->number;
mov sublist, [sublist+8] ; sublist=sublist->next;
jmp .sum_sublist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment