View Single Post
Old 10 April 2019, 19:12   #12
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 439
My thoughts:

* I would not check for success after opening DOSBase, it's there, unless you specify a futile version, but 33 is always sufficient.
* You could spare the AllocMem/FreeMem by using a global variable and it ends up in BSS.
* use __entrypoint at your start function to omit push/pop of registers.

Here you go:
Code:
#include <proto/exec.h>
#include <proto/dos.h>
#include <exec/alerts.h>

#include <string.h>

struct globals {
  struct ExecBase *SysBase;
  struct DosLibrary *DOSBase;
  BPTR   Stdout;
} __g = {0};

// needed here to open/close libs
__asm(".equ _SysBase, __g");
__asm(".equ _DOSBase, __g+4");

// needed everywhere ->put into header
#define SysBase gv->SysBase
#define DOSBase gv->DOSBase

STATIC const ULONG tricky=0x16c04e75; /* move.b d0,(a3)+ ; rts */
VOID myprintf(struct globals *gv, char *fmt,...) {
  unsigned char buf[256];
  STRPTR *arg = (STRPTR *)(&fmt+1);
  RawDoFmt((STRPTR) fmt, arg, (void (*)())&tricky, buf);
  Write(gv->Stdout, buf, strlen((char *)buf));
}

__entrypoint int foo() {
  struct globals *gv = (struct globals *)&__g;
  SysBase = (*((struct ExecBase **) 4));
  
  DOSBase = (struct DosLibrary *)OpenLibrary((STRPTR)"dos.library",33);
  
  gv->Stdout = Output();

  float x = 1.623 * (float) 100;
  myprintf(gv, "Just a hello world. if you cast x to integer is: %ld\n", (int)x);

  CloseLibrary((struct Library *) DOSBase);
  return 0;
}

Last edited by bebbo; 10 April 2019 at 19:52. Reason: added da code
bebbo is offline  
 
Page generated in 0.04257 seconds with 11 queries