Author Topic: extern macro  (Read 10298 times)

Offline nasm32

  • Jr. Member
  • *
  • Posts: 25
extern macro
« on: March 31, 2015, 10:08:41 PM »
Hi

I am looking for a way to include a file with many externs but only to allow the assembler to actually use those externs that have been used in the code.

I am not exactly sure how I would write such a macro. Let's say I have 10 externs in an include file, and I refer only to one of those in my code. I need a macro that excludes the 9 externs that havent been used and only keep the one which have been used.

Offline nasm32

  • Jr. Member
  • *
  • Posts: 25
Re: extern macro
« Reply #1 on: April 01, 2015, 05:25:52 PM »
Do anyone have experience with this issue, give me some advice here.

Frank Kotler, give me an example here.
« Last Edit: April 03, 2015, 02:13:02 PM by nasm32 »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: extern macro
« Reply #2 on: April 02, 2015, 03:21:10 AM »
There was a macro file named NASM32.inc floating around a few years ago that did just this. It's now evolved into the NASMX macro set, you should look into that for a lot of decent examples of macros. However, to save you a little time, this is how you could go about doing it:

Code: (externdef.inc) [Select]
%ifndef _EXTERNDEF_INCLUDED
%define _EXTERNDEF_INCLUDED

%imacro externdef 1
%ifndef __%1_defined
%define __%1_defined
%else
%error %1 already defined.
%endif
%endmacro

%imacro call 1
%ifndef %1
%ifdef __%1_defined
%ifndef __%1_used
%define __%1_used
extern %1
%endif
%endif
%endif
call %1
%endmacro

%endif ;_EXTERNDEF_INCLUDED

Code: (testapp.asm) [Select]
%include "externdef.inc"

externdef printf ; used
externdef strlen ; unused
externdef socket ; unused

section .text

my_proc:
push dword sz_message2
push dword sz_format
call printf
add esp, (2 * 4)
ret

global main
main:
push dword sz_message1
push dword sz_format
call printf
add esp, (2 * 4)

call my_proc

mov eax, 0
ret

section .data

sz_message1: db "Hello world!", 0
sz_message2: db "Hello again!", 0
sz_format: db "%s", 13, 10, 0

Code: (Linux build process) [Select]
bkeller@debian:~$ cd externdef/
bkeller@debian:~/externdef$ ls
externdef.inc  testapp.asm
bkeller@debian:~/externdef$ nasm -f elf32 testapp.asm -o testapp.o
bkeller@debian:~/externdef$ gcc -m32 testapp.o -o testapp
bkeller@debian:~/externdef$ ./testapp
Hello world!
Hello again!
bkeller@debian:~/externdef$

Code: (nasm -e testapp.asm) [Select]
%line 3+1 externdef.inc

%line 11+1 externdef.inc

%line 23+1 externdef.inc

%line 2+1 testapp.asm

%line 6+1 testapp.asm

[section .text]

my_proc:
 push dword sz_message2
 push dword sz_format
[extern printf]
%line 12+0 testapp.asm
 call printf
%line 13+1 testapp.asm
 add esp, (2 * 4)
 ret

[global main]
main:
 push dword sz_message1
 push dword sz_format
 call printf
 add esp, (2 * 4)

 call my_proc

 mov eax, 0
 ret

[section .data]

sz_message1: db "Hello world!", 0
sz_message2: db "Hello again!", 0
sz_format: db "%s", 13, 10, 0

As it is, the file EXTERNDEF.INC could also be included into other header files without worrying about multiple-inclusion issues. Hope this helps. :)

About Bryant Keller
bkeller@about.me

Offline nasm32

  • Jr. Member
  • *
  • Posts: 25
Re: extern macro
« Reply #3 on: April 02, 2015, 07:03:02 PM »
Excellent  :)

Thanks, no need to change it, it seems to work right out of the box.

I'm an object guru and I live and breed coff files so this saved my life (considering all the externs I had to type manually)
« Last Edit: April 02, 2015, 07:04:53 PM by nasm32 »