0

I am using Visual Studio 2013 and am attempting to write "raw" assembly. I'm just starting to learn assembly so I don't know too much about it but I'd like to write assembly if possible without wrapping it in C/C++ or any other languages.

I have a file main.asm with the following code that I am trying to get running. I am just looking to confirm that I can get an asm program running so that I can play with the code as I read how assembly works.

.MODEL FLAT
.code
    neg eax
    add eax,5 ;eax = eax -5
END

When compiling, I am getting the following errors:

Error   1   error LNK2001: unresolved external symbol _main 
Error   2   error LNK1120: 1 unresolved externals

How can I resolve this and get an understanding of what is occurring?

halfer
  • 19,824
  • 17
  • 99
  • 186
JimmyJims
  • 1
  • 1
  • 1
    Note that even if your code would link, it would most like crash after reaching the `add eax,5` because you didn't add code to shut down the process. The CPU doesn't know that your program ends there and continues executing whatever is in RAM after that. – fuz Jul 28 '18 at 16:45
  • If you don't need the _C_ runtime then after `.code` add a label `_main:` . After `add eax,5` add a `ret` and then change `END` to `END _main` – Michael Petch Jul 28 '18 at 18:34

1 Answers1

0

The CRT startup code wants to call a function called main (or _main in asm). You didn't provide one, so your program doesn't link.

Exactly the same error as if you compiled a C program that was missing a main function. Go look for some examples of MASM-syntax hello world, or of declaring a function. For example, How to write hello world in assembler under Windows? has a MASM answer (the question is tagged NASM, so most of the answers are for NASM, the other major flavour of intel-syntax assembly.)

Other hits on SO include x86 masm hello world, which manually calls the CRT init functions from a static executable, so that's more complex than you need if you're just writing the equivalent of a simple C program.


And/or look at compiler-generated asm output to see how it does it, with PROC / END. http://godbolt.org/ has MSVC installed, as well as gcc / clang (which emit GAS syntax)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847