0

I'm trying to run the following Hello Word example in x86 assembly under Windows:

global _main
extern  _GetStdHandle@4
extern  _WriteFile@20
extern  _ExitProcess@4

section.text
_main :
; DWORD  bytes;
mov     ebp, esp
sub     esp, 4

; hStdOut = GetstdHandle(STD_OUTPUT_HANDLE)
push - 11
call    _GetStdHandle@4
mov     ebx, eax

; WriteFile(hstdOut, message, length(message), &bytes, 0);
push    0
lea     eax, [ebp - 4]
push    eax
push(message_end - message)
push    message
push    ebx
call    _WriteFile@20

; ExitProcess(0)
push    0
call    _ExitProcess@4

; never here
hlt
message :
db      'Hello, World', 10
message_end :


But then I get the following error when trying to link the assembled .obj file:

error LNK2001: unresolved external symbol _GetStdHandle@4
error LNK2001: unresolved external symbol _WriteFile@20
error LNK2001: unresolved external symbol _ExitProcess@4
fatal error LNK1120: 3 unresolved externals


Source of my example: How to write hello world in assembler under Windows?

How to fix these? Or also, why isn't the example working for me?

Community
  • 1
  • 1
Marc.2377
  • 7,807
  • 7
  • 51
  • 95
  • possible duplicate of [http://stackoverflow.com/questions/20063224/linking-to-kernel32-lib-in-assembler](http://stackoverflow.com/questions/20063224/linking-to-kernel32-lib-in-assembler) – Ruslan Gerasimov Jun 26 '14 at 06:41

1 Answers1

0

When using a C compiler the standard libraries are typically linked to the code which is typically not done when calling the linker separately.

The three functions are located in "kernel32.dll" so you'll have to link against "kernel32.lib" (or "libkernel32.a" when using GNU tools).

Your entry point is "_main" so I assume you want to use the startup object files, too.

However this is not neccessary in your case so you may simply define "_main" as linker entry point and not link against the startup object files.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • Yeah, I'm calling the linker as this: `link /subsystem:console /nodefaultlib /entry:main hello.obj`. About your tip on kernel32.lib: It works, but is that a C standard library (or C++ maybe)? I was actually looking for a way to code Hello World in ASM "without the help of C functions" (as stated in the Source question I provided). – Marc.2377 Jun 26 '14 at 06:45
  • 1
    `kernel32` is not a C standard library. It's a Windows library that provides parts of the Windows API, which you will need to use if you want to interact with the OS from your application. – Michael Jun 26 '14 at 10:43
  • @Michael: In fact, my example said that would "go directly to the Windows API", I simply didn't know how to include it. Thank you very much. – Marc.2377 Jun 26 '14 at 18:19