Assembly trouble (not too complicated)

This is a discussion on Assembly trouble (not too complicated) within the A Brief History of Cprogramming.com forums, part of the Community Boards category; i had an assembly macro that i wrote that seems to compile at random. by that i mean that some ...

  1. #1
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    Assembly trouble (not too complicated)

    i had an assembly macro that i wrote that seems to compile at random. by that i mean that some places i put it will compile fine and work beautifully, and other places i put it wont compile at all. worst off all, they give really strange compiler errors.

    i kept cutting things out to narrow down on the problem in one of the spots that it doesnt compile, and i ended up with this much non-compiling code:

    Code:
    __asm mov eax, val
    this single, innocent line of code generates 6 compiler errors, all saying the same thing:

    error C2400: inline assembler syntax error in 'second operand'; found 'newline'
    i can copy and paste this exact line of code to a different spot in my program and, after changing 'val' to something valid, it will happily compile.


    while writing this post, i have figured out what causes the error, but i still dont understand why it does it. what i have discovered is this:

    if the variable that i am putting in the register is a function argument, it wont compile. why can i not put function arguments in registers, but i can put any other type of variable in them?
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    error C2400: inline assembler syntax error in 'second operand'; found 'newline'
    The error you got seemed to indicate a newline problem :S I don't know what that has to do with arguments to functions...

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    C or C++

    I know in C++ you have to worry about name mangling.

  4. #4
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    its C++, but it only happens to function arguments. the name mangling-should apply equally to all variables.

    does this have something to do with the stack? from what i understand, you can access stack elements the same way you access normal memory if you have the address of them. i didn't think that would be a problem. and the newline thing doesnt seem to be sensical. like i said, i can copy and paste that exact line of code and have it use a non-argument variable and it works perfectly fine, so there is nothing wrong with the code itself from what i can tell.
    I came up with a cool phrase to put down here, but i forgot it...

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Try placing a semicolon at the end of that statement. Its best to use a block:
    Code:
    __asm {
       /* .. */
    }

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Any references to local variables should be placed in brackets.

    And yes avoid single line inline assembly statements using __asm.


    Here is a sample:

    Code:
    void Copy32(DWORD *Source,DWORD *Dest,DWORD size)
    {
      asm {
         push          ds
         lds             esi,[Source]
         les             edi,[Dest]
         mov           ecx,[size]
         rep            movsd
    
         pop           ds
      }
    }
    You compiler is not parsing the line correctly so it is also not invoking the inline assembler. Since the compiler cannot compile the line it sees the end of line or new line character at the end of it and that is prob where your error is coming from. It's basically saying it cannot compile the line prior to the newline it has encountered.

  7. #7
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    block, single line... it makes no difference.

    i see what you are saying, but you are missing the point. it always fails whenever i am using a function argument, and only then.

    this code:

    Code:
    __asm
    {
    	mov eax, val
    }
    generates the same error as this code:

    Code:
    __asm mov eax, val
    or even this one:

    Code:
    {
       __asm
       {
    	mov eax, val
       }
    }
    and both lines of code work perfectly fine if val is not a function argument, be it local, global, or otherwise, and placing semicolons after the single line or the ending brackets does not fix the problem.

    in summation:
    it is only function arguments that cause this problem, and not the format of the line of code itself. the question still remains: why?
    Last edited by ...; 03-05-2004 at 04:49 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Which compiler and which assembler

  9. #9
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    oh sorry, i should have mentioned that before...

    MSVC++.NET
    I came up with a cool phrase to put down here, but i forgot it...

  10. #10
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    I assume it is probably because of the stack. The inline assembler code might not know where to get the function arguments by itself.

    The stack should look something like this:


    local var
    local var
    local var
    ...however many local vars you have...
    return address
    return link
    free address used for return value
    parameter 1
    parameter 2
    parameter 3
    ...however many parameters you have...
    My Website

    "Circular logic is good because it is."

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I'm lost because val in your examples is not part of a function argument.


    Code:
    ...
      mov    eax,val1
      push   eax
      mov    eax,val2
      push   eax
      call Add
    ...
    ...
    ...
    
    Add:
      push     ebp
      mov      ebp,esp
      
      ;The following line(s) will be correct only with certain calling 
      ;conventions - compilers will allow you to change the calling
      ;convention to be used
      mov     eax,[ebp+8]  ;first passed parameter resides at ebp+8
      add     eax,[ebp+12] 
      
      pop     ebp
      ret                             ;same as return(val1+val2)

    That is using val1 and val2 as function arguments and/or parameters.

    mov eax, val

    should work on any inline assembler provided that val is a valid variable.

    mov eax,[immediate value 8/16/32] is perfectly valid in any x86 assembler provided the immediate value is of an integral type.

    Post the entire section and perhaps we can help. Sometimes semi-colons in the wrong place or misplaced parentheses can spit out some strange errors. I don't think the error is in your assembly code.

  12. #12
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    hum... wait a minute...

    i just changed the variable i was using from "high" to "high_var" and it compiles now... :-\

    is "high" a keyword or something? this may end up being a dumber problem than i previously thought...
    I came up with a cool phrase to put down here, but i forgot it...

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    High probably extracts the high order bits from a WORD or DWORD and is probably pre-defined in your inline assembler. Look at the keywords that have been used in your inline assembler and you can answer this question.

    But you have not given us enough info. All I see in your code examples is var, not high. Where did high come from?

  14. #14
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    it was just a simple function to check between two variables and swap them if the high one was lower than the low one.

    its fixed now, though, thanks anyways...
    I came up with a cool phrase to put down here, but i forgot it...

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Aargh, when I first read this post I entered "masm reserved words" into Google. "val" wasn't in the list. :-(

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 04:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 02:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21