Inline assembly code check

This is a discussion on Inline assembly code check within the C++ Programming forums, part of the General Programming Boards category; Code: void a(char *string, HWND rsHwnd) { sMessage = (DWORD)GetProcAddress(LoadLibrary("User32.dll"), "SendMessageA"); LPARAM lstring = LPARAM(string); __asm { mov eax, rsHwnd ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Inline assembly code check

    Code:
    void a(char *string, HWND rsHwnd)
    {
    	
    	sMessage = (DWORD)GetProcAddress(LoadLibrary("User32.dll"), "SendMessageA");
    	LPARAM lstring = LPARAM(string);
    	__asm {
    		mov eax, rsHwnd
    		
    		xor ebx, ebx
    		cmp eax, ebx
    
    		je $invalidHandle
    
    		mov ebx, WM_SETTEXT
    		
    		push lstring
    		push 0
    		push ebx
    		push eax
    		call sMessage
    $invalidHandle:
    	};
    }
    I am kind of new to the whole assembly thing. I am just wondering if this code is ok. Someone told me I should save ebx.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You don't need to save EBX, it's done by the compiler for you. But I don't see why you need to use EBX in the first place - nor why you really need to use inline assembler (that will ensure that your code won't be portable to Windows x64 if you should wish to do that later on.

    Some simplification in red:
    Code:
    void a(char *string, HWND rsHwnd)
    {
    	
    	sMessage = (DWORD)GetProcAddress(LoadLibrary("User32.dll"), "SendMessageA");
    	LPARAM lstring = LPARAM(string);
    	__asm {
    		mov eax, rsHwnd
    
    		cmp eax, 0
    		je $invalidHandle
    
    		push lstring
    		push 0
    		push VM_SETTEXT
    		push eax
    		call sMessage
    $invalidHandle:
    	};
    }
    Alternative C-code:
    Code:
    typedef LRESULT (*tpSendMessage)(      
        HWND hWnd,
        UINT Msg,
        WPARAM wParam,
        LPARAM lParam
    );
    void a(char *string, HWND rsHwnd)
    {
            static tpSendMessage pSendMessage = 0;
    	if (rsHwnd) {
               if (!pSendMessage) {
         	      pSendMessage = GetProcAddress(LoadLibrary("User32.dll"), "SendMessageA");
                  if (!pSendMessage) {  ... do some error handling ... }
               }
    	   pSendMessage(rsHwnd, VM_SETTEXT, 0, LPARAM(string));
            }
    }
    Note, I made pSendMessage a static variable, so once it's been "found", you don't need to call GetProcAddress/LoadLibrary again.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    test eax, eax
    is the common form of the == 0 check (followed by a jz). The reason is that the instruction is smaller (1 byte vs 2, I think).

    But I agree that the plain C is better.
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    test eax, eax
    is the common form of the == 0 check (followed by a jz). The reason is that the instruction is smaller (1 byte vs 2, I think).

    But I agree that the plain C is better.
    Good point on test (or and eax, eax - same length and same result).

    But I don't see any point in using inline assembler for this at all - there's no performance benefit (calling loadlibrary will take 1000x more time than calling sendmessage anyways), and it can be done in C. Which makes it portable to other processor architectures, including x64 which doesn't allow inline assembler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    including x64 which doesn't allow inline assembler.
    whoa, what? why not?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    whoa, what? why not?
    Because Microsoft didn't want it in the compiler... I don't know if it's changed, but it wasn't part of 64-bit visual C++ a couple of years ago.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    x64 has no problem with inline assembler (how would it tell if there is any?) It's Visual C++ for x64 that refuses to compile it.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    x64 has no problem with inline assembler (how would it tell if there is any?) It's Visual C++ for x64 that refuses to compile it.
    Yes, that's what I was trying to say. May not have been entirely clear tho'.

    32-bit Inline assembler works just fine with the 32-bit compiler on 64-bit machine, but if you compile for 64-bit, the Visual C++ compiler will refuse any inline-code. Using GCC (and probably PGC) you can compile with Inline assembler. There's also a version of MASM that works for 64-bit, so if you really need some assembler-code, it can be done - just not as inline in Visual C++ [or the compiler behind the IDE].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly in C code?
    By TriKri in forum C Programming
    Replies: 18
    Last Post: 06-27-2006, 04:10 PM
  2. Understading assembly code created by gcc
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-30-2006, 10:58 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. converting c/c++ code to assembly
    By moonwalker in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-25-2002, 03:15 PM
  5. inline assembly question
    By DavidP in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 06:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21