Question

Logically, what I want to do is:

      GLOBAL  FRED
FRED  EQU     5

in one module, and

      EXTERN  FRED
      MOV     ECX,FRED

in a second.

I'm aware that

first module

      GLOBAL  FRED
FRED: DD      5

second module

      EXTERN  FRED
      MOV     ECX,[FRED]

works perfectly happily, but what I'd really prefer is the first version.

I've tried

FRED  EQU     5
FRED: EQU     5
%define FRED  5
%assign FRED  5

None worked - no error message, but the label is not exported. Only if FRED is an address does it co-operate.

So - is there any way of exporting a value from one module as a constant, not an address?

Was it helpful?

Solution 2

Short answer: No.

You cannot use equ, define or assign for exporting because they do not show up in the compiled module as separate values. They only affect the preprocessor, so writing

   FRED equ 5
   mov  ecx, FRED

actually compiles to

   mov  ecx, 5

Common practice is to use C-style header file that has all the constants defined and then just include the header file wherever needed.

fred.asm:

   %ifndef FRED_ASM
     %define FRED_ASM

       %define FRED 5
       ; ... other constants ...

   %endif

module.asm

   %include "fred.asm"

   mov ecx, FRED

OTHER TIPS

Nasm treats a dot character (".") as a legal character in symbol names. You can use this to fake COM (Component Object Model) "Object Properties". If you have 3 modules, each of which has a "width" variable, you can pull the following stunt:

;ButtonWidget
    SECTION .data
    global ButtonWidget.width, ButtonWidget.height
    ButtonWidget:
       .width dd 50
       .height dd 10

;SelectWidget
    SECTION .data
    global SelectWidget.width, SelectWidget.height
    SelectWidget:
       .width dd 50
       .height dd 100

;TextWidget
    SECTION .data
    global TextWidget.width, TextWidget.height
    TextWidget:
       .width dd 200
       .height dd 10

In your main module,

;GUIapp
extern ButtonWidget.width, ButtonWidget.height
extern SelectWidget.width, SelectWidget.height
extern TextWidget.width, TextWidget.height
%macro MakeChildWindow 4
push %1
push %2
push %3
push %4
call makechildwindow
%endmacro
MakeChildWindow 10, 10, ButtonWidget.width, ButtonWidget.height
MakeChildWindow 10, 30, SelectWidget.width, SelectWidget.height
MakeChildWindow 10, 200, TextWidget.width, TextWidget.height

This technique has the advantage that the external variables can be fixed or changeable and it is VERY readable.

If I understand you correctly, you want FRED to be defined somewhere and used by many modules/source files? Each module/source file, uses FRED and instead of changing the value in each source file and re-assembling each file, just change one instance of FRED and have it use everywhere? If this is the case, you can just plop the equates into a static library and link against it. This does not work with %define, only equ.

Try this:
libequates.asm

global FRED, JOHN

FRED    equ 5
JOHN    equ 6

makefile for libequates.asm (modify for you needs)

LIBRARY = libequates

all: $(LIBRARY) PostMake

$(LIBRARY): $(LIBRARY).o 
    ar -cr $(LIBRARY).a $(LIBRARY).o
    strip --strip-unneeded $(LIBRARY).a

$(LIBRARY).o: $(LIBRARY).asm
    nasm -f elf64 $(LIBRARY).asm -F dwarf   

PostMake:
    mkdir -p ~/projects/lib/
    mv $(LIBRARY).a ~/projects/lib

Now to use and test if it works (I will use printf for simplicity):

externs.asm

extern FRED, JOHN, printf, exit

global start

section .data
fmtint      db  "%d", 10, 0

section .text
start:
    mov     rsi, FRED
    mov     rdi, fmtint
    xor     rax, rax
    call    printf

    mov     rsi, JOHN
    mov     rdi, fmtint
    xor     rax, rax
    call    printf

    mov     rdi, 0
    call    exit

makefile for externs.asm

APP = externs

all: $(APP) clean

$(APP): $(APP).o 
    ld  -o $(APP) $(APP).o -entry start -L ~/projects/lib -lequates -lc -dynamic-linker /lib64/ld-linux-x86-64.so.2

$(APP).o: $(APP).asm
    nasm -f elf64 $(APP).asm -F dwarf

clean:
    rm $(APP).o

and output:
enter image description here

Now, you only need to change the equate value in one place, re-assemble only the static library and re-link your program/modules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top