Author Topic: BxbAsm  (Read 121308 times)

SteveA

  • Guest
Re: BxbAsm
« Reply #360 on: June 29, 2012, 06:39:07 am »
I have done nothing with assembler on linux other than jwasm. You might try a google search on nasm or fasm.

I have nasm installed (never used it) from a long time ago, but, I don't see any headers anywhere.
Okay, I'll play around with what I've got.
See what I can make work.

jcfuller

  • Guest
Re: BxbAsm
« Reply #361 on: June 29, 2012, 01:21:16 pm »
Steve,
  What do you need math.inc for anyway?
You have Raymond's fpu  library. Just roll your own math functions :)

James

SteveA

  • Guest
Re: BxbAsm
« Reply #362 on: June 29, 2012, 05:09:10 pm »
 What do you need math.inc for anyway?
You have Raymond's fpu  library. Just roll your own math functions :)

That's not the point.
The crt has a full set of standard math functions, of which bxbasm already uses most.
I've taken MATH.INC and stripped it down of what it looks like it doesn't need, so that it's just function prototypes.
Now, I can't figure how to link to the gcc math library.
What's it called ?
They don't make it obvious.

A very odd thing, when I looked inside: /usr/include/math.h,  it was devoid of any function names.
So, where are they keeping them ?

Steve

edit:
I found it, well..., I should say I found the libraries:
/usr/lib/i386-linux-gnu/libc.a
/usr/lib/i386-linux-gnu/libm.a

Took me forever.
I still haven't found where the function are defined.

« Last Edit: June 29, 2012, 07:29:55 pm by SteveA »

SteveA

  • Guest
Re: BxbAsm
« Reply #363 on: July 11, 2012, 03:14:44 pm »
Well,...
it's been nearly two weeks and I'm still plugging away at jwasm-linux.

With jwasm having not been supplied with at least a minimal set of functioning linux header (.inc) files, I can hardly say that jwasm-linux is "ready for primetime".
I asked, at Andreas' sourceforge site, about headers and nobody seems to have anything or offer any useful suggestions.
I can only assume that no one is really using jwasm on linux, (possibly not even Andreas).
Despite the fact that jwasm does in fact seem to work well on linux, (as far as I can tell),  it doesn't look like anyone is interested in using it.

I tried using Wininc, but, it does not work with linux headers.

I've made small progress, due to the fact that I have to figure out what headers I need and where they and the library files might be on the linux system.
Having to construct one's own headers for linux is a grinding, slow process.
Without some sort of automated process, I can't imagine the task ever being completed.

I have progressed up to Bxb code example #30.
Things seem to be working as expected.

Steve

Offline AIR

  • BASIC Developer
  • Posts: 782
Re: BxbAsm
« Reply #364 on: July 11, 2012, 04:04:28 pm »
I have done nothing with assembler on linux other than jwasm. You might try a google search on nasm or fasm.

I have nasm installed (never used it) from a long time ago, but, I don't see any headers anywhere.
Okay, I'll play around with what I've got.
See what I can make work.


You may want to take a look a the perl scripts at http://rudy.mif.pg.gda.pl/~bogdro/inne/#cconv

They will convert C header files to nasm,fasm, and GAS. Probably will need more tinkering with the converted file(s).

From what I know of nasm, referencing C functions  is usually done like: 

extern  acos
extern  acosf

extern  asin
extern  asinf

extern  atan
extern  atanf

In this case, using libm for the math functions as an example.

To find out what parameters need to be passed, or for info on a given function, just 'man' the function:

Quote
[riveraa@blossom ~] $ man atan

ATAN(3)                  BSD Library Functions Manual                  ATAN(3)

NAME
     atan -- arc tangent function of one variable

SYNOPSIS
     #include <math.h>

     double
     atan(double x);

     long double
     atanl(long double x);

     float
     atanf(float x);

DESCRIPTION
     The atan() function computes the principal value of the arc tangent of x.
     The result is in the range [-pi/2, +pi/2].

I'm assuming that Jwasm also has an extern directive, in which case you can just stuff those externs into an include and call it a day.

HTH,

A.

Offline AIR

  • BASIC Developer
  • Posts: 782
Re: BxbAsm
« Reply #365 on: July 11, 2012, 04:17:32 pm »

I've made small progress, due to the fact that I have to figure out what headers I need and where they and the library files might be on the linux system.


Use the 'man' command I mentioned previously for a given function to find out which header file you need to look at.

For libc6, take a look here to see what is installed where on an Ubuntu Hardy system for example.

On a given Debian-based distro, you can use dpkg -L <packagename> to get a listing of the contents of a package.  Other distribution package managers should also have this ability.

A.

SteveA

  • Guest
Re: BxbAsm
« Reply #366 on: July 12, 2012, 07:30:43 am »
Hey AIR,
thanks for the info.
Might be just what I was looking for.

I'll try the header converter and see what it produces.
At this point, it cant hurt anything.

I appreciate the help.
Steve

SteveA

  • Guest
