When using the MOV mnemonic to load/copy a string to a memory register in MASM, are the characters stored in reverse order?

前端 未结 2 2057
灰色年华
灰色年华 2021-01-15 02:37

I want to know if using the MOV instruction to copy a string into a register causes the string to be stored in reverse order. I learned that when MASM stores a string into a

2条回答
  •  醉酒成梦
    2021-01-15 03:19

    See @RossRidge's answer for a very detailed description of how MASM works. This answer compares it to NASM which might just be confusing if you only care about MASM.


    mov ecx, 4 is four dwords = 16 bytes, when used with repne scasd.

    Simpler would be to omit rep and just use scasd.

    Or even simpler cmp dword ptr [strLetters], "dcba".

    If you look at the immediate in the machine code, it will compare equal if it's in the same order in memory as the data, because both are treated as little-endian 32-bit integers. (Because x86 instruction encoding uses little-endian immediates, matching x86's data load/store endianness.)

    And yes, for MASM apparently you do need "dcba" to get the desired byte order when using a string as an integer constant, because MASM treats the first character as "most significant" and puts it last in a 32-bit immediate.


    NASM and MASM are very different here. In NASM, mov dword [mem], 'abcd' produces 'a', 'b', 'c', 'd' in memory. i.e. byte-at-a-time memory order matches source order. See NASM character constants. Multi-character constants are simply right-justified in a 32-bit little-endian immediate with the string bytes in source order.

    e.g.

    objdump -d -Mintel disassembly
       c7 07 61 62 63 64       mov    DWORD PTR [rdi], 0x64636261
    

    NASM source: mov dword [rdi], "abcd"
    MASM source: mov dword ptr [rdi], "dcba"
    GAS source: AFAIK not possible with a multi-char string literal. You could do something like $'a' + ('b'<<8) + ...

    I agree with Ross's suggestion to avoid multi-character string literals in MASM except as an operand to db. If you want nice sane multi-character literals as immediates, use NASM or EuroAssembler (https://euroassembler.eu/eadoc/#CharNumbers)


    Also, don't use jcc and jmp, just use a je close to fall-through or not.

    (You did avoid the usual brain-dead idiom of jcc over a jmp, here your jz is sane and the jmp is totally redundant, jumping to the next instruction.)

提交回复
热议问题