#949 call library function from assembly

OTHER
closed
Danny Smith
gcc (462)
works-for-me
No_User_Response
2013-01-31
2006-06-14
Jan Kechel
No

The following code crashes under windows XP, while it
works properly with dos djgpp and linux gcc:

--- cut prog.c ---
#include <stdio.h>

extern void PrintStrings();

unsigned int Exported;

int main()
{
Exported = 0xdeadbeef;
PrintStrings();
return 0;
}
--- end cut prog.c ---

--- cut printstr.asm ---
[BITS 32] ; Default is
32-bit code
[GLOBAL PrintStrings] ; export label
[EXTERN printf] ; external procedure
[EXTERN Exported] ; external variable

[SECTION .data] ; data segment

VersionString db "Testversion 0.01",00h
PrintTemplate1 db "VersionString = '%s'", 0ah, 00h
PrintTemplate2 db "unsigned int Exported = 0x%08x",0ah,00h

[SECTION .text] ; code segment

PrintStrings:

push ebp
mov ebp, esp
push dword VersionString
push dword PrintTemplate1
; pass parameters
call printf ; call C function
push dword [Exported]
push dword PrintTemplate2
; pass parameters
call printf ; call C function
mov esp, ebp
pop ebp
ret
--- end cut printstr.asm ---

To build the program we used:
under linux:
$ nasm -felf printstr.asm
$ gcc -o prog printstr.o prog.c
$ ./prog
-> works fine

version-numbers under linux:
gcc version 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)
NASM version 0.98.38 compiled on May 17 2005

Tu build the program under windows and dos all exported
variables and external functions must have a leading
underscore (e.g. '_printf')
> nasm -fcoff printstr.asm
> gcc -o prog.exe printstr.o prog.c
> prog
-> crashes (general protection fault)

version-numbers under windows:
gcc -dumpversion: 3.4.2
NASM version 0.98

Discussion

  • Danny Smith
    Danny Smith
    2006-06-14

    Logged In: YES
    user_id=11494

    The fine manual says use
    nasmw -f win32

    if you want win32 objects.

    win32 objects havew PE COFF format, not standard COFF

    Danny

     
  • Danny Smith
    Danny Smith
    2006-06-14

    • labels: 103944 --> non-mingw
    • status: open --> closed-works-for-me
     
  • Earnie Boyd
    Earnie Boyd
    2013-01-31

    • labels: non-mingw -->
    • status: closed-works-for-me --> closed
    • resolution: --> works-for-me
    • category: --> No_User_Response
    • milestone: --> OTHER
     
  • Earnie Boyd
    Earnie Boyd
    2013-01-31

    • labels: --> gcc