'Exception System.Drawing.dll

I'm making a calculator on Windovs Forms in VS 2010. The task is to make computational commands in Assembler. To do this, I use a mathematical coprocessor. After clicking on "=", an answer appears, but if the cursor is removed from the button, the application immediately crashes and crashes with this exception:

"Unhandled exception of type "System.Runtime.InteropServices.ExternalException" occurred in System.Drawing.dll Additional information: A general error occurred in GDI+."

Most likely, the problem is in the coprocessor, if you do it without it, then everything is OK.

Code

#include "stdafx.h"
#include "Form1.h"

#include <stdio.h>
#include "locale.h"


using namespace Kursovaya;


void main(array<System::String ^> ^argv)
{
    
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    Application::Run(gcnew Form1());
    

}

Code snippet responsible for processing clicks on "=" `private: System::Void button21_Click_1(System::Object^ sender, System::EventArgs^ e) { double res; b=System::Convert::ToDouble(textBox1->Text);

    switch(actions){
        case 1:
               // res=one(a,b);
                
                textBox1->Text = System::Convert::ToString(one(a,b));
                textBox2->Clear();
                break;
        case 2:
               
                //result = a - b;
                textBox1->Text = System::Convert::ToString(two(a,b));
                textBox2->Clear();
                break;
        case 3:
               
                result = a * b;
                textBox1->Text = System::Convert::ToString(result);
                textBox2->Clear();
                break;
        case 4:
                
                result = a / b;
                textBox1->Text = System::Convert::ToString(result);
                textBox2->Clear();
                break;

        default:
           break;
    }
    }`

Assembler code P.S. I know that you can't write code in a file, but I tried through .cpp connecting it to .h, nothing works.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include "StdAfx.h"
#include "one.h"

double one(double a, double b) {
    double result;
    _asm{
        
        fld a
        fld b
        fadd st(0),st(1)
        fst result
    }
    
    return result;
}

At first I tried to just write the code in a file .h, but nothing happened, he swears at the mathematical coprocessor, if you do it through the usual Assembler ADD and MOV, then everything works. I don't know what the problem is, thanks in advance.



Solution 1:[1]

Everything turned out to be much easier than it seems, you just need to empty the stack and add two lines of code

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include "StdAfx.h"
#include "one.h"

double one(double a, double b) {
    double result;
    _asm{
        
        fld a
        fld b
        fadd st(0),st(1)
        fst result
        FFREE ST(0)
        FFREE ST(1)
    }
    
    return result;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gry