News:

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

Complex expressions

Started by ezgar, June 06, 2011, 01:26:11 AM

Previous topic - Next topic

ezgar

Hi, im triying to learn assembler mainly for curiosity, i have experience in other lenguages like C, C++, Java and C#, but assembler is killing me, it just feels so weird lol, i think is a matter of time to get used to it, right? but well, that´s a completely  different subject; you know, im very used to used to the expressions that high levels lenguages let you make, like i don´t know, like this:



someVariable = ( function( bla, bla2, bla3 ) * someValue ) + someOtherValue / function2();



How do you translate that kind of expression in assembler? or something like this:



if ( ( anything <= something ) && ( variable1 != variable2 ) || !( function( function3() ) == function2( anotherThing, someOther, lastOne ) ) )
{
    //bla, bla, bla
}



It´s posible to make that kind of things with masm32 in a not so hard way? for example, i know theres a macro called .IF, but it let you make that kind of expressions? do you have to split those expressions in several steps, creating temporal variables or something like that?

Sorry, it´s just that i have programed for years in high level lenguages, and then you try assembler and bam! it´s like a completely different world :D

drizz

Hello,

Quote from: ezgar on June 06, 2011, 01:26:11 AM

someVariable = ( function( bla, bla2, bla3 ) * someValue ) + someOtherValue / function2();

Well, first explain how this expression is evaluated (feel free to use temporary variables in your explanation). If you programmed for years it shouldn't be a problem.
The truth cannot be learned ... it can only be recognized.

dedndave

 :bg
drizz - lol

there are "high-level" expressions in asm, as well - probably not as complex as C allows
i never use them - i prefer to use the lower-level instuctions   :P

drizz

Dave, I wasn't trying to be funny. I want him to answer his own question by himself.
The truth cannot be learned ... it can only be recognized.

hutch--

ezgar,

Drizz's approach is the right idea, you must break your code down into bits to do the calculation.


// turn this
someVariable = ( function( bla, bla2, bla3 ) * someValue ) + someOtherValue / function2();

// into something like this
rv1 = function2()
rv2 = someothervalue / rv1
rv3 = function(etc)


Then do the calculation.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tight_Coder_Ex

This is just one of the possibilities without using higher level constructs that are available in MASM and some other Assemblers

someVariable = ( function( bla, bla2, bla3 ) * someValue ) + someOtherValue / function2();


        push    bla3
        push    bla2
        push    bla
        call    function
        imul    someValue
        mov     someVariable, eax

        call    function2
        mov     ecx, eax
        mov     eax, someOtherValue
        xor     edx, edx
        div     ecx
        add     someVariable, eax

ezgar

Ok, thank you all, i know you can always split it into several steps but i was just hoping an easier way to do things, like some magical macro that make the dirty work for you lol (sorry, i just have a couple of days reading about masm32 and i don´t know what let you do and what don´t  :red); i have been playing around with the .IF macro and at least it let you make some complex things.

Thanks again :)

jj2007

Search the Forum for solving expressions - there are some threads dealing with your question.

MichaelW

ezgar,

A MASM high-level syntax version of your C statement can be not that different from the C. You cannot do direct memory to memory comparisons, so for each such comparison somewhere ahead of the high-level statement you must move one of the variables being compared into an appropriate register. You can use the MASM32 rv macro to effectively substitute EAX, loaded with the return value, for each of the function calls. For MASM a "!" is an operator, but not a logical-not operator, so you will need to change the sense of the equality that follows.

eschew obfuscation

qWord

Quote from: ezgar on June 06, 2011, 08:27:38 PMsome magical macro that make the dirty work
masm macros can be very powerful, as long as you do the dirty job of programming them   :lol
FPU in a trice: SmplMath
It's that simple!

jj2007

Quote from: MichaelW on June 07, 2011, 12:04:49 AM
For MASM a "!" is an operator, but not a logical-not operator

Michael,

Since this is the Campus, you might need to elaborate on that statement...
:bg

MichaelW

For MASM a "!" is the literal-character operator, used to force the assembler to treat the character following it as a literal character instead of a special character. So "!(expression)" will not have the desired not effect :bg
eschew obfuscation

jj2007

Michael,
That is true for using ! in strings (and can be confusing...), but the OP used if ( ( anything <= something ) && ( variable1 != variable2 )... - which seems a valid usage of the not operator.

MichaelW

This is the part of the original statement that I was referring to:

  !( function( function3() ) == function2( anotherThing, someOther, lastOne ) ) )
eschew obfuscation

jj2007

Hmmm....

Quoteinclude \masm32\include\masm32rt.inc

.code
start:
   .if
![/b]rv(HeapAlloc, rv(GetProcessHeap), 0, 1000)
      MsgBox 0, "Could not allocated heap mem, sorry", "Serious problem:", MB_OK
   .else
      invoke HeapFree, rv(GetProcessHeap), 0, eax
      MsgBox 0, "There is heap on your puter", "Hi", MB_OK
   .endif
   exit
end start