flat assembler
Message board for the users of flat assembler.

Index > Windows > Do I need to preserve EBX register in DLL?

Author
Thread Post new topic Reply to topic
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
Example code below preserve EBX register by push on entry and pop on exit.

Otherwise my C# code calling this DLL would crash. (Sorry no detailed error message could be provided at this moment)

I see example DLL code in FASM distribution does not mention the need to preserve EBX register, but at least I found it mentioned in an online article elsewhere.

Code:
proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
     mov     eax, TRUE
     ret
endp

proc AVX
     push ebx
     mov eax,1
     cpuid
     and ecx,18000000h
     cmp ecx,18000000h
     jne no_AVX
     xor ecx,ecx
     xgetbv
     and eax,110b
     cmp eax,110b
     jne no_AVX
     mov eax, 2
     pop ebx
     ret
no_AVX:
     mov eax, 1
     pop ebx
     ret
endp    


My example code in C#:

Code:
    public partial class Form1 : Form
    {
        [DllImport(@"C:\FASM\DLL\AVX\AVX.DLL")]
        private static extern uint AVX();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (AVX() == 1)
                textBox1.Text = "AVX Support: No";
            else if (AVX() == 2)
                textBox1.Text = "AVX Support: Yes";
            else 
                textBox1.Text = "AVX Support: Unknown";
        }
    }    
Post 19 May 2021, 21:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18625
Location: In your JS exploiting you and your system
revolution
The three main registers you can change:
EAX: the return value
ECX: don't care
EDX: don't care

All others you must not change: EDI, ESI, EBP, EBX, ESP return with the same value they had before.

But do take note that you can use your own calling convention if your DLL is used only by your code. The above only applies to things like the C (c-call) and Windows (standard call) calling conventions.
Post 19 May 2021, 21:36
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
revolution wrote:
The three main registers you can change:
EAX: the return value
ECX: don't care
EDX: don't care

All others you must not change: EDI, ESI, EBP, EBX, ESP return with the same value they had before.

But do take note that you can use your own calling convention if your DLL is used only by your code. The above only applies to things like the C (c-call) and Windows (standard call) calling conventions.


Thank you for the timely info!
Post 20 May 2021, 12:47
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.