flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Return a structure to the Caller

Author
Thread Post new topic Reply to topic
penguinglx



Joined: 05 Aug 2007
Posts: 10
penguinglx 23 Aug 2007, 06:52
struct Vector
{
float x,y;
}

Vector Add(Vector a,Vector b); --> C++ prototype declaration

I wanna implement it in FASM but I dont know how to return the result to the caller , I cant just put the result in EAX or ST0 , what am I supposed to do now
Thanks
Post 23 Aug 2007, 06:52
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 23 Aug 2007, 10:02
Easiest solution:
If you are using G++ (GNU compiler for C++) do this ...
g++ -S yourfile.cpp
and then have a look at yourfile.s to know how G++ did it.

NOTE: just put the bare bones in your source file.
e.g. put this:
Code:
struct Vector { float x; float y; }
Vecrot foo(Vector a, Vector b) {
 Vecrot c;
 c.x = a.x +  b.x;
 c.y = a.y + b.y;
 return c;
}
    
You can find out mostly everything about HLL features using this method.
If you do not use g++ then you have to find out how to ask your compiler to generate assembly output.

_________________
Code:
 o__=-
 )
(\
 /\  
    
Post 23 Aug 2007, 10:02
View user's profile Send private message Reply with quote
penguinglx



Joined: 05 Aug 2007
Posts: 10
penguinglx 24 Aug 2007, 05:05
Probably u got me wrong

I meant I can implement that function in assembly language ,but how to return the result

Let me make it clear

Module 1:
----------------
Code:
#include <iostream.h>
extern Vector Add(Vector a,Vector b);

void main()
{
        Vector Vector1,Vector2,Vector3;
        Vector3=AddVector(Vector1,Vector2);
        cout<<Vector3.x<<" "<<Vector3.y;
}

    


Module 2:
------------------
Code:
Fasm ..... all stuff blah blah....

AddVector:
          push ebp
          mov ebp,esp
          finit
          ;------
          fld dword [ebp+8]   Vector1.x
          fadd dword [ebp+16]  x + x
          fld  dword [ebp+12]  Vector1.y
          fadd  dword [ebp+20] y+y
          
           OK st0 = y+y  ,st1= x+x       
          What do I need to do here to return both values above to vector3 in the Caller

          pop ebp
          retn    
Thanks
Post 24 Aug 2007, 05:05
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 24 Aug 2007, 05:32
Probably you got me wrong too!
Post 24 Aug 2007, 05:32
View user's profile Send private message Reply with quote
penguinglx



Joined: 05 Aug 2007
Posts: 10
penguinglx 24 Aug 2007, 05:36
OK I get u right now , Ill do it
Post 24 Aug 2007, 05:36
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 24 Aug 2007, 05:42
Code:
// My source.
struct Vector
{
        float x;
        float y;
        float z;
        float w;
};

Vector foo(Vector a, Vector b)
{
        Vector c;

        c.x = a.x +  b.x;
        c.y = a.y + b.y;
        c.z = a.z + b.z;
        c.w = a.w + b.w;

        return c;
}

// Out put from g++.

  .file   "vec.cpp"
 .text
       .align 2
.globl __Z3foo6VectorS_
 .def    __Z3foo6VectorS_;       .scl    2;      .type   32;     .endef
__Z3foo6VectorS_:
     pushl   %ebp
        movl    %esp, %ebp
  movl    8(%ebp), %eax
       flds    12(%ebp)
    fadds   28(%ebp)
    fstps   (%eax)
      flds    16(%ebp)
    fadds   32(%ebp)
    fstps   4(%eax)
     flds    20(%ebp)
    fadds   36(%ebp)
    fstps   8(%eax)
     flds    24(%ebp)
    fadds   40(%ebp)
    fstps   12(%eax)
    popl    %ebp
        ret     $4
    

See that!
There no simple way to return a structure, so g++ just invoked the function "foo" with
address which is then modified to contain the structure values. Which we suppose it
is returning from function.
Got it?
No, take a deeper look.

_________________
Code:
 o__=-
 )
(\
 /\  
    
Post 24 Aug 2007, 05:42
View user's profile Send private message Reply with quote
penguinglx



Joined: 05 Aug 2007
Posts: 10
penguinglx 24 Aug 2007, 06:19
thanks Alot I got it Very Happy sorry Very Happy
Post 24 Aug 2007, 06:19
View user's profile Send private message Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 244
Location: US
Yardman 24 Aug 2007, 08:05
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 02:46; edited 1 time in total
Post 24 Aug 2007, 08:05
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 24 Aug 2007, 09:52
gud copy Yardman Wink
that what g++ is doing in the above example (posted by me) Laughing

_________________
Code:
 o__=-
 )
(\
 /\  
    
Post 24 Aug 2007, 09:52
View user's profile Send private message Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 244
Location: US
Yardman 24 Aug 2007, 11:03
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 02:46; edited 2 times in total
Post 24 Aug 2007, 11:03
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 24 Aug 2007, 11:22
Yardman is a nice guy and good human being!
Yardman is a nice guy and good human being!
Yardman is a nice guy and good human being!
Yardman is a nice guy and good human being!
Yardman is a nice guy and good human being!


Last edited by 0.1 on 25 Aug 2007, 04:09; edited 1 time in total
Post 24 Aug 2007, 11:22
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 24 Aug 2007, 11:28
Yardman is a good human being and nice guy!
Yardman is a good human being and nice guy!
Yardman is a good human being and nice guy!
Yardman is a good human being and nice guy!
Yardman is a good human being and nice guy!


Last edited by 0.1 on 25 Aug 2007, 04:10; edited 1 time in total
Post 24 Aug 2007, 11:28
View user's profile Send private message Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 244
Location: US
Yardman 24 Aug 2007, 14:50
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 02:46; edited 1 time in total
Post 24 Aug 2007, 14:50
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 25 Aug 2007, 04:08
Yardman wrote:
Now, I recall Vid giving
you a short quiz that you seem to have passed.
But perhaps an additional question should have
been asked for you to explain the difference
between "call by value" and "call by reference".

I know the difference. Do not kid me Smile

Yardman wrote:

After studying the GAS assembly for sometime
(more than I wanted to; it's weird), I can see
that eax is being used as a pointer to somewhere.
And that "somewhere" is the return structure of the
function call. That I will agree with you on,
provided you agree that my example was not the GAS
generated code.

I agree, you agree, we both agree Smile

_________________
Code:
 o__=-
 )
(\
 /\  
    
Post 25 Aug 2007, 04:08
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-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.