Author Topic: How to redefine symbols ?  (Read 17787 times)

nobody

  • Guest
How to redefine symbols ?
« on: December 07, 2008, 06:20:42 PM »
Simple code:

x dw 10
        y dw 10
       [come code here]
       x dw 20 ; i wish to manually redefine x value

compilation will fail:

filename.asm:65: error: symbol `x' redefined

Is there any simple way to manually redefine symbols in NASM ?

nobody

  • Guest
Re: How to redefine symbols ?
« Reply #1 on: December 18, 2008, 12:44:25 AM »
x dw 10
y dw 10
[come code here]
x dw 20 ; i wish to manually redefine x value

RE:
mov dword [x],20

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to redefine symbols ?
« Reply #2 on: December 18, 2008, 02:26:41 AM »
Cool! You've redefined x and y at the same time! :)

"dw", though it looks like "dword", is actually "data word" and takes only 16 bits. Moving a dword into it will overwrite the next 16 bits. Either make the variables "dd", or only move a word into 'em.

Original poster: if you could have 2 "x" variables in your program, which one would you use? How would you know?

I suspect "mov word [x], 20" is what you want, but you *could* do:

%define X 10
%define Y 10
; some code
%undefine X
%define X 20

Better, perhaps, would be:

%assign x 10
; some code
%assign x 20
or
%assign x x + 1
or whatever...

But these are "assemble-time" variables. For a "run-time" variable, you really only want *one* with a given name! Think about it...

Best,
Frank