Stackmaschine in C
Übersicht

FlorianBetreff: Stackmaschine in C |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
In Aufbau
DLL Stackmaschine [Userlib *.decls] Code: [AUSKLAPPEN] .lib "VM.dll" RUN%():"RUN" OP_NAME$(Nr%):"OPNAME" Test code mit seltsamen Fehler Code: [AUSKLAPPEN] Print OPNAME$(2) Print "Test" WaitKey [Ausgabe] Code: [AUSKLAPPEN] swap Test swap Test Code: [AUSKLAPPEN] +----------------------------------------------------------------------------------------------------------+ | | | V I R T U E L L E S T A C K M A S C H I N E | | | +----------------------------------------------------------------------------------------------------------+ |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++| +----------------------------------------------------------------------------------------------------------+ | | | B E F E H L E | | | +--------------------+--------------------+-----------------------+------------------+---------------------+ | | | | | | | Transportbefehle | Sprungbefehle | Arithmetische Befehle | Logische Befehle | Ein-/Ausgabebefehle | | | | | | | +--------+-----------+--------+-----------+-----------------------+------------------+--------+------------+ | | | | | | | | | | Befehl | Parameter | Befehl | Parameter | Befehl | Befehl | Befehl | Parameter | | | | | | | | | | +--------+-----------+--------+-----------+-----------------------+------------------+--------+------------+ | | | | | | | | | | AStor | addr | Jmp | addr | Mul | And | Int | Nr | | ALoad | addr | Jle | addr | Div | Not | | | | Stor | | Call | addr | Sub | Or | | | | Load | | Ret | | Add | Xor | | | | Push | zahl | | | Incr | | | | | Pop | | | | Decr | | | | | Dbl | | | | Neg | | | | | swap | | | | | | | | | | | | | | | | | +--------+-----------+--------+-----------+-----------------------+------------------+--------+------------+ Code: [AUSKLAPPEN] +----------+---------------+------------------------------------------------------------------------+ | | | | | Befehl | Parameter | Beschreibung | | | | | +----------+---------------+------------------------------------------------------------------------+ |###################################################################################################| +----------+----------------------------------------------------------------------------------------+ | | | | | Push | Zahl | Die Konstante Zahl wird auf den Stack gelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Pop | | Das oberste Stackelement wird gelöscht. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | dbl | | Das oberste Stackelement wird erneut auf den Stack gelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Swap | | Die beiden obersten Stackelemente werden getauscht. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Mul | | Die beiden obersten Elemente werden vom Stack genommen und das | | | | Ergebnis der Rechenoperation auf dem Stack abgelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Div | | Die beiden obersten Elemente werden vom Stack genommen und das | | | | Ergebnis der Rechenoperation auf dem Stack abgelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Sub | | Die beiden obersten Elemente werden vom Stack genommen und das | | | | Ergebnis der Rechenoperation auf dem Stack abgelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Neg | | Das Vorzeichen des obersten Stackelementes wird umgekehrt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Incr | | Der Wert des oberste Stackelementes wird um eins erhöht. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Decr | | Der Wert des oberste Stackelementes wird um eins erniedrigt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | And | | Die beiden obersten Elemente werden vom Stack genommen und das | | | | Ergebnis der Rechenoperation auf dem Stack abgelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Not | | Das oberste Stackelement wird vom Stack genommen und das Rechenerbinis | | | | auf den Stack abgelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Or | | Die beiden obersten Elemente werden vom Stack genommen und das | | | | Ergebnis der Rechenoperation auf dem Stack abgelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Xor | | Die beiden obersten Elemente werden vom Stack genommen und das | | | | Ergebnis der Rechenoperation auf dem Stack abgelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Call | addr | Durch diesen Befehl wird ein Unterprogramm auf gerufen. Zuerst wird | | | | die Rücksprungsadresse auf den Returnstack abgelegt. Danach wird zur | | | | Adresse addr gesprungen. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Ret | | Durch diesen Befehl wird das Unterprogramm verlassen. | | | | Es wird die Adresse addr vom Returnstack genommen und dort | | | | hingesprungen. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Jmp | addr | Unbedingter Sprung zur Instruktion an Adresse addr. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Jle | addr | Sprung zur Instruktion an Adresse addr, falls das oberste Stackelement | | | | kleiner oder gleich 0 ist. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | AStor | addr | Das oberste Element wird vom Stack genommen und im Speicher unter addr | | | | im Datasegment abgelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | ALoad | addr | Eine Zahl wird von der Speicheradresse addr im Datasegment gelesen und | | | | auf den Stack gelegt. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Stor | | Die Adressse addr wird vom Stack genommen. | | | | Die Zahl wird v om Stack genommen und unter der Adresse addr im | | | | Datasegment gespeichert. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Load | | Dieser Befehl liest eine Zahl unter der Adresee addr im Datasegment | | | | ein und legt diese auf den Stack ab. Die Adresse addr wird datei vom | | | | Stack genommen. | | | | | +----------+---------------+------------------------------------------------------------------------+ | | | | | Int | Nr | Durch diesen Befehl wird eine VM-Funktion mit der Funktions-Nummer Nr | | | | aufgerufen. | | | | | +----------+---------------+------------------------------------------------------------------------+ VM.dev Code: [AUSKLAPPEN] [Project] FileName=VM.dev Name=VM UnitCount=2 Type=3 Ver=1 ObjFiles= Includes= Libs= PrivateResource= ResourceIncludes= MakeIncludes= Compiler=-DBUILDING_DLL=1 CppCompiler=-DBUILDING_DLL=1 Linker=--no-export-all-symbols --add-stdcall-alias IsCpp=0 Icon= ExeOutput= ObjectOutput= OverrideOutput=0 OverrideOutputName= HostApplication= Folders= CommandLine= UseCustomMakefile=0 CustomMakefile= IncludeVersionInfo=0 SupportXPThemes=0 CompilerSet=0 CompilerSettings= [Unit1] FileName=dllmain.c CompileCpp=0 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd= [Unit2] FileName=dll.h CompileCpp=0 Folder= Compile=1 Link=1 Priority=1000 OverrideBuildCmd=0 BuildCmd= [VersionInfo] Major=0 Minor=1 Release=1 Build=1 LanguageID=1033 CharsetID=1252 CompanyName= FileVersion= FileDescription=Developed using the Dev-C++ IDE InternalName= LegalCopyright= LegalTrademarks= OriginalFilename= ProductName= ProductVersion= AutoIncBuildNr=0 dllmain.c Code: [AUSKLAPPEN] /* Replace "dll.h" with the name of your header */ #include "dll.h" #include <windows.h> #include <stdio.h> #include <stdlib.h> #define MaxCode 4096 #define MaxData 4096 #define MaxStack 1024 #define MaxCallStack 1024 #define op_push 1 #define op_pop 2 #define op_swap 3 #define op_dup 4 #define op_add 5 #define op_div 6 #define op_mul 7 #define op_sub 8 #define op_neg 9 #define op_incr 10 #define op_decr 11 #define op_and 12 #define op_not 13 #define op_or 14 #define op_xor 15 #define op_load 16 #define op_stor 17 #define op_aload 18 #define op_astor 19 #define op_call 20 #define op_ret 21 #define op_jmp 22 #define op_jle 23 #define op_int 24 char op_name_push[] ="push"; char op_name_pop[] ="pop"; char op_name_swap[] ="swap"; char op_name_dup[] ="dup"; char op_name_add[] ="add"; char op_name_div[] ="div"; char op_name_mul[] ="mul"; char op_name_sub[] ="sub"; char op_name_neg[] ="neg"; char op_name_incr[] ="incr"; char op_name_decr[] ="decr"; char op_name_and[] ="and"; char op_name_not[] ="not"; char op_name_or[] ="or"; char op_name_xor[] ="xor"; char op_name_load[] ="load"; char op_name_stor[] ="stor"; char op_name_aload[]="aload"; char op_name_astor[]="aload"; char op_name_call[] ="call"; char op_name_ret[] ="ret"; char op_name_jmp[] ="jmp"; char op_name_jle[] ="jle"; char op_name_int[] ="int"; char *op_name[]={"push", "pop", "swap", "dup", "add", "div", "mul", "sub", "neg", "incr", "decr", "and", "not", "or", "xor", "load", "stor", "aload", "astor", "call", "ret", "jmp", "jle", "int"}; char op_liste[]={"push pop swap dup add div mul sub neg incr decr" "and not incr decr and not or xor load stor aload astor" "call ret jmp jle int" }; #define Max_op 24 #define int_end 1 #define int_save 2 #define int_load 3 #define VM_ERROR 0 #define false 0 #define true 1 struct TVM { int IP; int SP; int RP; int CS[MaxCode]; int Operand[MaxCode]; int DS[MaxData]; int SS[MaxStack]; int RS[MaxCallStack]; }; typedef struct TVM TVM; struct TVM VM; void VM_SAVE(void) { FILE *file_nr; int code_lang; int data_lang; int code_zeiger; file_nr = fopen ("vm.txt", "wb"); for(data_lang=MaxData-1; data_lang>=0;data_lang--) { if (VM.DS[data_lang]!=0) { fprintf(file_nr,"datawert: %d\n",VM.DS[data_lang]); break; }; } for(code_lang=MaxCode-1; code_lang>=0;code_lang--) { if (VM.CS[code_lang]!=0) { break; }; } fclose(file_nr); } void VM_LOAD(void) { FILE *file_nr; fopen("VM.TXT","rb"); fclose(file_nr); } DLLIMPORT char *OPNAME(int Zeiger) { return op_name[Zeiger]; } DLLIMPORT void Push(int wert) { } DLLIMPORT int Pop() { } DLLIMPORT int RUN() { VM.CS[0]=op_int; VM.Operand[0]=4; Start: switch (VM.CS[VM.IP]) { case op_push:; VM.SS[VM.SP]=VM.Operand[VM.IP]; VM.SP=VM.SP+1; VM.IP=VM.IP+1; break; case op_pop:; VM.SP=VM.SP-1; VM.IP=VM.IP+1; break; case op_swap:; VM.SS[VM.SP-1]=VM.SS[VM.SP-1]+VM.SS[VM.SP-2]; VM.SS[VM.SP-2]=VM.SS[VM.SP-1]-VM.SS[VM.SP-2]; VM.SS[VM.SP-1]=VM.SS[VM.SP-1]-VM.SS[VM.SP-2]; VM.IP=VM.IP+1; break; case op_add:; VM.SS[VM.SP-2]=VM.SS[VM.SP-1]+VM.SS[VM.SP-2]; VM.SP=VM.SP-1; VM.IP=VM.IP+1; break; case op_div:; VM.SS[VM.SP-2]=VM.SS[VM.SP-1]/VM.SS[VM.SP-2]; VM.SP=VM.SP-1; VM.IP=VM.IP+1; break; case op_mul:; VM.SS[VM.SP-2]=VM.SS[VM.SP-1]*VM.SS[VM.SP-2]; VM.SP=VM.SP-1; VM.IP=VM.IP+1; break; case op_sub:; VM.SS[VM.SP-2]=VM.SS[VM.SP-1]-VM.SS[VM.SP-2]; VM.SP=VM.SP-1; VM.IP=VM.IP+1; break; case op_neg:; VM.SS[VM.SP-1]=-VM.SS[VM.SP-1]; VM.IP=VM.IP+1; break; case op_incr:; VM.SS[VM.SP-1]=VM.SS[VM.SP-1]+1; VM.IP=VM.IP+1; break; case op_decr:; VM.SS[VM.SP-1]=VM.SS[VM.SP-1]-1; VM.IP=VM.IP+1; break; case op_and:; VM.SS[VM.SP-2]=VM.SS[VM.SP-1] & VM.SS[VM.SP-2]; VM.SP=VM.SP-1; VM.IP=VM.IP+1; break; case op_not:; VM.SS[VM.SP]=!VM.SS[VM.SP]; VM.IP=VM.IP+1; break; case op_or:; VM.SS[VM.SP-2]=VM.SS[VM.SP-1] | VM.SS[VM.SP-2]; VM.SP=VM.SP-1; VM.IP=VM.IP+1; break; case op_xor:; VM.SS[VM.SP-2]=VM.SS[VM.SP-1] ^ VM.SS[VM.SP-2]; VM.SP=VM.SP-1; VM.IP=VM.IP+1; break; case op_load:; VM.IP=VM.IP+1; break; case op_stor:; VM.IP=VM.IP+1; break; case op_aload:; VM.IP=VM.IP+1; break; case op_astor:; VM.SS[VM.SP]=VM.Operand[VM.IP]; VM.SP=VM.SP+1; VM.IP=VM.IP+1; break; case op_call:; VM.RS[VM.RP]=VM.IP; VM.RP=VM.RP+1; VM.IP=VM.Operand[VM.IP]; break; case op_ret:; VM.RP=VM.RP-1; VM.IP=VM.RS[VM.RP]; break; case op_jmp:; VM.IP=VM.Operand[VM.IP]; break; case op_jle:; break; case op_int:; switch(VM.Operand[VM.IP]) { case int_save:; VM_SAVE; case int_load:; VM_LOAD; default:; return VM.Operand[VM.IP]; } default:; return VM_ERROR; } goto Start; } BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ , DWORD reason /* Reason this function is being called. */ , LPVOID reserved /* Not used. */ ) { switch (reason) { case DLL_PROCESS_ATTACH: break; case DLL_PROCESS_DETACH: break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; } /* Returns TRUE on success, FALSE on failure */ return TRUE; } Makefile.win Code: [AUSKLAPPEN] # Project: VM # Makefile created by Dev-C++ 4.9.9.2 CPP = g++.exe CC = gcc.exe WINDRES = windres.exe RES = OBJ = dllmain.o $(RES) LINKOBJ = dllmain.o $(RES) LIBS = -L"C:/Programme/Dev-Cpp/lib" --no-export-all-symbols --add-stdcall-alias INCS = -I"C:/Programme/Dev-Cpp/include" CXXINCS = -I"C:/Programme/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Programme/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Programme/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Programme/Dev-Cpp/include/c++/3.4.2" -I"C:/Programme/Dev-Cpp/include" BIN = VM.dll CXXFLAGS = $(CXXINCS) -DBUILDING_DLL=1 CFLAGS = $(INCS) -DBUILDING_DLL=1 RM = rm -f .PHONY: all all-before all-after clean clean-custom all: all-before VM.dll all-after clean: clean-custom ${RM} $(OBJ) $(BIN) DLLWRAP=dllwrap.exe DEFFILE=libVM.def STATICLIB=libVM.a $(BIN): $(LINKOBJ) $(DLLWRAP) --output-def $(DEFFILE) --implib $(STATICLIB) $(LINKOBJ) $(LIBS) -o $(BIN) dllmain.o: dllmain.c $(CC) -c dllmain.c -o dllmain.o $(CFLAGS) |
||
Das große BlitzBasic Community Tutorial
Stackmaschine 2.0 |
- Zuletzt bearbeitet von Florian am Mo, Apr 24, 2006 22:04, insgesamt 5-mal bearbeitet
![]() |
Artemis |
![]() Antworten mit Zitat ![]() |
---|---|---|
Ähm Florian,
das ist hier ein DLL/Userlib-Forum. Das heißt, hier sollten auf jeden Fall vorkompilierte DLLs und *.decls rein. |
||
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group