flat assembler
Message board for the users of flat assembler.

Index > Windows > .:: FASm & LUA ::.

Author
Thread Post new topic Reply to topic
vadana



Joined: 13 Jul 2003
Posts: 8
Location: Belarus
vadana 12 Apr 2004, 12:46
Who tried dynamically to connect to LUA libriary.
Post 12 Apr 2004, 12:46
View user's profile Send private message Reply with quote
guignol



Joined: 06 Dec 2008
Posts: 763
guignol 11 Mar 2019, 10:33
Who's LUA?
Post 11 Mar 2019, 10:33
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12937
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Mar 2019, 16:58
Post 11 Mar 2019, 16:58
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 12 Mar 2019, 18:26
Do you want to mix lua code with assembly libraries?
Do you want to offer an interpreted language for an existing assembly project?
Do you intend to replace inner loops from an existing lua code in assembly?

Interfacing with lua in assembly is easier than in C. Using the lua dll from fasm is a simple operation but the question is what are you trying to achieve?
Post 12 Mar 2019, 18:26
View user's profile Send private message Yahoo Messenger Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 727
Ali.Z 13 Mar 2019, 17:05
mixing bytecode with assembly? seriously i see it as a bad thing. (but why mixing?)

_________________
Asm For Wise Humans
Post 13 Mar 2019, 17:05
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 15 Apr 2019, 00:09
I would have preferred to see this post evolving. Lua and assembly are a match made in heaven.
Both at opposite sides of the programming spectrum but they can play along very well.
The fact that lua is designed to be easily interfaced with C means that we will get a lot more power from assembly.
Lua is a really well crafted piece of software and it will give you many things for free for your own application.
Here's a teaser: a functionally equivalent fasm version of the example found at http://lua-users.org/wiki/SimpleLuaApiExample
You would only need the lua53.dll library from https://www.lua.org/download.html
Code:
format PE CONSOLE
entry start

include 'win32a.inc'

LUA_MULTRET = -1

section '.text' code readable executable

  start:
    invoke  GetModuleHandle,0
    mov     [hInstance],eax
    invoke  GetStdHandle,STD_OUTPUT_HANDLE
    mov     [hStdout],eax

; Get a new lua context: lua allocates the memory and return a ponter in eax
; This context is the state for the lua thread in execution. We don't need to
; mess with it. Simply pass it as the first argument to any lua function. 
    invoke  luaL_newstate
; Store lua state for later reference (api functions don't change esi)
    mov     esi,eax

; Load lua standard libraries into the context
    invoke  luaL_openlibs,esi

; Load the script file
    invoke  luaL_loadfilex,esi,script_name,NULL
    cmp     eax,0
    je      .script_loaded
    invoke  lua_tolstring,esi,-1,0
    invoke  wsprintf,buffer,script_error,eax
    invoke  WriteFile,[hStdout],buffer,eax,io_bytes,NULL
    mov     eax,1
    jmp     exit

  .script_loaded:
    invoke  lua_createtable,esi,0,0
    mov     edi,1
  .start_loop:
    push    0 edi
    fild    qword [esp]
    fstp    qword [esp]
    invoke  lua_pushnumber,esi
    mov     eax,edi
    add     eax,edi
    push    0 eax
    fild    qword [esp]
    fstp    qword [esp]
    invoke  lua_pushnumber,esi
    invoke  lua_rawset,esi,-3
    inc     edi
    cmp     edi,5
    jle     .start_loop
    invoke  lua_setglobal,esi,table_name

; Run the script into the context
    invoke  lua_pcallk,esi,0,LUA_MULTRET,0,0,0
    cmp     eax,0
    je      .show_result
    invoke  lua_tolstring,esi,-1,io_bytes
    invoke  wsprintf,buffer,run_error,eax
    invoke  WriteFile,[hStdout],buffer,eax,io_bytes,NULL
    mov     eax,1
    jmp     exit

  .show_result:
    invoke  lua_tonumberx,esi,-1,0
    fist    dword [esp]  
    invoke  wsprintf,buffer,return_message
    invoke  WriteFile,[hStdout],buffer,eax,io_bytes,NULL
    invoke  lua_settop,esi,-2
    invoke  lua_close,esi
    mov     eax,0
  exit:
    invoke  ExitProcess,eax

section '.bss' readable writeable

  hInstance dd ?  ; instance handle
  hStdout   dd ?  ; standard output handle
  io_bytes  dd ?  ; total bytes written
  buffer    rb 256

section '.data' readable

  script_name TCHAR 'script.lua',0
  script_error TCHAR "Couldn't load file: %s",13,10,0
  run_error TCHAR "Failed to run script: %s",13,10,0
  return_message TCHAR "Script returned %i",13,10,0
  program_name TCHAR 'lua test',0
  table_name TCHAR 'foo',0

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
    lua53,'LUA53.DLL',\
    user,'USER32.DLL'

  import kernel,\
    GetModuleHandle,'GetModuleHandleA',\
    ExitProcess,'ExitProcess',\
    GetStdHandle,'GetStdHandle',\
    WriteFile,'WriteFile'

  import lua53,\
    luaL_error,'luaL_error',\
    luaL_loadfilex,'luaL_loadfilex',\
    luaL_newstate,'luaL_newstate',\
    luaL_openlibs,'luaL_openlibs',\
    lua_close,'lua_close',\
    lua_createtable,'lua_createtable',\
    lua_pcallk,'lua_pcallk',\
    lua_pushnumber,'lua_pushnumber',\
    lua_rawset,'lua_rawset',\
    lua_setglobal,'lua_setglobal',\
    lua_settop,'lua_settop',\
    lua_tolstring,'lua_tolstring',\
    lua_tonumberx,'lua_tonumberx'

  import user,\
    wsprintf,'wsprintfA'    
Post 15 Apr 2019, 00:09
View user's profile Send private message Yahoo Messenger Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12937
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 15 Apr 2019, 18:07
http://www.lua.org/manual/5.3/luac.html

luac output to .out, perhaps .dll in Windows, this make us more powerful.
Post 15 Apr 2019, 18:07
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2545
Furs 17 Apr 2019, 11:51
I dislike Lua's bytecode, it's bloated as hell.
Post 17 Apr 2019, 11:51
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 18 Apr 2019, 05:31
I'm no Lua coder, but IIRC, the bytecode output is optional. By that I mean that the main "lua" can be built without the compiler frontend and only include the interpreter. So you can have separate bytecode (and separate "luac"), but I suppose it's rarely recommended.

BTW, the Lua book is heavily revamped in its fourth edition (5.3), but the older first edition (5.0) is available for free online. I do recommend checking that out.
Post 18 Apr 2019, 05:31
View user's profile Send private message Visit poster's website Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 22 Apr 2019, 21:20
Furs wrote:
I dislike Lua's bytecode, it's bloated as hell.


You don't need to mess with lua bytecode. Everything is handled by the shared library. Just keep the two realms separated (assembly and lua scripts).

Lua is several times faster than any other general purpose interpreted language, specially for data related tasks.

An assembly application could use Lua in the same way that Redis use it, for example. But there is no need to statically link it. Just call the shared library functions.
Post 22 Apr 2019, 21:20
View user's profile Send private message Yahoo Messenger Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 22 Apr 2019, 21:27
rugxulo wrote:
I'm no Lua coder, but IIRC, the bytecode output is optional. By that I mean that the main "lua" can be built without the compiler frontend and only include the interpreter.


You are right. The shared library contains the "engine", designed to be embedded by default. It is small, thread safe, and the full language is at your disposition. You can create the variables in assembly, apply a lua script over them and then process the result back in assembly. Or make functions in assembly to be called by any lua script. It is truly versatile and powerful.
Post 22 Apr 2019, 21:27
View user's profile Send private message Yahoo Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.