2

I am interested in doing minimal assembly programming on Windows. No GUI, just a plain old .exe program that will print a few characters to the console. In order to see if it was even possible to simply compile and run assembly on Windows, I tried the following:

test.s:

ret

command line:

gcc test.s

And I got the following error:

In function 'main': undefined reference to 'WinMain'

So the question is how do I go about compiling a simple assembly do-nothing program to a Windows executable? Do I need to define WinMain in every program? Is it a flag to gcc?

I don't really care which assembler I use, but if someone knows if this is the same of different for gcc(gas), masm and other assemblers, that would be nice, too.

Baruch
  • 20,590
  • 28
  • 126
  • 201

1 Answers1

4

You say you want pure or clean assembly, is this along those lines?

taken from The Assembly Programmers Master Book
language masm32 assembler,linker ml,link

source;

.586P
; Flat memory model
.MODEL FLAT, STDCALL
;---------------------------------------
; Data segment
_DATA SEGMENT

_DATA ENDS
; Code segment
_TEXT SEGMENT
START:
    RET  ; Exit
_TEXT ENDS
END START

assembly & linking;

ML /c /coff PROG1.ASM
LINK /SUBSYSTEM:WINDOWS PROG1.0BJ

This program was literally called "Do Nothing" but it is pure masm that just does nothing.

If you want to program in windows without the help of C you will have to program with the WIN32 api, you can find a reference to this api on the net called win.hlp win32.hlp, it would appear to be on this web-page

Perhaps a handy link -> MASM 6.1 Documentation


EDIT: Using as (gas) & ld;

.globl _start

_start: // ld looks for _start by default
    movl $22, %eax // AT&T syntax
    ret

compile;

c:\Users\James\Desktop\asm\>as file.s

c:\Users\James\Desktop\asm\>ld a.out

c:\Users\James\Desktop\asm\>a.exe

c:\Users\James\Desktop\asm\>echo %errorlevel%
22

Disassembly of section .text:

00401000 <_start>:
  401000:       b8 16 00 00 00          mov    $0x16,%eax
  401005:       c3                      ret
  401006:       90                      nop
  401007:       90                      nop

Simply putting ret is not going to get you far, you can get the assembly listings of your c/c++ programs by compiling with the -S flag (-masm=intel gives you intels syntax).

That said you need to specify an entry point for the linker to know where the code begins, you also need to create a text segment for the code, here is a really really simple example that returns 1;

    .text
    .intel_syntax noprefix
    .globl _main

_main: // entry point!
    mov eax, 1
    ret

gcc [file].s yeilds a.exe

James
  • 1,009
  • 10
  • 11
  • 1
    Also, compiling with -S will give me assembly that needs to link with the C standard library. I want clean assembly – Baruch Jan 22 '14 at 18:30
  • I don't know how you got errors, i just compiled that code on two different computers under both linux and windows??? – James Jan 22 '14 at 19:00
  • I tried it on Windows and got that error. Which gcc are you using? – Baruch Jan 22 '14 at 20:25
  • typing in `gcc --version` output `gcc (rev2, Built by MinGW-builds project) 4.8.1 Copyright (C) 2013 Free Software Foundation, Inc.`, ill edit in another way using `as` & `ld` – James Jan 23 '14 at 02:07