tinypy: first working module written on FASM.

Exports: kolibri_dbg.debug_print(message).


git-svn-id: svn://kolibrios.org@2048 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
jaeger 2011-08-14 16:47:47 +00:00
parent 2a927db105
commit 50431b95fb
4 changed files with 72 additions and 46 deletions

View File

@ -1,7 +1,8 @@
export MENUETDEV=../../libraries/menuetlibc export MENUETDEV=../../libraries/menuetlibc
OUTFILE = tpmain OUTFILE = tpmain
OBJS = tpmain.o kolibri_init.o kolibri_fs.o kolibri_gui.o OBJS = tpmain.o kolibri_init.o kolibri_fs.o kolibri_gui.o kolibri_dbg.o
CFLAGS = -I.
include $(MENUETDEV)/makefiles/Makefile_for_program include $(MENUETDEV)/makefiles/Makefile_for_program
#testmod.o: fasm_modules/testmod.s kolibri_dbg.o: fasm_modules/kolibri_dbg.s
# fasm fasm_modules/testmod.s fasm fasm_modules/kolibri_dbg.s
# cp fasm_modules/testmod.o . cp fasm_modules/kolibri_dbg.o .

View File

@ -1,3 +1,9 @@
; TinyPy module
; Name: kolibri_dbg
;
; Exports:
; debug_print(msg) - prints a message to debug board.
format ELF format ELF
use32 use32
include 'proc32.inc' include 'proc32.inc'
@ -10,24 +16,22 @@ extrn tp_get
extrn tp_None extrn tp_None
extrn tp_fnc extrn tp_fnc
public testmod_init public kolibri_dbg_init
public getsize
;Module name ; Module name
modname db "testmod" modname db "kolibri_dbg"
modnamelen = $-modname modnamelen = $-modname
; Exported function name
debug_print_name db "debug_print" debug_print_name db "debug_print"
debug_print_namelen = $-debug_print_name debug_print_namelen = $-debug_print_name
; Export dictionary for module
testmod_dict rb sizeof.tp_obj kolibri_dbg_dict rb sizeof.tp_obj
; tp_obj of exported function
debug_print_obj rb sizeof.tp_obj debug_print_obj rb sizeof.tp_obj
tmp_tp_obj rb sizeof.tp_obj
getsize: ; Function debug_print(tp_vm *tp)
mov eax, tp_vm.params ; return nothing
ret
;void debug_print(tp_vm *tp)
debug_print: debug_print:
push ebp push ebp
mov ebp, esp mov ebp, esp
@ -36,34 +40,44 @@ debug_print:
push ebx push ebx
push ecx push ecx
push edx push edx
;Reserve space for tp_obj variable in stack sub esp, sizeof.tp_obj; Reserve tp_obj tmp
sub esp, sizeof.tp_obj mov edx, esp
push edx ;Store &tmp
; Obtain tp_string parameter ; Obtain tp_string parameter
push_tp_obj tp_None ; tp_get(&tmp, tp, *(tp_obj *)(tp_vm->params), tp_None)
mov eax, dword[ebp+8] push_tp_none
mov eax, dword[ebp+12] ;4 for return address, 4 for result pointer; 4 for tp_vm
add eax, tp_vm.params add eax, tp_vm.params
push_tp_obj_at_reg eax push_tp_obj_at_reg eax
push dword[ebp+8] push dword[ebp+12]
push tmp_tp_obj;esp+(sizeof.tp_obj*2+4) push edx ;ebx contains address of reserved tp_obj variable
call tp_get call tp_get
;Restore stack ;Restore stack
add esp, 56;sizeof.tp_obj*3+4;8? add esp, sizeof.tp_obj*2+4;2 tp_obj's and 2 pointers in parameters minus 1 pointer to result (cleared inside tp_get)
;Leave if parameter is not a string. tp_raise() should be called here. ;Leave if parameter is not a string. tp_raise() should be called here.
;cmp dword[esp], TP_STRING pop edx; edx = &tmp
;jne .exit cmp dword[edx], TP_STRING ; Check that function returned a TP_STRING
;mov ecx, dword[esp+12] jne .exit
;mov edx, dword[esp+8] mov ecx, dword[edx+tp_string_.len] ; String length
;.cont: mov edx, dword[edx+tp_string_.val] ;
; Print message. mov eax, 63
; mov eax, 63 mov ebx, 1
; mov ebx, 1 .cont:
; push ecx ; Print message.
; mov cl, byte[edx] push ecx ; Store ecx to use it in inner loop
; inc edx mov cl, byte[edx]
; int 40h inc edx
; pop ecx int 40h
; loop .cont pop ecx ; Get ecx back
;.exit: loop .cont
.exit:
add esp, sizeof.tp_obj ; Release tp_obj reserved in stack.
; Returning tp_None
mov eax, dword[ebp+8]
mov dword[eax], 0
; Restore registers
pop edx pop edx
pop ecx pop ecx
pop ebx pop ebx
@ -72,22 +86,22 @@ debug_print:
pop ebp pop ebp
ret ret
;void testmod_init(tp_vm *tp); ;void kolibri_dbg_init(tp_vm *tp);
testmod_init: kolibri_dbg_init:
push ebp push ebp
mov ebp, esp mov ebp, esp
;Save registers ;Save registers
push eax push eax
push ebx push ebx
push ecx push ecx
; Create module dictionary and store its address in testmod_str ; Create module dictionary and store its address in kolibri_dbg_str
mov eax, dword [ebp + 8] mov eax, dword [ebp + 8]
push eax push eax
push testmod_dict push kolibri_dbg_dict
call tp_dict call tp_dict
add esp, 4 ;Clear stack add esp, 4 ;Clear stack
; Push tp_obj structure pointed by testmod_dict ; Push tp_obj structure pointed by kolibri_dbg_dict
push_tp_obj testmod_dict push_tp_obj kolibri_dbg_dict
; Push modname as a tp_obj object ; Push modname as a tp_obj object
push modnamelen; len push modnamelen; len
push modname ; val push modname ; val
@ -121,7 +135,7 @@ loop .push_tpobj1
push 0 push 0
push TP_STRING push TP_STRING
;Pushing module dictionary tp_obj ;Pushing module dictionary tp_obj
push_tp_obj testmod_dict push_tp_obj kolibri_dbg_dict
;Pushing tp_vm ;Pushing tp_vm
push dword[ebp+8] push dword[ebp+8]
call tp_set call tp_set

