2
votes

I'm new to assembly language and I'm working out on it using the nasm assembler. I got this error which I'm not able to resolve:

error: attempt to define a local label before any non-local labels

at the line 1 of this program:

.data
hello:  db  "hello world"   
.code
..start
    MOV AX,hello        
    MOV DS,AX
    MOV AH,09h
    INT     21h
.exit
end

How can I fix it?

1
use section .data and section .code. Also ..start and .exit is going to produce an error, not sure what you wanted with that. end is also not used by nasm. Maybe you are trying to compile code intended for a different assembler.Jester
I just want to print the "hello world" string on the std output. Surely there are many errors in that program, but i still got the same error using "section .data", "section .code" and deleting "..start" and ".end"Marco

1 Answers

1
votes

Your code has some errors: the variable hello doesn't requiere colon, the word start with two dots isn't right, you are trying to access the variable without initialize the data segment, and the code to display the string won't do it.

Any assembler program x86 requires a basic structure to work, then you can add more code, procedures, etc. Next is that basic structure :

.stack 100h
.data
my_variable db 'hello$'
.code          
;INITIALIZE DATA SEGMENT.
  mov  ax,@data
  mov  ds,ax

  call my_procedure

;FINISH.  
  mov  ax,4c00h
  int  21h           

proc my_procedure
  mov  dx,offset my_variable
  mov  ah,9
  int  21h
my_label:
  ret
endp
  1. First you have the stack, in this case, 256 bytes (100h).

  2. Next comes the data segment for your variables.

  3. The code segment requires the data segment to be initialized, or you won't be able to access your variables. After that you add all your code, but never forget to finish the program properly. At the bottom you may add your procedures.

  4. In the procedure there is a label and the syntax to declare it: label-name followed by colon, and the right way to display the variable.

Hope this helps you.

Sorry, I forgot: I used EMU8086 compiler.

Another edit: pay attention to the '$' sign at the end of 'hello', required for any string you display this way. If you forget it, weird characters will be displayed too.