user98188
user98188

Reputation:

What is wrong with this assembly program?

I am very new to assembly language programming, so it is probably a very obvious error, but...

I use MSVC++, and when I compile any project that has a file with a .asm extension, it uses the rule

NAME    EXTENSIONS    COMMAND LINE                                  RULE FILE
MASM    *.asm         ml.exe \c [All Options] [Additional Opti...   C:\Program Files\Microsoft Visual St...

And just to make sure the compiler works, I tried the code

main proc
  mov ax, 10
main endp
end main

But when I compiled it, I get three errors

.\compt1.asm(1) : error A2034:must be in segment block : main
.\compt1.asm(2) : error A2034:must be in segment block
.\compt1.asm(3) : fatal error A1010:unmatched block nesting : main

So I am just wondering, is there some obvious error in part of the code, or is it the compiler thats messing up.

Upvotes: 7

Views: 23985

Answers (6)

Kjetil Limkjær
Kjetil Limkjær

Reputation: 1560

From a quick browse, it seems that assembly under VS.NET is uncommon enough that you have to set up custom build rules. You can find an example here: http://www.cs.virginia.edu/~evans/cs216/guides/vsasm.html

Out of curiosity, what are you trying to accomplish? If you're just doing this for fun, or to learn assembly language, you'll probably be better off using a dedicated assembly editor. The MASM32 SDK is one such option. You can then link to the libraries you create from the Visual Studio project.

If you're planning on using small segments of assembly in timing-critical pieces of a C/C++ project, I would advise having a good look at compiler intrinsics (http://msdn.microsoft.com/en-us/library/26td21ds.aspx) before deciding to use fully hand-coded assembly files.

Upvotes: 2

piCookie
piCookie

Reputation: 9820

This seems to work for me using the following ML and LINK lines
ml /coff /c test.asm
link /subsystem:console test.obj

TITLE Test app
.386
.MODEL flat, stdcall
.STACK 4096
; --------------------
.code
main PROC
ret
main ENDP
; --------------------
END main

Upvotes: 0

Eliseo Ocampos
Eliseo Ocampos

Reputation: 2523

I asume that the code needs some directives, try this out:

.model small
.code
main:
  mov ax, 10
end main

Here, .model says that we'll use a "small" memory program model and .code says that the following lines are executable code.

EDIT: Ok, there is another example tha should run ok.

; 
  include \masm32\include\masm32rt.inc
;

comment * -----------------------------------------------------
                 Build this console app with
              "MAKEIT.BAT" on the PROJECT menu.
        ----------------------------------------------------- *

  .data?
    value dd ?

  .data
    item dd 0

  .code

start:

; -------------------------------------------------------------------------

  call main
  inkey        ; wait for a keystroke before exiting
  exit

; -------------------------------------------------------------------------

main proc

  print "Hello World",13,10
  ret

main endp

; -------------------------------------------------------------------------

end start

(I took it from here )

Upvotes: 2

stormsoul
stormsoul

Reputation: 496

You lack a ".code" directive before the code to make MASM know that this is supposed to be in the code segment. Aside from that, you do not RETURN in any way from the code, so the CPU blissfully tries to execute whatever bytes follow as if they were executable code. No wonder it crashes after running.

Upvotes: 2

Louis Davis
Louis Davis

Reputation: 784

Try this out:

.model small
.stack
.data
message   db "Hello world!", "$"

.code

main   proc
   mov   ax,seg message
   mov   ds,ax

   mov   ah,09
   lea   dx,message
   int   21h

   mov   ax,4c00h
   int   21h
main   endp
end main

Upvotes: 4

rlbond
rlbond

Reputation: 67849

You need to use the segment statement. Try segment CODE at the top.

Upvotes: 0

Related Questions