Author Topic: Include file by a macro  (Read 9927 times)

Offline Roman

  • Jr. Member
  • *
  • Posts: 10
Include file by a macro
« on: May 29, 2012, 08:48:26 AM »

Hi All!
Sory for my English, i do not know it :)
I have a trouble:
i have some code, this code have follow line
Code: [Select]
%include 'FILE_NAME'and compile  as follows
Code: [Select]
nasm -dFILE_NAME=filename.asm test.asmbut i gets error
Code: [Select]
fatal: unable to open include file `FILE_NAME'
I need this to work, can you help me?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Include file by a macro
« Reply #1 on: May 29, 2012, 09:56:43 AM »
Well, I can't get it to work as you specify. We could do:

Code: [Select]
nasm -p filename.asm test.asm

That will "pre-include" filename.asm, but it's limited to including the file at the beginning of test.asm. Will that work for you?

If you need the included file at some specific place in test.asm, other than at the beginning, you may be stuck with...
Code: [Select]
%ifdef __LINUX__
%include "linux.inc"
%elifdef __WINDOWS__
%include "windows.inc
%elifdef __BSD__
%include "bsd.inc"
%else
%error "Must define __LINUX__, __WINDOWS__ or __BSD__"
%endif
or some such. This would limit you to fixed choices, rather than being able to define an arbitrary filename on the command line.

Hopefully, your include file can be the first thing in your source file - the "-p" switch would be much easier!

Best,
Frank


Offline Roman

  • Jr. Member
  • *
  • Posts: 10
Re: Include file by a macro
« Reply #2 on: May 29, 2012, 10:24:56 AM »
Thanks, but this is not what I need to. There is a code with a documentation, and it is said that method I described above will work.
Documentation said that I need nasm 0.98.35, but I tried that version and it still does't work.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Include file by a macro
« Reply #3 on: May 29, 2012, 11:40:14 AM »
0.98.35 was a long time ago!

What I think I'm running up against is that if the parameter to %include doesn't include quotes around it, Nasm complains that "%include expects a filename". If I do put quotes around it, the macro "FILE_NAME" doesn't get expanded to "filename.asm". I just tried no quotes in the %include directive, and:

Code: [Select]
nasm -dFILE_NAME=\"filename.asm\" test.asm

This appears to be working! I question whether it'll work in a dos/doze environment, since they use the backslash differently (as I recall... you could try it). You don't say what platform you're on, so maybe this isn't an issue.

Is this "code with documentation" available to the public? Can you post a link?

Best,
Frank


Offline Roman

  • Jr. Member
  • *
  • Posts: 10
Re: Include file by a macro
« Reply #4 on: May 29, 2012, 12:11:33 PM »
Thanks! It work:)

I use Archlinux
Quote
Is this "code with documentation" available to the public?
sorry, there is no.

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Include file by a macro
« Reply #5 on: May 29, 2012, 10:47:20 PM »
Try using the %defstr directive to quote the argument.

Code: (demo.asm) [Select]
BITS 32

%macro incdef 1
%push _incdef_
%defstr %$file %{1}
%include %{$file}
%pop
%endmacro

incdef SYSTEM_CALLS

SECTION .text

GLOBAL _start
_start:
sys_write 1, msg_str, msg_len
sys_exit 0

SECTION .data

msg_str db "Hello, World!", 10
msg_len equ ($-msg_str)

Code: (macros.asm) [Select]
%macro sys_write 3
mov edx, %3
mov ecx, %2
mov ebx, %1
mov eax, 4
int 80h
%endmacro

%macro sys_exit 1
mov ebx, %1
mov eax, 1
int 80h
%endmacro

Code: [Select]
$ ls
demo.asm  macros.asm
$ nasm -f elf demo.asm -DSYSTEM_CALLS=macros.asm
$ ls
demo.asm  demo.o  macros.asm
$ gcc -nostartfiles demo.o -o demo
$ ./demo
Hello, World!
$

About Bryant Keller
bkeller@about.me