+ Reply to Thread
Results 1 to 4 of 4

Thread: Macros

  1. #1
    Sysop_fb is offline Programmer
    Join Date
    Apr 2009
    Location
    Missouri
    Posts
    150
    Blog Entries
    2
    Rep Power
    12

    Macros

    Any code will be NASM, x86 arch tested on cygwin.
    Personally I love macros and what spurred me to write this was my adventure today while I was remapping the PICs for my hobby O/S.

    So what is a macro in assembly?
    When the assembler encounters a macro label in your source code, it replaces the label with the text lines that the macro label represents. Also called 'expanding the macro'.

    A macro definition is stored between a %macro and %endmacro directive.
    Code:
    %macro
    ...
    %endmacro
    Now after the %macro will be our label then followed by the number of parameters.

    Code:
    %macro mymult 2
    ...
    %endmacro
    So we named our macro 'mymult' and said it will take two parameters but how do we use these parameters?

    Code:
    %macro mymult 2
        mov ecx, %2
        mov eax, %1
    ...
    %endmacro
    Basically this says to copy the first parameter or %1 into eax and copy the second parameter or %2 into ecx.
    Pretty simple huh? Ok now for the rest of the code.

    Code:
    %macro mymult 2
        mov ecx, %2
        mov eax, %1
        dec ecx
    .loop:
        add eax, %1
        dec ecx
        jnz .loop
    %endmacro
    Cool now how do we call it? It's easy just use the label 'mymult', no need for a call.

    Code:
    mymult 2, 4
    Another reason to use macro is when you have a bunch of similar pieces of code to write many times.
    Such as writing IRQs when remapping a PIC. If you don't understand the code itself don't worry just watch how I use macros to my advantage.

    Code:
    global _irq0
    _irq0:
        cli
        push byte 0    
        push byte 32
        jmp irq_common_stub
    
    ;All the way to...
    
    global _irq15
    _irq15:
        cli
        push byte 0    
        push byte 47
        jmp irq_common_stub
    You'll notice there's only a few numbers that are changing.
    So instead I just make a macro and let it do the work.

    Code:
    %macro Irq 2
      global _irq%1
      _irq%1:
        cli
        push byte 0
        push byte %2
        jmp irq_common_stub
    %endmacro
    
    Irq 0, 32
    Irq 1, 33
    Irq 2, 34
    ...
    Irq 15, 47
    So when the assembler hits this for instance
    Code:
    Irq 0, 32
    It will turn it into
    Code:
    global _irq0
    _irq0:
        cli
        push byte 0
        push byte 32
        jmp irq_common_stub
    And it'll continue to do that for the rest of them.


    So why do this? Why not just write a procedure? Well both ways have there pros and cons.
    Macros are fast, there's no call or ret instruction the only instructions used are the one's actually doing work. However if there is a speed increase in code what does that usually tell us about memory?
    Well let's say we have a macro that does 4 instructions and that macro is called 3 times. That's 12 instructions generated into memory.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Macros

    +rep! Just because I'm anal-retentive, I have a few qualms about your mymul. What if the second operand is something large, like 80? I'll post a tutorial about multiplying and dividing so you'll know what I mean.
    Last edited by dargueta; 03-18-2010 at 10:35 PM.
    sudo rm -rf /

  4. #3
    Sysop_fb is offline Programmer
    Join Date
    Apr 2009
    Location
    Missouri
    Posts
    150
    Blog Entries
    2
    Rep Power
    12

    Re: Macros

    That piece of code wasn't written with the intention of being optimized in any way. I wrote it in my head with the intention of an easy to follow macro so a beginner can get his feet wet before moving into a real-world example.
    I assumed you were just plugging for that other tutorial so I waited until you wrote it to post

    Once I get some extra time I'll read over your post a bit more, assembly optimization is a favorite subject of mine eventhough it's possibly the broadest subject to ever conceive of mastering. I would argue impossible even.
    I do appreciate the first paragraph explaining how cycle counting is insanely deceiving. Will read more later.
    "The best optimizer is between your ears" - Michael Abrash
    Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Macros

    Great tutorial, it's just that I'm...uh...optimizing is my cocaine...
    I'm trying to kick it, I swear.
    sudo rm -rf /

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. macros problem........ help please........
    By csepraveenkumar in forum C and C++
    Replies: 11
    Last Post: 01-21-2011, 10:24 PM
  2. VB Macros Help Please!
    By Johnathanx in forum Visual Basic Programming
    Replies: 0
    Last Post: 09-13-2010, 04:43 AM
  3. predefined macros
    By x42 in forum C and C++
    Replies: 2
    Last Post: 06-20-2010, 09:40 PM
  4. Excel Macros: program to run multiple
    By GNR23 in forum Visual Basic Programming
    Replies: 0
    Last Post: 07-08-2009, 06:25 PM
  5. Messing with Macros in C
    By helixed in forum C and C++
    Replies: 4
    Last Post: 12-24-2008, 04:36 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts