1 Replies - 1051 Views - Last Post: 29 November 2012 - 06:11 PM

#1 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 392
  • Joined: 08-January 12

ASM x86, Using INVOKE to call Strnicmp

Posted 29 November 2012 - 09:59 AM

I am having some trouble understanding how to compare two strings in ASM using this function. The book I have been reading does not show any examples of how this works.
I believe the interface should look something like this.
INVOKE procedureName [,argumentList]

INVOKE Strnicmp, strA, strB, nBytes



The book does show an example like this
push TYPE array
push LENGTHOF array
push OFFSET array
call DumpArray



It also says this is equivalent to the stack statements above

INVOKE DumpArray, OFFSET array, LENGTHOF array, TYPE array



Here is what I came up with so far for Strnicmp
INVOKE Strnicmp, strA DWORD, strB DWORD, nBytes DWORD


but I am still confused... :/

Any help would be appreciated

This post has been edited by Toadill: 29 November 2012 - 10:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: ASM x86, Using INVOKE to call Strnicmp

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: ASM x86, Using INVOKE to call Strnicmp

Posted 29 November 2012 - 06:11 PM

INVOKE is an internal MASM macro, that does a parameter count check and basic parameter type check during Assembly. Many users of other Assemblers have written macros to emulate this behavior since it cuts down on bugs tremendously.

In MASM, in order to use INVOKE, you need a prototype (This tells the Assembler how many parameters the function accepts, and their type)

Let's use a well known Windows function for example - CreateWindowEx. This function has 12 parameters, and is used many times in a program. What would happen if your writing code after a long day and you are very tired.
    push    NULL
    push    hInst
    push    NULL
    push    HEIGHT_MAIN_WINDOW
    push    WIDTH_MAIN_WINDOW
    push    eax
    push    ebx
    push    WS_OVERLAPPED or WS_SYSMENU or WS_MINIMIZEBOX or WS_CLIPCHILDREN or WS_VISIBLE
    push    offset szAppName
    push    offset szWndClsMain
    push    WS_EX_APPWINDOW or WS_EX_CONTROLPARENT
    call    CreateWindowEx


This would Assemble and link without issue, but your program will bomb on you since total pushed parameters are not correct.

The prototype for CreateWindowEx is:
CreateWindowExA PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD

This tells the Assembler, that anytime it sees:
    invoke  CreateWindowEx

Make sure 12 parameters are pushed onto the stack, if not, warn me. Also, make sure all parameters are DWORDs (99% of the parameters in 32 bit windows are DWORDs.)

Quote

Here is what I came up with so far for Strnicmp
INVOKE Strnicmp, strA DWORD, strB DWORD, nBytes DWORD

Means absolutely nothing without seeing the code for Strnicmp. What is nBytes for? The count of different characters, or the string length? If it is the count of different characters, the normal way would be to return in a register (EAX is used for return values if you are interfacing with another language, but you could return in any GP register for your program), if it is for the string length, you could just test for ASCII 0, since most strings you come across are NULL terminated. Unless you are dealing with non NULL terminated strings.


Quote

The book I have been reading

Er, what book?

Quote

but I am still confused...

About what? What exactly are you confused about?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1