Author Topic: BxbAsm  (Read 180161 times)

SteveA

  • Guest
Re: BxbAsm
« Reply #375 on: July 14, 2012, 08:50:52 PM »
Most interesting !

c++ translation:

Code: [Select]
// *********************************************************************
//   Created with Ubx 311.40 (2011/10/19)by James C. Fuller
//   Based on OSX port by Armando Rivera
//   Ported from BCX32 BASIC To C/C++ Translator (V) 5.12
//   BCX (c) 1999 - 2009 by Kevin Diggins
// *********************************************************************
//   Translated for compiling with the g++ Compiler
//   g++ -Os -Wformat -D_FORTIFY_SOURCE=2 -Wno-write-strings $FILE$.c -ldl -o $FILE$
// *********************************************************************
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <setjmp.h>
#include <time.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dlfcn.h>
#include <term.h>


// ***************************************************
// Compiler Defines
// ***************************************************
  #define C_EXPORT extern "C"
  #define C_IMPORT extern "C"

#ifndef stat
  #define lstat stat
#endif
#ifndef _fcloseall
  #define _fcloseall _fcloseall
#endif
#ifndef MAX_PATH
  #define MAX_PATH 2048
#endif
#ifndef CALLBACK
  #define CALLBACK
#endif
  typedef unsigned int HINSTANCE;
  typedef const char CCHAR;
  typedef char* PCHAR;
  typedef unsigned char byte;
  typedef unsigned int  UINT;
  typedef unsigned char UCHAR;
  typedef unsigned long ULONG;
  typedef unsigned long DWORD;

#ifndef TRUE
  #define TRUE 1
#endif

#ifndef FALSE
  #define FALSE 0
#endif

#define BOOL bool

// *************************************************
//                System Variables
// *************************************************

char    ESC [2]={27,0};  // Escape

// *************************************************
//            User Global Variables
// *************************************************

static PCHAR   *G_argv;
static int     G_argc;
static char    a[2048];



// *************************************************
//               Standard Macros
// *************************************************



// *************************************************
//               Standard Prototypes
// *************************************************

void    cls(void);
char*   BCX_TmpStr(size_t);
char*   inkey (void);
int     _getch_(int);


// *************************************************
//            User Global Initialized Arrays
// *************************************************


// *************************************************
//                  Main Program
// *************************************************

int main(int argc, char *argv[])
{
  G_argc = argc;
  G_argv = argv;
  cls();
  while(a[0]==0)
    {
      strcpy(a,inkey());
    }
  printf("%s\n",a);
  return 0;   //  End of main program
  }

// *************************************************
//                 Runtime Functions
// *************************************************

char *BCX_TmpStr (size_t Bites)
{
  static int   StrCnt;
  static char *StrFunc[2048];
  StrCnt=(StrCnt + 1) & 2047;
  if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
    #if defined BCX_MAX_VAR_SIZE
  if(Bites*sizeof(char)>BCX_MAX_VAR_SIZE)
  {
  printf("Buffer Overflow caught in BCX_TmpStr - requested space of %d EXCEEDS %d\n",(int)(Bites*sizeof(char)),BCX_MAX_VAR_SIZE);
  abort();
  }
  #endif
  return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
}


char* inkey(void)
{
  char *strtmp = BCX_TmpStr(2);
  strtmp[0] = _getch_(FALSE);
  if(strtmp[0] == -1) strtmp[0] = 0;
  return strtmp;
}


int _getch_(int waitkey)
{
  struct termios initial_settings, new_settings;
  unsigned char ch;

  tcgetattr(0,&initial_settings);
  new_settings = initial_settings;
  new_settings.c_lflag &= ~ICANON;
  new_settings.c_lflag &= ~ECHO;
  new_settings.c_lflag &= ~ISIG;
  new_settings.c_cc[VMIN] = waitkey;
  new_settings.c_cc[VTIME] = 0;
  tcsetattr(0, TCSANOW, &new_settings);

    //read(0,&ch,1);
    ch = getchar();
  tcsetattr(0, TCSANOW, &initial_settings);
  return ch;
}


void cls (void)
{
  printf("%s%s%s%s",ESC,"[2J",ESC,"[H");
}


Thanks James.
That explains a lot.

Steve

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: BxbAsm
« Reply #376 on: July 14, 2012, 09:53:10 PM »
Code: [Select]
void cls (void)
{
  printf("%s%s%s%s",ESC,"[2J",ESC,"[H");
}

@James - I thought your version of BCX was using ncurses for console screen control. I had to take a similar approach with direct ANSI ESC codes to get it to work on both Android and Ubuntu Linux. ( SB example )

Code: [Select]
SUB CLS
  PRINT "\033[2J\033[1;1H"
END SUB

I noticed in your example that you didn't home the cursor with x/y values. I'll give that a try.

 
« Last Edit: July 14, 2012, 09:58:40 PM by JRS »

