Search code examples
assemblyimportx86masm

Exporting a byte array in x86 assembly


I'm currently working on an assembly project (MASM in combination with Dosbox), which is basically recreating pong for educational purposes. I wrote a function to draw sprites (byte arrays) to the video memory (mode 13h) but these sprites are starting to clutter the top half of the file (because they can be quite large, and I much prefer the artistic value of these large byte arrays in comparison to drawing from bitmaps)

I don't really know how includes work (which is kind of the problem), but I found a file that shows me the general syntax. For example, I could have a "DERP.INC" in combination with a "DERP.ASM" file. This ASM file would define a variable (db value) "horse" which I could export in the INC file by doing:

externdef horse:BYTE

and putting this in the ASM file:

PUBLIC horse

In the INC file, we clearly state the size of horse is one byte. Now, the sprites are defined like this:

sprite db 1, 0, 0
       db 0, 1, 0
       db 0, 0, 1 

How would I be able to export the whole byte array?


Solution

  • There's no need. sprite is simply a name that will map to some address. It's up to you to deal with how many bytes starting from that address might constitute parts of that same piece of data. One common method is something like:

    sprite db 1, 0, 0
           db 0, 1, 0
           db 0, 0, 1 
    
    sprite_len = $ - sprite
    

    Making the name public so it's accessible from some other file doesn't really change the situation. You're still only making the name public. If you want to keep track of the length, you can make the name you've given to the length public as well. That doesn't really export any more or less of the data though -- if the receiver already knows that the sprite will always be 9 bytes long, they can work with a spite based only on its starting address, just like code in the same file would. The fact that you haven't defined anything to specify the length of the sprite tends to indicate that your code depends on its being a fixed length -- if so, just exporting sprite will work just fine.

    Bottom line: public and externdef basically make it as if the code and the data declaration were in the same file. "Exporting" the symbol doesn't automatically give receiving code any more or less information about that symbol than if it was in the same file so the data declaration was directly visible.