MASM equivilent to _emit

I have a C++ function that contains one huge _asm block (over 1000 lines).  I'm trying to convert this C++ function to true assymbly so that I can compile it with the 64-bit MASM.

I'm running into some _emit commands and the MASM assymbler doesn't know what that is.  
LONG ExampeC_Function( LONG bitwidth )
    if( bitwidth > 10000 )
        return -1;
    mov    eax,bitwith
    and    eax,7
    add     esi,eax
    and     ecx,07H
    mov     ebx,[esi]
    _emit 0x0F
    _emit 0xCB
    mov      eax,ebx
    mov      lReturn,eax
    return lReturn;

Open in new window

LVL 16
Who is Participating?
AkenathonConnect With a Mentor Commented:
Try DB instead of _emit, see here
HooKooDooKuAuthor Commented:
Two questions:

1. Will DB place the BYTE into the source code like _emit is supposed to?  (I'll admit, I'm not sure what _emit is exactly supposed to do, or why the programmer that wrote the code is using _emit).

2. I would like to leave the asm code as untouched as possible.  Is there a way I can define a MACRO that will automatically convert the _emit 0x0F into DB 0x0F?  I tried the following, but it failed:

_emit MACRO insert_byte
  DB insert_byte
HooKooDooKuAuthor Commented:
I tried to replace "_emit 0x0F" with "DB 0x0F" in the middle of source code, and I get the error "missing operator in expression".
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

1. Yes... actually the docs for _emit say that it does just the same as DB... look here

2. Your syntax for the macro looks fine, though I don't know whether macro names can start by an underscore. Here are a couple of nice macro links:

3. (!) Try giving a label name before DB. Instead of "db 8", say "somelabel db 8"

If you really really want to dig deeper into this... see here
HooKooDooKuAuthor Commented:
I've already discovered one problem... I've got C++ hex values rather than MASM hex values.  That now gets the code to at least compile.

I had already found the Microsoft documentation... and noticed the word "resembles" NOT "does the same as".

The deeper part didn't really seem to go much deeper (as it seemed to be more of a discussion of .code v .data).  However at least one poster stated "Generally, you can have a db anywhere you like; it merely "defines a byte" at that point in code." which SOUNDS like DB will do the same as _emit (i.e. place values in the middle of the code).

So if I understand this correctly, I could write hand-assymboled code in MASM where I enter all the op codes with either _emit or DB commands rather than use Assymbly numonics (i.e. rather then enter the command "inc cx", I could accomplish the same thing with "DB 041h".)
Exactly. If you don't like mnemonics, you can use the opcodes. That would be taking a leap from an assembly programmer to a machine code programmer... as in the old times!

And yes, DB throws the bytes you want where you say. That's why they were ranting about placing arbitrary bytes on a code section -they could be interpreted as code and executed.

Now... why do you think the original programmer used it? My guess is that the original compiler did not recognize the assembler instruction he wanted, so he had to resort to using _emit to get the corresponding opcodes in the code. Your best move is to "migrate" those opcodes to the actual assembler mnemonics they represent, so if you find e.g. "_emit 0x41" you ought to replace it with "inc cx" :-)
HooKooDooKuAuthor Commented:
Thanks for all the feedback.

I've been able to execute the code... and at least the end results are the same as the original code that used the c++ wrapper function around an _asm block.

I'm going to follow up to see what these opcodes are that the original programmer put in there.
Found it, it's "BSWAP ebx", which is 100% consistent with the sandwiching commands:

    mov     ebx,[esi]
   bswap ebx
    mov      eax,ebx

It "Changes the byte order of a 32 bit register from big endian to little endian or vice versa", so if EBX has 0x01020304, it will turn it into 0x04030201 and vice versa :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.