jcfuller

  • Guest
Re: BxbAsm
« Reply #377 on: July 15, 2012, 03:08:41 AM »

Just to give credit where it's due this is Armando's code from MBC -> UBX.

James

jcfuller

  • Guest
Re: BxbAsm
« Reply #378 on: July 15, 2012, 03:11:35 AM »
Note that clearing the screen on linux with the ubx code is not the same as Windows. It just scrolls the existing text off screen where windows does a complete clear.

James

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: BxbAsm
« Reply #379 on: July 15, 2012, 08:55:47 AM »
Note that clearing the screen on linux with the ubx code is not the same as Windows. It just scrolls the existing text off screen where windows does a complete clear.

James


That's pretty much how it works with the "clear" terminal command also.  If you clear the screen, and then scroll the window up you'll see what I mean.

BTW, functions in term.h actually utilize curses.  That's why you need to link -lncurses.

@Steve:  Can you try this for your ClearScreen()?
Code: [Select]
void ClearScreen()      /* this function clears the console screen. */
{   
    if (!cur_term) {
        int result;
        setupterm( NULL, STDOUT_FILENO, &result );
        if (result <= 0) return;
    }
    putp(tigetstr("clear"));
    fflush(stdout);
}

The "cur_term" variable is set when you call setupterm.  What I believe was missing was flushing your buffer via "fflush", so the "putp" call wasn't actually completed until "getchar" was called.

A.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: BxbAsm
« Reply #380 on: July 15, 2012, 09:47:34 AM »
Interesting tidbit:  Busybox uses the terminal codes like mbc/ubx do:

Code: [Select]
int clear_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int clear_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
{
/* home; clear to the end of screen */
return full_write1_str("\033[H""\033[J") != 6;
}

"full_write1_str" is a wrapper around the write() api call.

There is a wealth of good stuff contained in the Busybox source.  I recommend downloading it for study...

A.

SteveA

  • Guest
Re: BxbAsm
« Reply #381 on: July 15, 2012, 02:59:55 PM »
Steve:  Can you try this for your ClearScreen()?

Code: [Select]
void ClearScreen()      /* this function clears the console screen. */
{   
    if (!cur_term) {
        int result;
        setupterm( NULL, STDOUT_FILENO, &result );
        if (result <= 0) return;
    }
    putp(tigetstr("clear"));
    fflush(stdout);
}

The "cur_term" variable is set when you call setupterm. 
What I believe was missing was flushing your buffer via "fflush", so the "putp" call wasn't actually completed until "getchar" was called.

Excellent !
This works perfectly.
Flushing the buffer makes a lot of sense.

Thanks for the help guys.
I'm way, still at the early learning stages of linux.
Too much to learn in a very short time.

Steve

SteveA

  • Guest
Re: BxbAsm
« Reply #382 on: July 17, 2012, 06:33:03 PM »
Here are some NCurses tutorials I found.
Save them locally and open them in your browser.

The .tar.gz file has some code samples.

I'm finding NCurses to be really quite interesting.

Steve

SteveA

  • Guest
Re: BxbAsm
« Reply #383 on: July 21, 2012, 09:28:05 AM »
James,
I'm beginning to understand what you were referring to, when you suggested making a choice between term and ncurses.
From what I can make out so far, ncurses just about has to be treated as an API unto itself.
Unlike windows, which only has one main API, (the win32 API), with the addition of the C Library, Linux has numerous (what can be called) API's.

It is unfortunate, I am learning, that when using the nucurses API, not even a simple printf statement can be used without using the ncurses version of the function.
If you use it, you have to use it almost exclusively.
Not all CLib functions have their compliments in ncurses, just enough to be annoying.

steve

jcfuller

  • Guest
Re: BxbAsm
« Reply #384 on: August 07, 2012, 01:08:49 PM »
Steve,
  Just read the linux inc file discussion on sourceforge; I would still go with invoke and flesh out your protos as you go along.
You still need to know the parameters to write your push/call so it's no biggie to write a proto.

James
 

SteveA

  • Guest
Re: BxbAsm
« Reply #385 on: August 07, 2012, 03:00:07 PM »
  Just read the linux inc file discussion on sourceforge;
I would still go with invoke and flesh out your protos as you go along.
You still need to know the parameters to write your push/call so it's no biggie to write a proto.

Yeh,
funny thing, after my exchange with japheth, I came to that same conclusion.
I still need the parameter. I prefer to use invoke and I still need the protos anyway.

I'm working on constructing something (h2inc like) that will at least get me the protos and defines.

I wonder, just how much of the variables declarations I really need.
Like the #define's, typedef's and struct's ?

Searching around, it looks like others (besides japheth) have tried to craft something similar (to h2inc/h2incx) in the past, without ever completeing the task.

