-1

I am trying to work with Visual Studio 2015 for the first time for a class I am taking. I have used a few different tutorials and re-downloaded visual studio multiple times but am still having the same issue.

The comments at the top (first 2 lines) are being underlined with red and the error message says, "this declaration has no storage class or type specifier." I'm assuming I messed something up in the settings when I started messing with Visual Studio but have not found a solution all weekend. Has anyone seen this or have any ideas as to what could be going on?

Here is a copy of the sample code I was given to work with:

; AddTwo.asm - adds two 32 - bit integers.
; Chapter 3 example

.386
.model flat,stdcall
.stack 4096

ExitProcess proto,dwExitCode:dword
.code

main proc
    mov eax,5
    add eax,6               
    invoke ExitProcess,0
main endp
end main
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
A.Linden
  • 37
  • 4
  • 3
    That's not C++ code. How are you trying to compile it? – qxz Jan 30 '17 at 01:29
  • I'm sorry, I was not clear. It's assembly language code I am doing for the first time. All the setting instructions I've been given have related to c++ – A.Linden Jan 30 '17 at 01:36
  • You'll have to put that code in an assembly file and compile it as assembly instead of C++, then. – qxz Jan 30 '17 at 01:36

1 Answers1

0

Change file type to .asm, such as example.asm. Create an empty project and add the .asm file to the project. From the project window, right click on the .asm file name, then properties, then add these custom build steps:

debug build command line:

ml /c /Zi /Fo$(OutDir)\example.obj example.asm

release build command line:

ml /c /Fo$(OutDir)\example.obj example.asm

both debug and release build output file:

$(OutDir)\example.obj

Example asm file:

        .586p
        .model  flat,c
        .data           ;  initialized data
        .data?          ;uninitialized data
        .stack  4096
        .code
main    proc    near
        mov     eax,5
        add     eax,6
        xor     eax,eax
        ret
main    endp
        end

Note - if you are using Visual Studio 2015, printf is now part of an include file and effectively is inlined in a C / C++ file. This makes calling printf from asm problematic. One solution is to have a project with both a C / C++ and an asm file, using the C/C++ file to generate the printf.

rcgldr
  • 27,407
  • 3
  • 36
  • 61
  • 1
    Can't you just create a project, change the build dependencies/build customization and add the MASM target, and then add the ASM files to the project as needed? I'm not sure a custom rule is needed but I could be missing something. – Michael Petch Jan 30 '17 at 03:30
  • @MichaelPetch - it depends if you choose the default setting for .asm files when creating a project. In some cases it didn't work or I missed it, so I had to go back and manually enter the custom build command line and output files. Different versions of Visual Studio have have been an issue. I seem to recall that some of them did not default build .asm files, but maybe I missed something when creating a project. – rcgldr Jan 30 '17 at 03:34
  • Most times ASM aren't built by default - correct. In VS 2015 this is still true. You can create a new project, then right click the project and there is a "build dependency" item on the menu, then select "build customization" and select MASM in the list. If you add ASM files to the project after that they will automatically be assembled/linked. If you do the build dependency change procedure after adding ASM files then you need to right mouse click each ASM files properties and mark it as being part of the build (via item type and Microsoft Macro Assembler) – Michael Petch Jan 30 '17 at 03:37
  • You are right about `printf`. Sometime back I wrote an SO answer that may still apply: http://stackoverflow.com/a/33725587/3857942 . The other answer that may work (if you have the proper toolset installed) is http://stackoverflow.com/a/33724617/3857942 – Michael Petch Jan 30 '17 at 03:43