Results 1 to 4 of 4
  1. #1
    TrollerCoaster's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Location
    I am a fish
    Posts
    633
    Reputation
    61
    Thanks
    800

    Cool Object-oriented assembly <FASM>

    I used FASM Struct libs to demonstrate this and 16-bit assembly. This will also work for 32 bit assembly and it is possible to do this without struct libraries, which I show later.
    Code:
    use16
    org 100h ;ew, DOS
    
    include "MACRO\STRUCT.INC"
    
    jmp start
    
    struct Enemy ;new class/object
      hp dd 22
      name db 'Zombie$'
      getName dw Enemy_getName ;You can put functions, variables, whatever you'd like!
      size db 0
    ends
    
    Enemy_getName:
      mov ax,bx ;We can assume bx is the instance pointer since function new returns it that way. Register ES may also be a good choice.
      add ax,Enemy.name ;Enemy.name is an offset to the name variable, so add it
      ret
    
    enemy Enemy ;use this as your default instance for copying
    
    start:
      mov ax,enemy ;push the default instance
      mov bx,Enemy.size ;push the STRUCT's .size
      call new
    
      ;new returns the instance pointer in bx, so do what you need to do with it
    
      call [bx+Enemy.getName] ;We could just directly get the pointer as well with mov ax,[bx+Enemy.name]
      mov dx,ax ;we'll use a DOS interrupt to print the new enemy's name. mov dx,[bx+Enemy.name] would've been more efficient.
      mov ah,09 ;DOS print-string
      int 21h ;DOS interrupt. I usually don't use them, but it's for demonstration purposes
      ret
    
    new: ;makes a new instance of an object. Could also be used to clone an instance.
            ;ax - Instance/object to copy
            ;bx - Size of class
            ;returns: bx - pointer to instance
            ;This could be modified to use pushes instead of registers, but I made an interrupt that pointed to this function.
      pusha
      mov si,ax ;What to copy
      mov ax,[new_obj_ptr]
      mov di,ax ;Where to paste
      mov cx,bx ;Size of object
      rep movsb ;very easy to copy the object.
      popa
    
      push ax
      mov ax,[new_obj_ptr]
      push ax
      add ax,bx
      mov [new_obj_ptr],ax
      pop ax
      mov bx,ax
      pop ax
      ret
    
      new_obj_ptr dw _objects ;This will point to where to copy a new instance
    
    _objects:
    ;This space is reserved for instances. Do not add anything after this. Optionally define 0s so no other memory is loaded here
    times 0x400 db 0 ;allocate 1,024 bytes for instances
    Just a reminder that if you are using 32-bit assembly, you may have to adjust the registers accordingly. You need to change pointer-style variables to 4 bytes too.

    This simply creates an instance of Enemy and shows it's name. I'm not sure if it's 100% working on DOS, but the instance part definitely is.
    A limitation of this system is that you cannot use destructors. If you want to use destructors, you could make a space reserved for only one class, and then manage the memory that way.

    If you don't have structs, you can use raw pointers, but it'll be very awkward and inconvenient to use. Here's an example:

    Code:
    define Enemy.hp 0 ;if periods don't work, use underscores
    define Enemy.name 4
    define Enemy.getName 11
    define Enemy.size 13
    
    jmp start
    
    Enemy: ;default instance
      dd 24
      db 'Zombie$'
      dw Enemy_getName
    
    Enemy_getName: ;You can technically put it inside the object, but why calculate the size of that and have to copy it?
      mov ax,bx
      add ax,Enemy.name
      ret
    
    start:
      mov ax,Enemy
      mov bx,Enemy.size
      call new
      ;And you can do what you want with it normally
      ret
    With some extra work, you can probably also include parent and child classes.

    Hope you liked the tutorial

  2. #2
    ♪~ ᕕ(ᐛ)ᕗ's Avatar
    Join Date
    Jun 2010
    Gender
    male
    Location
    Uterus
    Posts
    9,119
    Reputation
    1096
    Thanks
    1,970
    My Mood
    Doh
    TBH I find assembly really hard to learn, but I love seeing tuts like this

  3. #3
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    same here. Even though my brain slows down when I try to think about assembly, I *know* I don't understand it as well as I should. It's still nice to see tuts like this, even if I can't actually apply them to my own knowledge/projects yet. very interesting concept. thanks.

  4. #4
    Psychotic's Avatar
    Join Date
    May 2012
    Gender
    male
    Posts
    13,825
    Reputation
    4234
    Thanks
    6,055
    I once tried learning ASM. I couldn't even find a compiler for it. I remember getting this one program and I don't even know what to do with it. Nonetheless, nice tutorial.






    Super User since 02.02.2020
    Global Moderator since 09.23.2017
    Moderator since 09.01.2016
    Minion+ since 07.22.2016

    Marketplace Minion since 06.09.2016
    Trove Minion since 06.06.2016
    Middleman since 04.21.2016
    Social Engineering Minion since 02.03.2016
    News FO Freelancer From 11.08.2015 to 07.23.2016
    News FO Head Editor From 08.23.2015 to 11.08.2015
    News FO Head Editor From 07.19.2012 to 08.11.2014
    MPGH News and News FO Founder
    Programming Minion From Unknown to 04.23.2013
    Minecraft Minion From 09.19.2012 to 04.23.2013
    Member since 05.13.2012



Similar Threads

  1. In What Do you Code Assembly ?
    By apezwijn in forum Assembly
    Replies: 5
    Last Post: 08-11-2008, 10:47 AM
  2. I wanne start learning Assembly plz read.!
    By Niratsio in forum Visual Basic Programming
    Replies: 14
    Last Post: 10-31-2007, 08:05 AM
  3. Converting Assembly Into Bytes
    By radnomguywfq3 in forum Visual Basic Programming
    Replies: 0
    Last Post: 09-24-2007, 04:42 PM
  4. HTML Settings in SWF ActX Object
    By arunforce in forum C++/C Programming
    Replies: 2
    Last Post: 09-16-2007, 01:18 AM
  5. [TUT] How to add Oriental/Korean Fonts
    By W$t$5TA34TYTHSETH5Y5 in forum WarRock Korea Hacks
    Replies: 2
    Last Post: 05-06-2007, 11:08 AM