Author Topic: mos: My assembly learning playground  (Read 10202 times)

Offline dlundqvist

  • Jr. Member
  • *
  • Posts: 3
  • Country: cn
mos: My assembly learning playground
« on: June 07, 2015, 08:33:00 AM »
Hi all,

After programming in various higher level languages for many years I wanted to take a look at assembler and programming at a lower level and learn to program the hardware directly. I created mos (https://bitbucket.org/dlundqvist/mos) as my playground for this. Basically is a bootable floppy, but only been verified in a virtual machine since I don't have a real floppy drive ..., that runs a small boot loader that in turn chain loads a program loader. This program loader will eventually learn how to read RDOFF programs from a filesystem embedded in the disk image, right now it can only show a listing on the files on it. RDOFFs program could be games, utilities etc, whatever I want to explore.

As I started this my head quickly got dizzy of all the details surrounding this low level programming, so I created a small set of macros to deal with calling conventions, passing arguments and local variables in functions. Greatly simplified for me but still allows me to write assembly.

Don't know how far I'll take this, it may grow into something useful, as a stepping stone to something else, who know. I welcome any feedback.

Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: mos: My assembly learning playground
« Reply #1 on: June 07, 2015, 09:52:34 AM »
You want to standardize asm language ? me too ^^

What is yours macros ?

Offline dlundqvist

  • Jr. Member
  • *
  • Posts: 3
  • Country: cn
Re: mos: My assembly learning playground
« Reply #2 on: June 08, 2015, 02:13:46 AM »
You want to standardize asm language ? me too ^^

Not sure I want to standardize, I just want to remove some of the details.

What is yours macros ?

Basically these now:
  • mosdeclare - Declare a function.
  • mosdefine  - Define a function. Generates prologue and epilogue and some other small things.
  • mosparam  - Define a parameter.
  • moslocal    - Define a local variable.
  • moscal     - Call a function declared with mosdeclare.

And then there are mosenddec and mosenddef to end declaration and definition. Examples of usage:

Code: [Select]
mosdeclare putc
        mosparam character, byte
mosenddec

mosdeclare puts
        mosparam string, word
mosenddec

mosdefine putc
        mov al, byte [%$character] ; Parameters and local variables are accessed with %$<name>.
        mov ah, 0xe
        mov bx, 0xf
        int 0x10
mosenddef

mosdefine puts
        mov si, [%$string]
.loop:
        lodsb
        cmp al, 0
        je %$exit ; %$exit label exist in all functions defined with mosdefine.
        movzx ax, al
        moscall putc, ax ; Call function declared with mosdeclare, makes correct number of arguments are passed in.
        jmp .loop
mosenddef

You can look at https://bitbucket.org/dlundqvist/mos/src/HEAD/mos.mac for more details on what the macros do and what code is generated.

Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: mos: My assembly learning playground
« Reply #3 on: June 08, 2015, 09:00:41 AM »
Thanks but i'm not sure i'm able to understand code of macro, don't have this level x)

So you will able to do C compiler job only with macro if I understand ^^

Here's mine: http://forum.nasm.us/index.php?topic=2076.msg9415#msg9415
« Last Edit: June 15, 2015, 09:12:33 AM by shaynox »

Offline dlundqvist

  • Jr. Member
  • *
  • Posts: 3
  • Country: cn
Re: mos: My assembly learning playground
« Reply #4 on: June 15, 2015, 06:50:33 AM »
Thanks but i'm not sure i'm able to understand code of macro, don't have this level x)

So you will able to do C compiler job only with macro if I understand ^^

In a sense yes. But only because my background is in higher level languages, and as I get more comfortable
with assembly I suppose I will use these kind of macros less and less.

Here's mine: http://codes-sources.commentcamarche.net/source/browse/100763/1.ASM/HackEngine/Headers

Thanks, I'll take a look later. A bit busy recently.

Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: mos: My assembly learning playground
« Reply #5 on: June 15, 2015, 09:22:04 AM »
Yes for sure, but we know all the default of assembly, is a little complex to coding with that, and if you develop on asm in a long time, you will see macro will save you ^^

Anyway, there is a problem with macro, we know all if we build a lot of higher level languages with macro, we risk to abandon asm for higher language, so it needed to find the middle (and it's very difficult to find them)

For example, before I use if and while for structure block of code, then I use jif macro (next generation of condition block x) ), it's 2 instructions in one and translate conditional jump into math condition (> < = != ect).

do while like:
Code: [Select]
            .l1:
           ;{
                call_   windows_event, show_fps
           ;}
            jif     [quit] ,=, false, .l1


If like:
Code: [Select]
                jif  eax ,<, 1_000, .no_show_fps
               ;{
                    printf_     "FPS = %d", [.fps_data]
                    mov         [.fps_data], i32(0)
                    clock_      [.fps_data + 4]
               ;}
               .no_show_fps:

(I have update the url to http://forum.nasm.us/index.php?topic=2076.msg9415#msg9415)
« Last Edit: June 15, 2015, 11:27:39 AM by shaynox »