Re: BxbAsm
« Reply #367 on: July 12, 2012, 01:40:41 pm »
I've attempted to use different methods for converting gcc headers into assembler style '.inc' files.
Below are some examples that illustrate the resulting code.
I used 'h2incx', konw-fasm.pl and manual conversion of the gcc header 'term.h'.
Below is a snippet of a code section from the source file, followed by the resulting conversion of the same code section.

This is the source fragment:
Code: [Select]
/* term.h */

/* internals */
extern NCURSES_EXPORT(int) _nc_set_tty_mode (TTY *buf);
extern NCURSES_EXPORT(int) _nc_get_tty_mode (TTY *buf);
extern NCURSES_EXPORT(int) _nc_read_entry (const char * const, char * const, TERMTYPE *const);
extern NCURSES_EXPORT(int) _nc_read_file_entry (const char *const, TERMTYPE *);
extern NCURSES_EXPORT(int) _nc_read_termtype (TERMTYPE *, char *, int);
extern NCURSES_EXPORT(char *) _nc_first_name (const char *const);
extern NCURSES_EXPORT(int) _nc_name_match (const char *const, const char *const, const char *const);
extern NCURSES_EXPORT(const TERMTYPE *) _nc_fallback (const char *);

/* entry points */
extern NCURSES_EXPORT(TERMINAL *) set_curterm (TERMINAL *);
extern NCURSES_EXPORT(int) del_curterm (TERMINAL *);

/* miscellaneous entry points */
extern NCURSES_EXPORT(int) restartterm (NCURSES_CONST char *, int, int *);
extern NCURSES_EXPORT(int) setupterm (NCURSES_CONST char *,int,int *);

This is a h2incx converted fragment of the above:
Code: [Select]
/* h2incx generated: term.inc */

;NCURSES_EXPORT proto  :DWORD
_nc_set_tty_mode proto  :ptr TTY
;NCURSES_EXPORT proto  :DWORD
_nc_get_tty_mode proto  :ptr TTY
;NCURSES_EXPORT proto  :DWORD
_nc_read_entry proto  :ptr SBYTE, :ptr SBYTE, :ptr TERMTYPE
;NCURSES_EXPORT proto  :DWORD
_nc_read_file_entry proto  :ptr SBYTE, :ptr TERMTYPE
;NCURSES_EXPORT proto  :DWORD
_nc_read_termtype proto  :ptr TERMTYPE, :ptr SBYTE, :DWORD
;NCURSES_EXPORT proto  :ptr SBYTE
_nc_first_name proto  :ptr SBYTE
;NCURSES_EXPORT proto  :DWORD
_nc_name_match proto  :ptr SBYTE, :ptr SBYTE, :ptr SBYTE
;NCURSES_EXPORT proto  :ptr TERMTYPE
_nc_fallback proto  :ptr SBYTE
;NCURSES_EXPORT proto  :ptr TERMINAL
set_curterm proto  :ptr TERMINAL
;NCURSES_EXPORT proto  :DWORD
del_curterm proto  :ptr TERMINAL
;NCURSES_EXPORT proto  :DWORD
restartterm proto  :ptr NCURSES_CONST, :DWORD, :ptr DWORD
;NCURSES_EXPORT proto  :DWORD
setupterm proto  :ptr NCURSES_CONST, :DWORD, :ptr DWORD
The comments were added by me. It appears that h2incx misunderstood the declaration and converted the return type as well.
The header does not compile without errors.


This is a konw-fasm.pl converted fragment:
Code: [Select]
/* konw-fasm.pl generated term.inc */

/* internals */


/* entry points */


/* miscellaneous entry points */

konw-fasm.pl (as well as the -nasm.pl and -gas.pl) could not understand the declaration and therefore completely omitted the section.


This was converted manually:
Code: [Select]
/* hand generated: term.inc */

;/* internals */
_nc_set_tty_mode proto c :DWORD
_nc_get_tty_mode proto c :DWORD
_nc_read_entry proto c :DWORD,:DWORD,:DWORD
_nc_read_file_entry proto c :DWORD,:DWORD
_nc_read_termtype proto c :DWORD,:DWORD,:DWORD
_nc_first_name proto c :DWORD
_nc_name_match proto c :DWORD,:DWORD,:DWORD
_nc_fallback proto c :DWORD

;/* entry points */
set_curterm proto c :DWORD
del_curterm proto c :DWORD

;/* miscellaneous entry points */
restartterm proto c :DWORD,:DWORD,:DWORD
setupterm proto c :DWORD,:DWORD,:DWORD

The manually generated header compiles without errors, but, does not execute properly.
I have to tinker with it some more.

SteveA

  • Guest
Re: BxbAsm
« Reply #368 on: July 12, 2012, 08:23:34 pm »
Can anyone figure out why this code sample does not function correctly on Linux ? 

