assembly

This is a discussion on assembly within the Tech Board forums, part of the Community Boards category; what is wrong with this code. I don't know assembly and am not tyring to learn(yet atleast ), but I ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    assembly

    what is wrong with this code. I don't know assembly and am not tyring to learn(yet atleast), but I took this from a site and it gives me two errors. Could someone help please?
    Code:
    inline void FloatToInt(int *int_pointer, float f){
            __asm__("fld f");
            __asm__("mov  edx, int_pointer");
            __asm__("FRNDINT");
            __asm__("fistp dword ptr [edx]");
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    what are the errors?

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    /tmp/cch2cud4.s: Assembler messages:
    /tmp/cch2cud4.s:10: Error: too many memory references for `mov'
    /tmp/cch2cud4.s:12: Error: junk `ptr [edx]' after expression

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Perhaps its because that's written in the ASM for a VC++ compiler, and you're compiling it with gcc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Banned
    Join Date
    Dec 2003
    Posts
    367
    You have to be careful you are using the correct syntax for assembly. Unlike C/C++, assembly can vary. For inline assembly, you MUST know whether your compiler uses AT&T or Intel syntax. You MUST also know how to convert between the two.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It sounds a lot like a problem with using Intel syntax instead of at&t
    try:
    Code:
    __asm__("movl  int_pointer, %edx");
    as for
    Code:
    __asm__("fistp dword ptr [edx]");
    No idea, we didn't get into the floating point part of asm during my class.

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You might get away with using .intel_syntax noprefix (example) if you want to stick with the intel syntax and are working with the gnu assembler.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I changed what thantos said and it got rid of one error. I changed it back and tried with -masm=att and it got those two errors still. If I stick with the original code and compile with -masm=intel i get one error about the memory references for mov.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    using -masm=intel is the error occuring with my mov? if so change it back. my move is at&t syntax

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    ah screw it. Here is what I was trying to do test this and see if it was true or not, but if any of ya'll want to see it seemed pretty interesting.

  11. #11
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Modify this to suit:

    1. You need to declare variables with global scope for proper linkage, so, for example:
    Code:
    /*globals*/
    int *int_ptr __asm__ ( "int_ptr" );
    float floatval __asm__( "floatval" );
    2. Then in your FloatToInt fn, just doing it by assignment, ie.:
    Code:
    inline void FloatToInt(int *int_pointer, float f) 
    {
      int_ptr=int_pointer;
      floatval=f;
    
      __asm__("fld floatval");
      __asm__("mov  edx, int_ptr");
      __asm__("FRNDINT");
      __asm__("fistp dword ptr [edx]");
    
    }
    You'll probably want to rewrite the fn so it doesn't take any parameters and make other changes to use the global variables directly - I just did it this way as proof of concept.

    Used: gcc -o test.exe test.c -masm=intel (winxp, gcc 3.2.3)

    Output:
    10000000 sin and cos computed in 3781 ticks with standard math funcs
    10000000 sin and cos computed in 1125 ticks with fast[cos/sin]

    And change void main to int main in the original example, of course.

    ******************************
    edit: Just noticed the name 'Linuxdude' so, for linux:

    1. Remove conio.h, assert.h includes and getch(); in 'main' fn.

    2. You need to declare variables with global scope for proper linkage, so, for example:
    Code:
    /*globals*/
    /*with linux you don't need to use following syntax - normal declaration is sufficient*/
    int *int_ptr __asm__ ( "int_ptr" );
    float floatval __asm__( "floatval" );
    3. Then in your FloatToInt fn, just doing it by assignment, ie.:
    Code:
    inline void FloatToInt(int *int_pointer, float f) 
    {
      int_ptr=int_pointer;
      floatval=f;
    
      __asm__(".intel_syntax noprefix"); /*kills error: too many memory references for mov*/
      __asm__("fld floatval");
      __asm__("mov  edx, int_ptr");
      __asm__("FRNDINT");
      __asm__("fistp dword ptr [edx]");
    }
    You'll probably want to rewrite the fn so it doesn't take any parameters and make other changes to use the global variables directly - I just did it this way as proof of concept.

    Used(fedora, gcc 3.3.2): gcc -o test.exe test.c -masm=intel -lm

    Output:
    10000000 sin and cos computed in 4410000 ticks with standard math funcs
    10000000 sin and cos computed in 3450000 ticks with fast[cos/sin]

    (slower machine)
    Last edited by Ken Fitlike; 06-23-2004 at 07:30 AM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  12. #12
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    thanx. I of course changed the void to int and got rid of the asserts didn't see a need for them and I got rid of getch(). I am in a terminalthanx very much
    Last edited by linuxdude; 06-23-2004 at 03:27 PM.

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, 05:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03: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, 02: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, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM

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