学习nasm 的各种 语句
今天 用nasm 知道了extern 这个语句,学习SECTION .data
msg: db "hello world,this is a message",10,0
SECTION .text
extern printf
global main
main:
push ebp ; 创建栈
mov ebp, esp
push msg
call printf
mov esp, ebp ; 消灭栈
pop ebp
ret
编译的时候则用
$ nasm -f elf -o asm1.o asm1.asm
$ gcc -o asm1 asm1.o
说明来自 nasm manual
EXTERN is similar to the MASM directive EXTRN and the C keyword extern:
it is used to declare a symbol which is not defined anywhere in the module being assembled,
but is assumed to be defined in some other module and needs to be referred to by this one.
Not every object-file format can support external variables: the bin format cannot.
The EXTERN directive takes as many arguments as you like.
Each argument is the name of a symbol:
extern _printf
extern _sscanf,_fscanf
GLOBAL: Exporting Symbols to Other Modules
GLOBAL is the other end of EXTERN:
if one module declares a symbol as EXTERN and refers to it,
then in order to prevent linker errors, some other module must actually define the symbol
and declare it as GLOBAL. Some assemblers use the name PUBLIC for this purpose.
The GLOBAL directive applying to a symbol must appear before the definition of the symbol.
GLOBAL uses the same syntax as EXTERN,
except that it must refer to symbols which are defined in the same module as the GLOBAL
directive. For example:
global _main
_main:
; some code
[ 本帖最后由 madfrogme 于 2012-10-2 22:39 编辑 ]