views:

4201

answers:

5

Hi I wanted to write something basic in assembly under windows, I'm using nasm, but I can't get anything working.

How to write and compile hello world without help of c functions on windows?

thx

+2  A: 

NASM examples.

; ----------------------------------------------------------------------------
; helloworld.asm
;
; This is a Win32 console program that writes "Hello, World" on one line and
; then exits.  It needs to be linked with a C library.
; ----------------------------------------------------------------------------

    global _main
    extern _printf

    section .text
_main:
    push message
    call _printf
    add  esp, 4
    ret
message:
    db 'Hello, World', 10, 0

Then run

nasm -fwin32 helloworld.asm
gcc helloworld.obj
a

There's also The Clueless Newbies Guide to Hello World in Nasm without the use of a C library. Then the code would look like this.

org 100h
mov dx,msg
mov ah,9
int 21h
mov ah,4Ch
int 21h
msg db 'Hello, World!',0Dh,0Ah,'$'

Good luck.

anderstornvig
The question explicitly mentions "without using C libraries"
Mehrdad Afshari
There's no reliable way to do this without calling a C function at some point. Except if by "C function" you mean "standard C function".
Bastien Léonard
Wrong. The C library itself obviously can, so it's possible. It's only slightly harder, in fact. You just need to call WriteConsole() with the right 5 parameters.
MSalters
Although the second example doesn't call any C library function it's not a Windows program either. Virtual DOS Machine will be fired to run it.
Romulo A. Ceccon
Yeah, the second example is one of the classics. Technically interrupt 0x21 is part of the DOS "API" - the interrupt pointed to a chunk of code installed into memory by DOS at startup, unlike the BIOS mapped interrupts which work even before an OS is loaded. Critical however for the ah=0x09 sub-function is that the string is terminated with $ (otherwise it just starts writing junk from memory). Alternatively you can also use the ah=0x40 function, for which you specify the number of characters (this function is also used to write to files, as the "screen" is just another pipe)
David
+15  A: 

This example shows how to go directly to the Windows API and not link in the C Standard Library.

    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:

To compile, you'll need NASM and LINK.EXE (from Visual studio Standard Edition)

   nasm -fwin32 hello.asm
   link /subsystem:console /nodefaultlib /entry:main hello.obj 
caffiend
+1 for the Win32 native way
Jonas Gulle
+2 for the Win32 native way!
Atømix
+3 for the Win32 native way!!
csl
A: 

Unless you call some function this is not at all trivial. (And, seriously, there's no real difference in complexity between calling printf and calling a win32 api function.)

Even DOS int 21h is really just a function call, even if its a different API.

If you want to do it without help you need to talk to your video hardware directly, likely writing bitmaps of the letters of "Hello world" into a framebuffer. Even then the video card is doing the work of translating those memory values into VGA/DVI signals.

Note that, really, none of this stuff all the way down to the hardware is any more interesting in ASM than in C. A "hello world" program boils down to a function call. One nice thing about ASM is that you can use any ABI you want fairly easy; you just need to know what that ABI is.

Captain Segfault
+1  A: 
PhiS
+1  A: 

Also check out Steve Gibson's Small Is Beautiful windows assembly starter kit.

Jeremy
+1 Security Now
BlueRaja - Danny Pflughoeft