flat assembler
Message board for the users of flat assembler.

Index > Main > I found strange asm x86 output code

Thread Post new topic Reply to topic

Joined: 21 Apr 2012
Posts: 1724
Roman 26 Dec 2018, 15:24
_src$ = 8
mov     eax, DWORD PTR _src$[ebp]

I always thinked DWORD PTR mean DWORD
But in this case i think code look like this:
mov     eax, DWORD [ebp+_src$]
Post 26 Dec 2018, 15:24
View user's profile Send private message Reply with quote
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 26 Dec 2018, 16:04
Roman wrote:
_src$ = 8
mov     eax, DWORD PTR _src$[ebp]
This is a traditional syntax of eldest x86 assemblers like Intel ASM86 and then MASM. The DWORD PTR means that you access a DWORD... Well, one would think that PTR part means access through a pointer and therefore implies a memory operand, but with MASM this is not always the case (please read on). I think that this was what it originally meant, though - it seems that ASM86 defined it as such.

As for "_src$[ebp]", it is a syntax borrowed from C - this is a pointer arithmetic expressed analogously to how C language allowed it, with square brackets being a fancy way to write down an addition of an offset to a pointer. You could even add multiple terms in separate sets of square brackets there.

Back in the day, many people disliked this C-originated syntax in assembly language. Perhaps because they preferred languages more in spirit of Pascal, but much more likely because they simply disliked the messiness and lack of consistent indication when an operand is a memory. Sometimes it could be signaled by use of square brackets (in case of plain operands like [BX]), but usually not. You can see an opinion on some flaws of this syntax preserved in NASM documentation. The worst sin of MASM's syntax was that presence of PTR keyword was not a guarantee that operand would be a memory access, seemingly contrary to any logic. For many of us this was a mark of a truly horrible syntax.

This reaction led to development of alternative syntaxes, notably an Ideal mode of Borland's TASM assembler (a mode that you could select instead of default MASM-compatible one). It was a syntax that required all memory operands to use square brackets, and they had to wrap around entire operand. The square brackets would no longer be a C-like pointer addition operator, they would be solely an indication of x86 memory operand. In TASM's Ideal mode the above instruction would look like:
mov     eax, [DWORD _src$+ebp]    
Note that PTR keyword is no longer needed, since the brackets are already enough to signify a memory addressing. But TASM still allowed to put a redundant PTR in there, perhaps to allow for more familiarity to people used to MASM-compatible syntax.

Then came assemblers like NASM and my FASM, that evolved this concept further, moving the size override outside of square brackets. Idea was that size operator could precede any kind of operand and therefore should come first. This allowed a size override inside square brackets to take on a different meaning, indicating the size of the address and not of the operand (something that could come in handy sometimes when developing on 386+ machines where a 67h prefix could be used to make an instruction use a different addressing mode that default).

Nevertheless, PTR keyword is implemented in FASM, too. Here it acts as an alternative to square brackets to specify memory operand. So you either use square brackets or PTR, but never both at the same time.
mov     eax, DWORD [ebp+_src$]
mov     eax, DWORD PTR ebp+_src$    ; both are correct for FASM, and mean the same    
Post 26 Dec 2018, 16:04
View user's profile Send private message Visit poster's website Reply with quote

Joined: 21 Apr 2012
Posts: 1724
Roman 26 Dec 2018, 16:52
Thanks !
Post 26 Dec 2018, 16:52
View user's profile Send private message Reply with quote

Joined: 10 Aug 2007
Posts: 789
Location: Adelaide
sinsi 26 Dec 2018, 23:24
To add to the MASM side, these are all legal
  mov     eax, DWORD PTR _src$[ebp]
  mov     eax, DWORD PTR [ebp+_src$]
  mov     eax, _src$[ebp]
  mov     eax, [ebp+_src$]

DWORD PTR is assumed, since you are loading EAX. If it's ambiguous you need the override
  mov _src$[ebp],0  ;will throw an error - 0 could be a byte/word/dword
  mov DWORD PTR _src$[ebp],0
Post 26 Dec 2018, 23:24
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.