Search code examples
assemblyx86masm

"end main" directive breaks a MASM Hello World, works fine with "end"?


I was debugging my hello world program, and got frustrated so I straight up copied and pasted the example from this website into visual studio and ran it.

It worked. I then took an extra thirty minutes trying to find out what the bug was that broke it. Turns out it's the last line. The below code doesn't work, and the only thing that is changed was the end main at the end of the file to end.


So this assembles, but doesn't work, using end main

; requires /coff switch on 6.15 and earlier versions
.386
.model small,c
.stack 1000h

.data
hello     db "Hello world!",0

.code
    includelib libucrt.lib
    includelib legacy_stdio_definitions.lib
    includelib libcmt.lib
    includelib libvcruntime.lib

extrn printf:near
extrn exit:near

public main
main proc
        push    offset hello
        call    printf
        push    0
        call    exit
main endp

end main

but this version does work, using just end

; requires /coff switch on 6.15 and earlier versions
.386
.model small,c
.stack 1000h

.data
hello     db "Hello world!",0

.code
    includelib libucrt.lib
    includelib legacy_stdio_definitions.lib
    includelib libcmt.lib
    includelib libvcruntime.lib

extrn printf:near
extrn exit:near

public main
main proc
        push    offset hello
        call    printf
        push    0
        call    exit
main endp

end

All I heard was that it was an optional step you can take to tell the program where the code starts. Additionally, why does it even break the program?


Solution

  • You don't want "end main" because you don't want your entry point to be main because this is a libc program and you want the C library up and running before main is called.

    You aren't writing start.asm yourself; don't set "end main" like you would do with a libc-less assembly program.