Author Topic: help! samplest code, symbol data undefined  (Read 11382 times)

Offline starca

  • Jr. Member
  • *
  • Posts: 2
help! samplest code, symbol data undefined
« on: July 05, 2011, 02:24:14 AM »
code :

segment data
msg   db    'hello word$'

segment stack
   st resb 100

segment code
..start:
   mov ax, stack
   mov ss, ax
   mov sp, 100

        mov ax, data
   mov ds, ax
   
       mov dx,msg
       mov ah,09
       int 21h
 
       mov ax,4c00h
       int 21h

I get error messages: symbol data undefined
                               symbol stack undefined
anyone know why ?  thank you for help

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: help! samplest code, symbol data undefined
« Reply #1 on: July 05, 2011, 04:38:19 AM »
Hi starca,

The source code you show is suitable for only one output format: "-f obj". Give Nasm a command line like:

Code: [Select]
nasm -f obj hello.asm

That should get Nasm to "shut up and assemble", producing "hello.obj" (assuming you named your source file "hello.asm" - otherwise change it to whatever you named it, of course).

Now you'll need to link it, to produce an executable. The command line to the linker will differ slightly, depending on which linker you're using. Something like:

Code: [Select]
alink -oEXE hello.obj

You can leave out the "-oEXE" if you want - it is the default for Alink. No space after the "-o" - Alink's funny that way: with a space after it, "-o" specifies the output file name, with no space after it "-oEXE", "-oCOM" or "-oPE" specify the type of executable. You want "-oEXE" (for this source code). You could even leave out the ".obj" and just type:

Code: [Select]
alink hello

This will produce "hello.exe". It will also produce a warning about "no stack", but it should run okay anyway. To get rid of the warning, you need a slight change to your source code:

Code: [Select]
segment stack stack

The first "stack" is just a name, and could be anything. The second "stack" tells Nasm the type of segment, so Nasm can tell Alink. IF you do this, you can leave out part of your code...

Code: [Select]
   mov ax, stack
   mov ss, ax
   mov sp, 100

I know, I know, it shows that in the example in the manual, but IF we do it right, DOS will load your file with ss and sp all set up. Won't hurt to leave it in, but DOS does it for us. You still need to set up ds, as you've done.

Another option would be to create a ".com" file (different type of executable). Nasm will do this without the asssistance of a linker, but needs slightly different source code:

Code: [Select]
org 100h ; tell Nasm where DOS will load us

segment .data
msg   db    'hello word$'

segment .text
       mov dx,msg
       mov ah,09
       int 21h
 
       mov ax,4c00h
       int 21h

Notice the spelling of the segment names! Nasm knows ".data" and ".text" (and ".bss" if you have uninitialized data - you don't). These have to have the '.', and have to be lowercase! There's no entrypoint ("..start" is only for "-f obj") specified - a .com file always starts at the beginning of the file. Nasm will move ".data" after ".text" (we don't want to execute the ".data" segment!)... IF we spell 'em right.

You'll need a slightly different command line for Nasm:

Code: [Select]
nasm -f bin hello.asm -o hello.com

That should produce "hello.com", all ready to run (if I haven't typo'ed anything). Somewhat simpler than using a linker (IMO), but less flexible when you get to more advanced code.

If you're running a 64-bit version of Windows, DOS isn't supported and this won't run (neither the .exe or the .com versions). In this case, get "dosbox" or find an example suitable for Windows. Otherwise it should work - post again if it doesn't... (you might want to spell "world" with an "l" in it :) )

If you put the word "code" inside of square brackets, "[]" (like a Nasm memory reference) at the beginning of your code, and "/code" in "[]"s at the end of your code, it'll make "code blocks". I don't care, myself, but some of the moderators request that you use 'em!

Best,
Frank


Offline starca

  • Jr. Member
  • *
  • Posts: 2
Re: help! samplest code, symbol data undefined
« Reply #2 on: July 05, 2011, 11:40:54 AM »
thanks Frank!!   I sucked this for whole day. I used masm before. moved to nasm now.

really help.