LMS
LMS

Reputation: 4207

"The value of ESP was not properly saved across a function call." even with LEAVE

When attempting to call a method which is defined in assembly, I'm receiving the error "The value of ESP was not properly saved across a function call.", using Visual Studio 2012. Looking at other questions, a common factor was the mention that the assembly may not have the LEAVE instruction at the end of each label.

I am receiving this error, but with the following code, which does include the LEAVE instruction.

section .bss
    vs: resb 13 ; 12-byte vendor string + NULL char
    ns: resb 49 ; 48-byte proc. name + NULL char

section .text

global _meta_vendor
global _meta_procname

_meta_vendor:
    push ebp
    mov ebp, esp
    mov eax, 0h
    cpuid
    mov [vs], ebx
    mov [vs + 4], edx
    mov [vs + 8], ecx
    mov byte [vs + 12], 0h
    mov eax, vs
    leave
    ret

_meta_procname:
    push ebp
    mov ebp, esp
    mov eax, 80000002h
    cpuid

    mov [ns], eax
    mov [ns + 4], ebx
    mov [ns + 8], ecx
    mov [ns + 12], edx
    mov eax, 80000003h
    cpuid
    mov [ns + 16], eax
    mov [ns + 20], ebx
    mov [ns + 24], ecx
    mov [ns + 28], edx
    mov eax, 80000004h
    cpuid
    mov [ns + 32], eax
    mov [ns + 36], ebx
    mov [ns + 40], ecx
    mov [ns + 44], edx
    mov byte [ns + 48], 0h
    mov eax, ns
    leave
    ret

Function prototypes for those labels are then in a header file which contains only this:

#include <cstdint>

extern "C" {
    char* meta_vendor();
    char* meta_procname();
}

Any insight as to why I am receiving this error?

Note that, if I click "Continue" on the popup which appears, the expected values do appear.

Upvotes: 0

Views: 771

Answers (1)

LMS
LMS

Reputation: 4207

The problem was solved by adding push ebx and pop ebx as is shown below:

_meta_vendor:
    push ebp
    mov ebp, esp
    push ebx

    ; code...

    pop ebx
    leave
    ret

Upvotes: 2

Related Questions