Some functions to handle assembler

Posted on
  • I was searching for a way to handle assembler code.
    Web Editor supports this in a nice way, but how should assembler be treated in modules.
    So a small object was born, which supports loading from array and/or from local sdCard.
    Location for storage are calculated inside the object, so multiple binarys are easy to handle.
    There is also a list of loaded binarys.

    function ASM(){
      this.ASMBase = process.memory().stackEndAddress;
      this.ASMList = [];
    }
    ASM.prototype.loadArray = function(varName,format,arr){
      var a = {name:varName,format:format,start:this.A­SMBase + 1};
      for(var i = 0; i < arr.length; i++){
        poke16(this.ASMBase,arr[i]);
        this.ASMBase += 2;
      }
      this.ASMList.push(a);
      eval('var ' + a.name + ' = E.nativeCall(' + a.start + ',"' + a.format + '");');
    };
    ASM.prototype.loadSD = function(varName,format,fileName){
      var arr,fs = require("fs");
      arr = new Uint16Array(E.toArrayBuffer(fs.readFile(­fileName)));
      return(this.loadArray(varName,format,arr­));
    };
    //convert to a module by uncomment next line
    //exports.connect = function(){ return new ASM(); }
    
    ASMMod = new ASM();
    //load binarys by name, format of variables and array or filename on SD
    ASMMod.loadArray("tst1","int(int,int)",[­0x1840,0x4770]);//add r0,r0,r1  ...  bx lr
    ASMMod.loadArray("tst2","int(int)",[0x47­70]);// bx lr
    ASMMod.loadSD("tst3","int(int,int)","nod­e_binaries/adder.BIN");//same as tst1 loaded as binary file from sdCard
    
    console.log(ASMMod.ASMList);
    
    console.log(tst1(2,6) + " should be 8");
    console.log(tst2(3) + " should be 3");
    console.log(tst3(4,5) + " should be 9");
    
  • Nice! If this were a module, you could do something like this from within an existing module, and it would all 'just work':

    var tst1 = require("asm").load("int(int,int)",[0x18­40,0x4770]);
    

    The module could also do some more interesting stuff - perhaps using STM32F1Flash to write the data into flash memory, so it can still be used after power off.

  • Module could be like this

    /* Copyright (c) 2014 Juergen Marsch, Pur3 Ltd. See the file LICENSE for copying permission. */
    /*!
      **/
    var ASMList,ASMBase;
    ASMBase = process.memory().stackEndAddress;
    ASMList = [];
    exports.connect = function(){
      function ASM(){
        this.loadArray = function(varName,format,arr){
          var a = {name:varName,format:format,start:ASMBas­e + 1};
          for(var i = 0; i < arr.length; i++){
            poke16(ASMBase,arr[i]);
            ASMBase += 2;
          }
          ASMList.push(a);
          eval('var ' + a.name + ' = E.nativeCall(' + a.start + ',"' + a.format + '");');
        };
        this.loadSD = function(varName,format,fileName){
          var arr,fs = require("fs");
          arr = new Uint16Array(E.toArrayBuffer(fs.readFile(­fileName)));
          return(this.loadArray(varName,format,arr­));
        };
        this.list = function(){ return ASMList; }
      }
      return new ASM();
    }
    

    It could be used like this

    var x = require("ASM").connect();
    x.loadArray("tst1","int(int,int)",[0x184­0,0x4770]);
    
    require("ASM").connect().loadArray("tst2­","int(int)",[0x4770]);
    
    require("ASM").connect().loadSD("tst3","­int(int,int)","node_binaries/adder.BIN")­;
    
    console.log(require("ASM").connect().lis­t());
    
    console.log(tst1(2,6) + " should be 8");
    console.log(tst2(3) + " should be 3");
    console.log(tst3(4,5) + " should be 9");
    

    I can't follow the comment about STM32F1Flash. That module already exists in your collection. Which kind of data would you write from ASM to Flash ?
    I would like to keep ASM module as small as possible, so it doesn't add load which is used in special cases only.

  • At the moment, the assembler code is written into RAM at the end of the stack. It means that if you power the board off and back on you'd have to call loadArray again (as well as making sure that ASMBase was set).

    By writing the data into flash, it could be stored there so the functions worked like you'd expect after power cycling (that's what STM32F1Flash can do).

  • IMHO, STM31F1Flash should support this with a command like copyMem(startAdress,endAdress,length)
    It would be a overload for the small ASM module to require STM31F1Flash always.

  • Sorry, I'm not sure I understand. You want another method inside it?

  • Let me try with other words.

    The assembler module is designed to support handling of multiple binaries from everywhere (module or main code). We don't have too much experience with binaries, anyway my expection is that they are stored in memory in most cases. To be useful, the modul should be small.

    The decision of storing in Flash is project specific. If somebody wants to copy binaries to flash, it would be helpful to have a function in the module for Flash handling. Some other functions like copy arrays to Flash may also be helpful.

    In my project, this is not the case (yet?). Therefore I'm not asking for it. And I will not extend ASM module.

  • The situation here has now changed a bit...

    When you write assembler it's now assembled into a 'flat string' which is stored in the RAM dedicated to variable storage. It means that you can now save() assembled code and it'll 'just work'.

    Loading from flash is now as easy as:

    var myFunction = E.nativeCall(1, "JsVar(JsVar,JsVar)", require("fs").readFileSync("myFunction.b­in")); 
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Some functions to handle assembler

Posted by Avatar for JumJum @JumJum

Actions