Code: [Select]
/* cls.c:
    Simulate Win32 ClearScreen() and Win32 C library _kbhit() function.
    Both are excerpts from: Beginning Linux Programming, 4th ed.
*/
/*
    compile using: gcc -o cls cls.c -lncurses
    execute using: ./cls
*/

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <term.h>
#include <curses.h>
#include <unistd.h>


static struct termios initial_settings, new_settings;
static    int peek_character = -1;

void init_keyboard(void);
void close_keyboard(void);
int _kbhit(void);
int kb_hit(void);
int readch(void);
void ClearScreen(void);

int _kbhit()                 /* this tests if a key has been pressed */
{   int ch = 0;
   
    init_keyboard();

    if(kb_hit())
    {
        ch = readch();
        ch = 1;
    }

    close_keyboard();
    return ch;
}


void init_keyboard()         /* initialize console keyboard */
{
    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] = 1;
    new_settings.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &new_settings);
}


int kb_hit()               /* if kb press, then return TRUE, else */
{   char ch;               /* reset parameters and return FALSE */
    int nread;
   
    if(peek_character != -1)
    {
        return 1;
    }
   
    new_settings.c_cc[VMIN] = 0;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0, &ch, 1);
    new_settings.c_cc[VMIN] = 1;
    tcsetattr(0, TCSANOW, &new_settings);
   
    if(nread == 1)
    {
        peek_character = ch;
        return 1;
    }
    return 0;
}


int readch()                 /* if keypress = TRUE, then put char back in buffer */
{   char ch;
   
    if(peek_character != -1)
    {
        ch = peek_character;
        ungetc(ch, stdin);
        peek_character = -1;
        return 1;
    }
    return 0;
}


void close_keyboard()        /* reset console to original settings */
{
    tcsetattr(0, TCSANOW, &initial_settings);
}


void ClearScreen()      /* this function clears the console screen. */
{   char *clear;
   
    setupterm(NULL, fileno(stdout), (int *)0);
    clear = tigetstr("clear");
    putp(clear);
}


int main()                    /* now test these functions */
{   char ch;
    int ich = 0;

    ClearScreen();

    while(ich == 0)           /* this routine will behave like INKEY$ */
    {                         /* waiting for a keypress */
        ich = _kbhit();
    }

    ch = getchar();           /* this will capture the keypress character */
    printf("char = %c\n", ch);
    return 0;
}


It performs 2 Basic routines: CLS and INKEY$.
Each function performs correctly when run independently.
However, when the two are combined, as in the above example, it does not behave as expected.

The "main" function represents this BASIC program:
Code: [Select]
   CLS
   WHILE a$ = ""
       a$ = INKEY$
   WEND
   PRINT a$
   END

The CLS part does not clear the screen until after the keypress event has occurred.  ???

jcfuller

  • Guest
Re: BxbAsm
« Reply #369 on: July 14, 2012, 05:26:23 am »
Steve,
  I know very little of ncurses but replacing your ClearScreen with this one works:
James

Code: [Select]
void ClearScreen()      /* this function clears the console screen. */

 system("clear");
}


jcfuller

  • Guest
Re: BxbAsm
« Reply #370 on: July 14, 2012, 06:35:19 am »
Steve,
  In the ncurses examples I remember (yeah right:)) I think the library was initialized in main before any calls?
It probably is not a good idea to mix term and ncurses calls? If you commit to ncurses just use them.

James
 

SteveA

  • Guest
Re: BxbAsm
« Reply #371 on: July 14, 2012, 12:15:44 pm »
  I know very little of ncurses but replacing your ClearScreen with this one works:

Code: [Select]
void ClearScreen()      /* this function clears the console screen. */

 system("clear");
}


Hey James,

Yeh, that does work.
However, I wanted to avoid using system().
It is claimed that using system() is a weak point and leaves the operating system open to vulnerabilities.
That's why I wanted to use the API functions as much as possible.

SteveA

  • Guest
Re: BxbAsm
« Reply #372 on: July 14, 2012, 12:25:04 pm »
Steve,
  In the ncurses examples I remember (yeah right:)) I think the library was initialized in main before any calls?
It probably is not a good idea to mix term and ncurses calls? If you commit to ncurses just use them.

James
 

Huh...!
can't mix term and ncurses....hmm.
I suppose that could explain the unusual behaviour.

But,...
in the book they seem to do just that.
The examples given use term.h, termios.h and curses.h in the same sample programs.
I don't think I've made a mistake in the code, but, something clearly isn't right.

BTW, did you happen to compile the sample I posted ?
If so, did it behave strangely, as I indicated ?

jcfuller

  • Guest
Re: BxbAsm
« Reply #373 on: July 14, 2012, 01:21:15 pm »
BTW, did you happen to compile the sample I posted ?
If so, did it behave strangely, as I indicated ?

Yes
Yes

James

jcfuller

  • Guest
Re: BxbAsm
« Reply #374 on: July 14, 2012, 02:08:32 pm »
Steve,
Ubx basic code:
Code: [Select]
DIM a$
CLS
   WHILE a$ = ""
       a$ = INKEY$
   WEND
   PRINT a$


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");
}


James