News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Can you Figure out the Syntax?

Started by why06, February 02, 2010, 11:10:54 PM

Previous topic - Next topic

why06

Pretend like its a game. Haha...  :lol
It's not MASM, that's for sure.

PUBLIC @MyFunction@8
_TEXT SEGMENT
_y$ = -8 ; size = 4
_x$ = -4 ; size = 4
@MyFunction@8 PROC NEAR
; _x$ = ecx
; _y$ = edx
; Line 4
push ebp
mov ebp, esp
sub esp, 8
mov DWORD PTR _y$[ebp], edx
mov DWORD PTR _x$[ebp], ecx
; Line 5
mov eax, DWORD PTR _y$[ebp]
imul eax, 3
mov ecx, DWORD PTR _x$[ebp]
lea eax, DWORD PTR [eax+ecx*2]
; Line 6
mov esp, ebp
pop ebp
ret 0
@MyFunction@8 ENDP
_TEXT ENDS
END


I don't know what syntax it is. My wikibook Im using to learn disassembly,which I really am more interested in the assembly (don't hate me), uses this syntax. I think its from cl.exe, but I haven't compiled a programmed from command line in my life, unless u want to count a makefile. plus I just wanted to come back here again, to not become a stranger.

EDIT: Hmmmm just occured to me I might have posted this in the wrong place... =/

qWord

Quote from: why06 on February 02, 2010, 11:10:54 PM
It's not MASM, that's for sure.
it is!

however, didn't know before, that masm accept such things like: DWORD PTR _y$[ebp]
FPU in a trice: SmplMath
It's that simple!

why06

Quote from: qWord on February 02, 2010, 11:22:22 PM
Quote from: why06 on February 02, 2010, 11:10:54 PM
It's not MASM, that's for sure.
it is!

however, didn't know before, that masm accept such things like: DWORD PTR _y$[ebp]

Really?! Well I'll say. o_O Shows how much I know. haha  :lol

well do you know what the little $ things mean? I don't get it.

qWord

in this context it is nothing more than a character of constant name. The '$' get only a special meaning if it stands alone: then it the location counter in current segment (code,data...).
FPU in a trice: SmplMath
It's that simple!

why06

Quote from: qWord on February 02, 2010, 11:35:36 PM
in this context it is nothing more than a character of constant name. The '$' get only a special meaning if it stands alone: then it the location counter in current segment (code,data...).

Hmmmmm? So its like an offset from the ebp?

BTW: nice fractal! Couldn't resist. Im also a fan of fractal art.  :thumbu

qWord

Quote from: why06 on February 02, 2010, 11:42:32 PMHmmmmm? So its like an offset from the ebp?
no, it is part of the constants name - you can delete it and there will be no difference.

Quote from: why06 on February 02, 2010, 11:42:32 PMBTW: nice fractal! Couldn't resist. Im also a fan of fractal art.  :thumbu
  :bg I've used a program called Fractalizer
FPU in a trice: SmplMath
It's that simple!

why06

Quote from: qWord on February 02, 2010, 11:59:42 PM
no, it is part of the constants name - you can delete it and there will be no difference.
Ok. I guess its just a style thing. How bout this mov DWORD PTR _y$[ebp], edx
Quote from: qWord on February 02, 2010, 11:59:42 PM
  :bg I've used a program called Fractalizer
Cool. I will check that out  :wink

dedndave

well - "DWORD PTR" isn't necessary in that case, but it doesn't hurt anything
it just tells the assembler the size of the data item at _y$[ebp] is a dword
because the source operand is a register, it already knows the size

if it were an immediate constant, the DWORD PTR would be needed

        mov     _y$[ebp],6

the assembler doesn't know whether the destination operand is a byte, a word, or a dword, so you have to tell it

        mov dword ptr _y$[ebp],6

why06

Hmmmm.. well yes that is important too I suppose. Though Im not sure, if it is specified specifically when you say view the same line in a debugger. I think it is, but may its not necessary in that case either... idk. In anycase though that wasn't what I need help on. =/

_y$[ebp]

Im not sure what that means. That's what I really need to know.

qWord

Quote from: why06 on February 03, 2010, 01:30:57 AM_y$[ebp]
this results in:
_y$ = -8
...
mov DWORD ptr [ebp+(-8)],edx

you will get the same by writing

mov DWORD ptr [ebp+_y$],edx
or
mov DWORD ptr ebp[_y$],edx
FPU in a trice: SmplMath
It's that simple!

dedndave


        mov     [ebp-8],edx

that means the value in the ebp register is an address
in this case, it is an address on the stack

        push    ebp         ;save EBP
        mov     ebp,esp     ;set EBP to the current stack pointer (called the base pointer)
        sub     esp,8       ;make room on the stack (under ebp) for 2 dwords

now, you have space reserved on the stack for 2 temporary local dword variables, one at address [ebp-4], and one at [ebp-8]
they use these 2 names to reference them

_y$ = -8 ; size = 4
_x$ = -4 ; size = 4

the routine calculates 3*y+2*x and returns the result in EAX
a little sloppy - lol - i wouldn't use a subroutine for that - maybe a macro

why06

Thank you both, really appreciate the help.

Quote from: dedndave on February 03, 2010, 02:07:29 AM
the routine calculates 3*y+2*x and returns the result in EAX
a little sloppy - lol - i wouldn't use a subroutine for that - maybe a macro

And yeh I see what you mean, but it was just meant to be an example.

offtopic: man that fractal program gets slow, when you start increasing the finesteps! nearly killed my video driver o_O

hutch--

why,

Its just a CL.exe assembler output that has been cleaned up a bit. Take the start values like,


_y$ = -8 ; size = 4
_x$ = -4 ; size = 4


and replace each instance of _y$ with -8 etc .... then go back to each replacement and put the code inside the brackets "-8[reg] becomes [reg-8]" and it will both build in MASM and be readable.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

why06

Very nice explanation hutch. That was simple and I understood it perfectly :)

MichaelW

FWIW the C source would look something like this:

int __fastcall MyFunction ( int x, int y )
{
    return x*2+y*3;
}


The posted code would be the result of compiling with no optimizations. Using /O2 /G6 the compiler would reduce the procedure to just three instructions:

    lea eax, DWORD PTR [edx+edx*2]
    lea eax, DWORD PTR [eax+ecx*2]
    ret 0

eschew obfuscation