Anyway, I'm still working on it. I haven't stalled or anything yet.
Steve

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: BxbAsm
« Reply #386 on: September 25, 2012, 08:30:33 PM »
Things have been pretty quite here on the developer forum. Any news to share Steve or James?


SteveA

  • Guest
Re: BxbAsm
« Reply #387 on: September 29, 2012, 01:15:48 PM »
Things have been pretty quite here on the developer forum. Any news to share Steve or James?

Hey guys,...
thanks for the kick in the pants John...,
sorry, I've been taking a little time to work on a few things and have not been keeping up with regular posts.

I actually have done quite a bit of work of trying to understand the Linux/GCC headers.
I've written a little program (using Bxbasic) that ports the headers to an ".inc" type of format.
Altho' it's not a complete port, as it makes no attempt to actually translate the conditional statements contained in the headers.
I find that to be way too complicated, at this time.
It simply performs an extraction of "#define" statements and function prototypes and kind of dumps them into an ".inc" file for use with the assembler.
I haven't tested the results yet. I'm just about to.
One main problem I've encountered is that nearly each header file has it's own set of rules, you might say.
Many have very little in common.

Once I get them tested and working I will provide more information.










Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: BxbAsm
« Reply #388 on: September 29, 2012, 05:27:12 PM »
Thanks Steve for the update. It's good to see you still interested in your project. James is BCXing so you are going to have to give him a ping.

SteveA

  • Guest
Re: BxbAsm
« Reply #389 on: October 05, 2012, 08:57:36 AM »
Attached is what I have accomplished so far, in my effort to construct a program that will produce a list of function prototypes for the Linux GCC LIBC headers.
It is not complete, but it's a good start.
Here is my preface to the program:


' h2incz.bas [Copyright (C) 2012, sarbayo]
' using:  Bxbasic v20.3.1   --   Win32 executable
'
' The original h2inc program is designed to translate GCC C header files
' into ".inc" include files, more or less usable by Masm and other
' assemblers.
' This program functions similar to h2inc, with a number of exception.
'
' Presently, the primary goal of this program is to extract the #define
' and extern statements and produce clean and uncluttered variable
' definitions and function prototypes and not to attempt to parse the
' conditional #if statements, typically found in header files.
'
' As stated, conditional statements are ignored, entirely.
' Instead, each line is parsed and tested for a valid and understandable
' variable definition that can be translated into an assembly language
' equate statement.
'
' How to run this program:
' ========================
' 1) First, by default, this program is designed to read the contents of a
' text file, (named header.txt), in the working directory, which contains
' a list of the header file names to be translated.
' The header.txt file must be formated in a specific way.
' The first line of the file must contain the number of headers to be
' processed. The second and subsequent lines must contain the names of the
' header files, without the ".h" extension and must appear between quotes,
' like so:
'                          __________
'                         |10
'                         |"a.out"
'                         |"aio"
'                         |"alloca"
'                         |"ar"
'                         |"argp"
'                         |"argz"
'                         |...etc
'
'
' 2) Copies of the headers to be translated must be placed in a sub-
' directory of the current working directory, named "Linux-Headers".
' As an example, assume the working directory is named h2incz somewhere on
' the current drive: i.e.:
'      c:\..\h2incz
'
' the files required to reside in the working directory would be:
'  bxbasic.exe,
'  h2incz.bas and
'  headers.txt.
'
' 3) Next, create a subdirectory named: "Linux-Headers"
'      c:\..\h2incz\Linux-Headers
'
' Store copies of the gcc headers to be processed into that subdirectory.
'
' 4) Create a second subdirectory and name it: "Linux-Inc".
' This is where the translated files will be stored.
'      c:\..\h2incz\Linux-Inc
'
' 5) To run, from the command-line, type:
'      bxbasic h2incz
'
'
' Anywhere from 1 to 100 (or more) header files may be processed this way.
'
' The resulting output files will be usable, but, not in their entirety in
' the outputted format. Many items, will be commented-out, with the
' exception of function prototypes. Includes, defines and possibly other
' things will be commented-out. Since the main objective of this program
' is to provide usable function prototypes, that is where the main focus of
' the output files will be placed.
'
' Those items which are commented-out can be un-commented and tested
' individually. The reason for this is that many define statements are
' subject to conditional statements and in some cases do not apply in a
' given context.
'
' It should be understood, (by the user), that GCC (as do most C compilers)
' uses a preprocessor to read, parse and interpret the C header files. This
' small program is in no way a preprecessor and falls far short of
' interpreting the meaning and context of the statements contained in the
' header files. This simple program simply strips-out and dumps what
' information may be useful to the Assembly Language programmer.
'
' **NOTE:
' The prototypes and equate statements generated are purely theoretical.
' Most of the files generated by this program are untested. There are just
' too many header files contained in the GCC LIBC to be able to test them
' all in the time I have available.
' Should you find errors (and I am sure there will be several), in the
' output files, please let me know, so that I can make corrections to the
' program.
' Thanks, Steve.
'