Thread: inline ASM as a macro

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    inline ASM as a macro

    I am just wondering if anyone could please tell me how I could write this as a macro so that it could be used several places throughout the code (without having to write the inline asm every time):

    Code:
    __asm {
       call short afterString
       db 'myString',0
    afterString:
       pop myVar
    }
    so that it looks something like this:

    Code:
    GET_STRING(myVar, "myString");
    any help would be appreciated. thank you in advance.
    Last edited by Bleech; 10-05-2006 at 03:18 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Since there must be a linebreak before a label, and since macro definitions cannot contain whitespace, I would say this is impossible. What on earth to you want to achieve with it, anyway?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Looks like a funky thing used in shellcode to get the address of a string on the stack.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CornedBee
    Since there must be a linebreak before a label, and since macro definitions cannot contain whitespace, I would say this is impossible. What on earth to you want to achieve with it, anyway?
    What do you mean macros can't have whitespace? In the macro name? Macros can span multiple lines. Just end the line in a \
    Code:
    #define FOO(x,y) \
    { \
        blah \
        bla \
        blahblah \
    }
    Something like that perhaps? The only problem with the ASM being turned into a macro is the single quote around the string in the example, where they're passing it a string literal which uses double-quotes. You'd have to pass it in single quotes.
    Code:
    #define FOO(x,y) \
    __asm { \
       call short afterString \
       db x,0 \
    afterString: \
       pop y \
    }
    That might work. Used like:
    Code:
    FOO('whatever', somevar )
    Give it a shot. Worse case scenario, it nukes your PC.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm convinced some folks should be banned from using assembly.

    What on earth are you accomplishing in that crappy section of assembly code that you cannot do in C?

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    What on earth are you accomplishing in that crappy section of assembly code that you cannot do in C?
    Trying to crash his box maybe? If he wants to do it, let him. We wouldn't want to appear authoritarian, would we.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by quzah
    What do you mean macros can't have whitespace?
    I mean they can't have newlines in the expansion and mistyped.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This code does nothing but perform a call to a subroutine, an illegal call at that, and then pops something off of the stack. Now why would you return something on the stack from a function and moreso how would you? This is returning a reference to an object that is out of scope. The myVar would have to exist prior to the function in order to work. Either myVar is global which is stupid b/c then why would it be on the stack, or something else is going on that we cannot see from this ridiculous section of code.

    Most likely this will result in an access violation and I'm not even sure call short is a valid Intel combination. jmp short is and means to jump to -128 to +127 bytes from the current address to another address. In C, you cannot guarantee even using asm macros that this will be the case.

    To the OP: Do yourself a favor...if you do not understand assembly language, don't use it. Nothing you do will probably ever require you to use inline assembly. Inline assembly comes with overhead and dangers not apparent in the snippet. Best thing is to use a pure assembly file and link with it instead of doing inline.
    Last edited by VirtualAce; 10-06-2006 at 11:38 AM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CornedBee
    I mean they can't have newlines in the expansion and mistyped.
    Do you have a point of reference in the Standard? I'm not finding it.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by quzah
    Do you have a point of reference in the Standard? I'm not finding it.
    A newline in the expansion that is not escaped ends the macro definition. A newline that is escaped is removed. The sequence \n is uninterpreted except within string literals. How would you get a newline into the macro expansion, then?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The newline doesn't matter. You can use what Quzah said and it works just fine. I don't understand what you are getting at.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The macro emits this code:
    Code:
    __asm {    call short afterString    db x,0 afterString:    pop y }
    I was under the impression that newlines matter for labels. I might be wrong. I never cared about them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't see anything in the Standard saying it has to be replaced as a single line.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Doesn't 6.10 of C99 mean that a #define must be one line?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    This code does nothing but perform a call to a subroutine, an illegal call at that, and then pops something off of the stack. Now why would you return something on the stack from a function and moreso how would you? This is returning a reference to an object that is out of scope. The myVar would have to exist prior to the function in order to work. Either myVar is global which is stupid b/c then why would it be on the stack, or something else is going on that we cannot see from this ridiculous section of code.
    I believe what the call does is it pushes address of the 'instruction', represented by the db 'myString', to return to onto the call stack as a RET address, and the pop var gets the address of the string by popping it back off. This could be useful for shellcode. Of course, now the 'procedure' can't really return but it never was really intended to be returned to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Inline asm
    By wavering in forum C Programming
    Replies: 2
    Last Post: 01-29-2002, 02:42 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM