Author Topic: Embedding Lua Scripts in NASM (demo)  (Read 3821 times)

Offline Mathi

  • Jr. Member
  • *
  • Posts: 79
  • Country: in
    • Win32NASM
Embedding Lua Scripts in NASM (demo)
« on: January 16, 2012, 05:38:48 AM »
The following program executes Lua Statements/script files from within NASM.

This program is a direct translation from C Version.
Original page (or the one I referred) URL is broken now. Still wanted to mention that.

http://www.heavycoder.com/tutorials/lua_embed.php     **broken

Code: [Select]
;;Assemble with:
;;nasm -fobj HelloLua.asm
;;Link with:
;;alink -c -oPE -subsys con HelloLua.obj

%include "win32n.inc"
;;LUA include files
%include "lua.inc"
%include "lauxlib.inc"
%include "lualib.inc"

;;IMPORTS
extern luaL_newstate
import luaL_newstate lua51.dll

extern luaL_loadstring
import luaL_loadstring lua51.dll

extern lua_pcall
import lua_pcall lua51.dll

extern luaL_loadfile
import luaL_loadfile lua51.dll

extern luaL_openlibs
import luaL_openlibs lua51.dll

extern lua_close
import lua_close lua51.dll

extern GetStdHandle
import GetStdHandle kernel32.dll
extern WriteFile
import WriteFile kernel32.dll
extern ExitProcess
import ExitProcess kernel32.dll

global ..start

segment .data USE32

message:
    db      'Hello, World - Inside NASM', 10
message_end:

funcname1 db "base",0
scriptfilename db "script.lua",0

scriptstatement db "print 'HelloWorld- Inside LUA'",0


segment .bss USE32

byteswritten resd 1
lptr resd 1
segment .code USE32

..start:
   
    ; hStdOut = GetstdHandle( STD_OUTPUT_HANDLE)
    push    dword STD_OUTPUT_HANDLE
    call    [GetStdHandle]
    mov     ebx, eax   

    ; WriteFile( hstdOut, message, length(message), &bytes, 0);
    push    dword 0
    push    dword byteswritten
    push    (message_end - message)
    push    message
    push    ebx
    call    [WriteFile]

    call [luaL_newstate]
    mov [lptr], eax
   
    push dword [lptr]
    call [luaL_openlibs]
    add esp,4
     
    push dword scriptstatement
    push dword [lptr]
    call [luaL_loadstring]
    add esp,8
   
    push dword 0
    push dword LUA_MULTRET
    push dword 0
    push dword [lptr]
    call [lua_pcall]
    add esp,16

    push dword scriptfilename
    push dword [lptr]
    call [luaL_loadfile]
    add esp,8
   
    push dword 0
    push dword LUA_MULTRET
    push dword 0
    push dword [lptr]
    call [lua_pcall]
    add esp,16

    push dword [lptr]
    call [lua_close]
    add esp,4

   
    ; ExitProcess(0)
    push    dword 0
    call    [ExitProcess]



This is the C version

Code: [Select]
#include <stdlib.h>
#include <stdio.h>

/* Include the Lua API header files. */
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main(void)
{


/* Declare a Lua State, open the Lua State and load the libraries (see above). */
lua_State *l;
l = lua_open();

luaL_openlibs(l);

/* print some text directly from C. */
printf("This line in directly from C\n\n");

luaL_dostring(l,"print 'Helloworld inside LUA '");
/* In the lines below, I load and run the Lua code contained in the file */
/* "script.lua". */
luaL_dofile(l, "script.lua");
printf("\nBack to C again\n\n");

/* Remember to destroy the Lua State */
lua_close(l);

return 0;
}


The zip file has include files which are converted from .h files from lua package. (not complete though).
It also has a Nagoa Version(HelloLua_nagoa.asm) which uses the macros in nagoa+.inc)

<will link the zip file shortly>
edit - fbk:
http://home.myfairpoint.net/fbkotler/luanasm.zip

(currently unable to attach files here :( )

Thanks,
Mathi.
« Last Edit: January 16, 2012, 10:50:13 AM by Frank Kotler »