Home > Back-end >  When to use EXTERNDEF with ABS in MASM?
When to use EXTERNDEF with ABS in MASM?

Time:12-20


EXTERNDEF

Defines one or more external variables, labels, or symbols called name whose type is type.

Syntax

EXTERNDEF name:type

If name is defined in the module, it is treated as PUBLIC. If name is referenced in the module, it is treated as EXTERN. If name is not referenced, it is ignored. The type can be ABS, which imports name as a constant. Normally used in include files.


QUESTION

When a MASM global constant

NUMBER EQU 12345

needs to be shared between assembly files, should it be added to an include file as

EXTERNDEF NUMBER:ABS

or should the constant itself be moved into the include file?

Both approaches build correctly but which is the preferred method?


EXAMPLE

Declare the global constant in this include file

num.inc

EXTERNDEF NUMBER:ABS

The constant is defined in this assembly file

num.asm

option casemap:none

include num.inc

NUMBER EQU 12345  ; Decided to define here (not moved to num.inc)

.code

DoStuff proc
mov rax,NUMBER
ret  
DoStuff endp

end

and is also referenced in this assembly file

abs.asm

option casemap:none

include num.inc

.code

main proc
mov rax,NUMBER
ret
main endp

end

Assembled using this batch file

abs.bat

@echo on

if not defined DevEnvDir (
  call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"
)

ml64.exe abs.asm num.asm /link /subsystem:console /defaultlib:kernel32.lib /defaultlib:libcmt.lib

CodePudding user response:

Found the answer in this book The Art of 64-Bit Assembly, Volume 1 x86-64 Machine Organization and Programming (Randall Hyde), it states the following about the ABS usage:

MASM actually has two external symbol declaration directives: extern and externdef.

These two directives use the syntax

extern symbol:type {optional_list_of_symbol:type_pairs}<br>
externdef symbol:type {optional_list_of_symbol:type_pairs}

where symbol is the identifier you want to use from another assembly unit, and type is the data type of that symbol. The data type can be any of the following:

  • proc, which indicates that the symbol is a procedure (function) name or a statement label

  • Any MASM built-in data type (such as byte, word, dword, qword, oword, and so on)

  • Any user-defined data type (such as a struct name)

  • abs, which indicates a constant value

The abs type isn’t for declaring generic external constants such as someConst = 0. Pure constant declarations, such as this one, would normally appear in a header file (an include file), which this section will describe shortly. Instead, the abs type is generally reserved for constants that are based on code offsets within an object module. For example, if you have the following code in an assembly unit,

        public someLen
someStr byte   "abcdefg"
someLen =      $-someStr

someLen’s type, in an extern declaration, would be abs.

  • Related