Author Topic: Maximum Over Drive System - Lua Assembly Toolkit  (Read 12213 times)

0 Members and 2 Guests are viewing this topic.

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Maximum Over Drive System - Lua Assembly Toolkit
« on: August 19, 2011, 07:58:46 pm »
Welp, I am in need of more posts before I can join the IRC apparently, so I will introduce you all to a project I'm working on :)

Since Lua is currently the fastest programming language for up-to-date NSpire calculators, I thought I would share this relevant project.
This really is for those who want to get the absolute most out of Lua. It's very impractical to write an entire program from Lua assembly, since the compiler does a decent job for most non-time-critical functions.

Lua is actually a semi-compiled language (at least that's what I'm going to call it).
It is compiled to bytecode, and than interpreted dynamically on a virtual machine.

You can execute Lua bytecode directly in Lua with the loadstring() function.

Code: (Example 1) [Select]
loadstring("\27\76\117\97\81\0\1\4\4\4\8\0\56\0\0\0\64\67\58\47\85\115\101\114\115\47\74\97\115\111\110\47\68\111\99\117\109\101\110\116\115\47\80\114\111\103\114\97\109\109\105\110\103\47\76\117\97\47\77\79\68\83\47\116\101\115\116\46\108\117\97\0\13\0\0\0\13\0\0\0\0\0\0\2\1\0\0\0\30\0\128\0\0\0\0\0\0\0\0\0\1\0\0\0\13\0\0\0\0\0\0\0\0\0\0\0")();

Now I'd go over the bytecode format, but you can learn all of that here
This document also includes all of the information on all of the instructions to the Lua VM.

Now, onto the project.
(In retrospect, the name I chose was rather silly, but meh, it applies)

Basically, it includes the following:
  • An assembler for a custom syntax of my own design (feature ridden)
  • Something I would call a Linker, that takes a table of all the relevant information, and compiles it into Lua bytecode
  • Soon to be a disassembler, which will turn a normal Lua function into Lua assembly.

You can find the repository here: https://github.com/NecroBumpist/MODS

So, time for "Hello World!" in Lua assembly
Code: [Select]
local mods = require("MODS.Current.main");
local bytecode = mods.Assemble(
[[
; Hello_World.lasm
.options 0, 0, 0, 2        ; a directive, which sets some properties, the '2' meaning MaxStackSize

getglobal    0,    <'print'>        ; <> denotes a constant, i've yet to bother with a directive for doing constants
loadk        1,    <'Hello World!'> ; The LuaVM is register based, we put the function in one register, then arguments above it
call         0,    2,    1          ; now we call register 0, with (2-1) parameters, accepting (1-1) values back
return       0,    0                ; and now we return
]]);

loadstring(bytecode)() -- turns the bytecode into a function, then we call that function

>Hello World!


So,  I'd like to hear what some of you Lua users who are looking for speed improvements think :)
I plan on updating the repository soon to include:
  • Write some proper documentation
  • Updates to the assembler to include NASM like Macros (debugging right now)

In ten or twenty minutes the repository should be much cleaner ;)

Comments, questions, or concerns ? Or Lua Assembly general thread.
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #1 on: August 19, 2011, 09:44:01 pm »
Wow. That looks pretty cool, I know no Lua at all though but still :P

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #2 on: August 19, 2011, 10:48:41 pm »
Hmm Interesting. I unfortunately no longer am into programming but I'M glad to see more programming tools coming out. For which platforms will this be?

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #3 on: August 19, 2011, 11:26:00 pm »
Looks real nice. So that means another programming language for the NSpire? (And maybe anything that runs Lua?) Cool.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #4 on: August 19, 2011, 11:31:09 pm »
Wow, looks pretty nice :)
Can't wait for it to be finished :D

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #5 on: August 20, 2011, 12:38:32 am »
Hmm Interesting. I unfortunately no longer am into programming but I'M glad to see more programming tools coming out. For which platforms will this be?
I've yet to run the assembler/linker on calc (the code is horendous (no planning), and it would be really slow for large files.

But I'm mainly posting it because you can utilize whatever code you assemble on any platform that runs Lua.
So basically the NSpire series :)

Looks real nice. So that means another programming language for the NSpire? (And maybe anything that runs Lua?) Cool.
I'm not sure I would call it an entire new language, since the code it produces is ran normally in Lua. It's just a more minimalistic version of Lua.
So if you do your planning, you could definetly organize your code in way that would be better than the standard Lua compiler.
How much better, I do not know.

