Author Topic: Poor man's assembler - help needed  (Read 1475 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Hero Member
  • *****
  • Posts: 872
Poor man's assembler - help needed
« 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:
Code: [Select]
/* 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:
Code: [Select]

#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


Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
Re: Poor man's assembler - help needed
« Reply #1 on: February 11, 2015, 11:54:30 AM »
Hi Roland,

Use macro instead. It takes formal parameters:

Code: OxygenBasic
  1. macro NEXTM()
  2. lodsd
  3. jmp [eax]
  4. end macro
  5.  
  6. macro PUSHRSP(reg)
  7. sub ebp 4
  8. mov [ebp],reg
  9. end macro
  10.  
  11. macro POPRSP(reg)
  12. mov reg,[ebp]
  13. add ebp,4
  14. end macro
  15.  

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
« Last Edit: February 11, 2015, 12:52:30 PM by Charles Pegge »

Arnold

  • Hero Member
  • *****
  • Posts: 872
Re: Poor man's assembler - help needed
« Reply #2 on: February 11, 2015, 01:24:16 PM »
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