Author Topic: Using duplicate "extern"  (Read 6433 times)

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Using duplicate "extern"
« on: April 22, 2018, 02:40:50 AM »
I wonder if there's any side effects that I don't know of if I declare two similar externs in the same source (besides code replication). For example;

Code: [Select]
%macro ecall 1
    extern %1
    call %1
%endm

Of which I use to import external functions like below, without the need to declare separate externs for each;

Code: [Select]
ecall printf
ecall scanf
ecall printf ;this one is repeating the extern

So far I been using it without any problem, but still, I am not so sure.

Thanks for clarification.

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 221
  • Country: gb
    • GPUZoo
Re: Using duplicate "extern"
« Reply #1 on: April 22, 2018, 09:49:17 PM »
That code is / would be fine.

ecall printf
would expand to
Code: [Select]
    extern printf
    call printf

Each time you call the macro it sets up your EXTERN and CALL code.

If you want to write
ecall printf

twice, you would need to modify your macro:

Code: [Select]
    %ifndef ext_%1
    %define ext_%1
    extern %1
    endif
    call %1

That way you exclude the risk of bloating your code with multiple identical externs.
« Last Edit: April 24, 2018, 12:44:04 AM by debs3759 »
My graphics card database: www.gpuzoo.com

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: Using duplicate "extern"
« Reply #2 on: April 23, 2018, 08:20:08 AM »
Thanks debs3759.

My suspicion actually came from other syntaxes that do not allow such repetitive declaration. It would translate to a syntax error. I am glad that NASM does allow it, making my life easier. Like some people say, a bug can be another's feature.