View File

@ -152,6 +152,15 @@ struct tp_vm
steps rd 1 steps rd 1
ends ends
macro push_tp_none
{
local .push_more
mov ecx, 4
.push_more:
push 0
loop .push_more
}
macro push_tp_obj obj macro push_tp_obj obj
{ {
local .push_more local .push_more

View File

@ -2,7 +2,8 @@
/* INCLUDE */ /* INCLUDE */
const char header[]="TinyPy for kolibriOS"; const char header[]="TinyPy for kolibriOS";
const int argc = 2; const int argc = 2;
extern _stdcall void testmod_init(tp_vm *tp); extern _stdcall void kolibri_dbg_init(tp_vm *tp);
void main(void) { void main(void) {
char *argv[2]={"tpmain", "test.py"}; char *argv[2]={"tpmain", "test.py"};
@ -15,6 +16,7 @@ void main(void) {
argv[1][strlen(argv[1]) - 1] = '\0'; argv[1][strlen(argv[1]) - 1] = '\0';
con_printf("Running file %s\n", argv[1]); con_printf("Running file %s\n", argv[1]);
tp_vm *tp = tp_init(argc, argv); tp_vm *tp = tp_init(argc, argv);
kolibri_dbg_init(tp);
kolibri_init(tp); kolibri_init(tp);
/* INIT */ /* INIT */
tp_call(tp,"py2bc","tinypy",tp_None); tp_call(tp,"py2bc","tinypy",tp_None);