In C. How to retrieve RETURN values from assembly functions? I always got 0

Started by extremed, April 11, 2005, 02:12:11 AM

Previous topic - Next topic

extremed

Hello. I have  a simply programs that sums two numbers in assembler. I also have a c program with a .h  file that must use that program.

But I always got as a result a 0.

This is my asm code
MyProc proc var1:REAL4, var2:REAL4
    LOCAL retval:REAL4
    PUBLIC MyProc
    finit
    fld var1
    fld var2
    fadd
    fstp retval
    fwait                ;<- is this really needed?
    mov eax, retval
    ret
MyProc endp



This is my .h code
#ifndef _FLOTANTE_H
#define _FLOTANTE_H

extern "C" unsigned long __stdcall __MyProc(float, float);

float MyProc(float f1, float f2);


#endif

and this is my C code

void sum(void)
{
     int ch;
     float f1;
     float f2;
     printf("Enter first number\n\n");
scanf("%d",&f1);
printf("Enter second  number\n\n");
scanf("%d",&f2);
     
     float total2;
     total2 = MyProc(f1, f2);
     printf("Total is: %d", total2);

while ( (ch = getchar()) != '\n' && ch != EOF) ;

printf("\n\nPress ENTER to continue.");
while ( (ch = getchar()) != '\n' && ch != EOF)
;
   if(ch=='\n' || ch==EOF)
   {
      printmenu();           
   }
   
}

hutch--

Note that this is NOT a C forum, if you want to post questions about C coding, do it in the specific subforum for that task.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

extremed


Brett Kuntz

MyProc proc var1:REAL4, var2:REAL4
PUBLIC MyProc

    LOCAL retval:REAL4

    fld var1
    fadd var2
    fstp retval
    mov eax, retval
    ret

MyProc endp



#ifndef _FLOTANTE_H
#define _FLOTANTE_H

extern "C" float __stdcall __MyProc(float, float);

#endif



void sum(void)
{
     int ch;
     float f1;
     float f2;
     printf("Enter first number\n\n");
scanf("%f",&f1); // need %f for real4
printf("Enter second  number\n\n");
scanf("%f",&f2);
     
     float total2 = __MyProc(f1, f2); // need the __ part because that is the name of the external function
     printf("Total is: %f", total2); // %f for real4

while ( (ch = getchar()) != '\n' && ch != EOF) ;

printf("\n\nPress ENTER to continue.");
while ( (ch = getchar()) != '\n' && ch != EOF)
;
   if(ch=='\n' || ch==EOF)
   {
      printmenu();           
   }
   
}


I think it should work now.

Scorpie

Try leaving the float you want on the FPU, i know VB works that way.

Eóin

Yeah I'm pretty sure you return floats and doubles on the stack, and have only the return value at TOS, nothing else.