It's similar to the coding in C vs. ASM for x86. The best C compilers are better than average ASM coders.
But then again, the Lua compiler really does no optimzations at all. Writing important functions in Lua ASM might be of benefit, but writing simple or non-time-critical functions would likely be a waste of time.

I plan to do an example with a SHA256 algorithm tomorrow, one in normal Lua, another specifically in Lua ASM.
(I'm going to use an external library, so it won't run on calc, but it will show how Lua ASM can be helpful)
« Last Edit: August 20, 2011, 12:40:41 am by NecroBumpist »
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #6 on: August 20, 2011, 02:08:39 am »
Ah ok thanks for the info.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #7 on: August 20, 2011, 05:20:24 am »
Very interesting!
I have yet to try if it actually works on the nspire, but if it does,that would be wonderfull :)
I've got lots of routines that could use a speed boost :)

Thanks for your work :)

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #8 on: August 20, 2011, 07:55:21 pm »
How exactly would we include this stuff in the actual script, though?

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #9 on: August 20, 2011, 08:16:21 pm »
Update to v2.2
Now includes NASM like macros:

Code: (Example LASM) [Select]
.options 0, 0, 0, 200 ; yay for large stacks!

.macro print(s, msg){
    getglobal   s,  <'print'>
    loadk       s + 1,  msg
    call        s,  2,  1
}

print   0, <"Omnimaga :)">      ; this line will be replaced with everything in the print macro (this is good for inlining functions and stuff)
return  0,  0

>Omnimaga :)

It took forever to get this shit working, mainly because of my terrible code layout :P

Anyway, v2.3 will include cool stuff like automatically tracking the top of the stack so you don't have to use .options in every prototype, and LOTS of linker stage checks. This will prevent (well, help you figure out) the common "invalid bytecode" warnings loadstring() returns if you try to load bad code.

More can be read in the README in the repository.


How exactly would we include this stuff in the actual script, though?

You'd use the loadstring() function to convert the code Assemble() returns into a proper Lua function, which you can easily call and pass arguments to from your normal scripts.

Code: (Use this code to generate bytecode) [Select]
local mods = require("current");
local code = mods.Assemble([[
; put cool Lua ASM code here
]]);

print(code:gsub(".", function(a) return "\\" .. a:byte() end)) -- turn the code into an escape sequence
Code: (Use this code in your production code, or what you put on the calc) [Select]
local code = "" -- copy and paste the output of the previous script here
local func = loadstring(code);
func()
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #10 on: August 21, 2011, 07:29:57 am »
Really nice indeed.

Looking forward to be able to test that project in more complex situations, once I understand how to program in this asm-looking language :P
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #11 on: August 21, 2011, 10:41:59 am »
Really nice indeed.

Looking forward to be able to test that project in more complex situations, once I understand how to program in this asm-looking language :P

Thanks :)
I definetely do more basic documentation on the features of the assembler.

After that, I've included a copy of ANoFrillsIntroToLua51VMInstructions.pdf in the \docs\ folder(the de facto document for learning Lua bytecode, and ultimately Lua assembly)
So that should  be a great reference to all those who want to learn the specifics of a particular instruction.

I'll get to work on explaining how to use everything now  ;D
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #12 on: August 21, 2011, 11:49:02 am »
Err,
Either I'm stupid (and this is not impossible), or I don't understand how you can excecute this function in the Lua TI-Nspire API :

Code: [Select]
require("MODS.Current.main");
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #13 on: August 21, 2011, 11:56:46 am »
Yes, that wouldn't run, TI removed file interactions. The examples are really meant to be ran on a desktop.

I would not advocate using this on your calculator (as I've said before, it's slow),
but I'll steal ocLua's concept so you can, just for fun :)

Edit: in the \bin\ directory there is now a "ocLASM.tns" file. It works exactly like ocLua (many many thanks to ExtendeD :))
Though I still don't recommend actually typing LASM on an NSpire, as it is really tedious.
« Last Edit: August 21, 2011, 01:29:39 pm by NecroBumpist »
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: Maximum Over Drive System - Lua Assembly Toolkit
« Reply #14 on: August 22, 2011, 10:36:51 am »
Interesting project :)
I'd love to see how TI-Nspire programs will benefit from it.
Ndless.me with the finest TI-Nspire programs