Oxygen Basic
Programming => Problems & Solutions => Topic started by: Arnold on February 11, 2015, 09:16:59 AM
-
Hello,
I do know almost nothing about assembler, Lisp, Scheme or Forth. My ignorance is definitely embarrasing. By accident I read about JonesForth which is a nice tutorial and source code for Forth in Linux (public domain) written using Gnu assembler instructions. Another program is JonesForthInC for Windows using inline assembler instructions. I would like to do it with OxygenBasic and to learn about asm with o2. Of course I already have a problem with the first three instructions which are macros:
This is the code in JonesForth.s:
/* NEXT macro. */
.macro NEXT
lodsl
jmp *(%eax)
.endm
/* The macro is called NEXT. That's a FORTH-ism. It expands to those two instructions.
/* Macros to deal with the return stack. */
.macro PUSHRSP reg
lea -4(%ebp),%ebp // push reg on to return stack
movl \reg,(%ebp)
.endm
.macro POPRSP reg
mov (%ebp),\reg // pop top of return stack to reg
lea 4(%ebp),%ebp
.endm
/*
And with that we can now talk about the interpreter.
This is the code in JonesForthInC.c for Windows:
#define D DWORD PTR
/* NEXT macro. */
#define NEXT \
_asm lodsd \
_asm jmp D [eax] \
/*
The macro is called NEXT. That's a FORTH-ism. It expands to those two instructions.
* Macros to deal with the return stack. */
/*
sub ebp,4 // push reg on to return stack
mov [ebp],reg
*/
#define PUSHRSP(reg) \
_asm sub ebp,4 \
_asm mov [ebp],reg
/*
mov reg,[ebp]; // pop top of return stack to reg
add ebp,4;
*/
#define POPRSP(reg) \
_asm mov reg,[ebp] \
_asm add ebp,4
/*
And with that we can now talk about the interpreter.
This is my attempt in OxygenBasic:
/* NEXT macro. */
def NXT
lodsw
jmp [eax] ; ???
end def
/*
/* Is this FORTH-ish?
/* Macros to deal with the return stack. */
def PUSHRSP reg
sub ebp,4 // push reg on to return stack
mov [ebp],reg
end def
def POPRSP reg
mov reg,[ebp] // pop top of return stack to reg
add ebp,4
end def
*/
And if I ever manage this we can then talk about the interpreter. */
Is anything of this code correct? And can I later use: PUSHRSP eax? Without a little help I am lost at the beginning.
Roland
-
Hi Roland,
Use macro instead. It takes formal parameters:
macro NEXTM()
lodsd
jmp [eax]
end macro
macro PUSHRSP(reg)
sub ebp 4
mov [ebp],reg
end macro
macro POPRSP(reg)
mov reg,[ebp]
add ebp,4
end macro
No need for dword ptr in o2 assembler. It is the default
PS:
I noticed lodsd was not working correctly. This is a legacy micro-coded instruction one is not supposed to use on today's Pentiums. But I have fixed the problem.
Oxygen Update
http://www.oxygenbasic.org/o2zips/Oxygen.zip
-
Thank you Charles. I was not sure about macro. def and [eax]. For the rest of the tutorial I think I can compare with the demos of \examples\asm32.
Roland