| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1770 人关注过本帖
标题:学习nasm 的各种 语句
只看楼主 加入收藏
madfrogme
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
结帖率:98.63%
收藏
已结贴  问题点数:20 回复次数:9 
学习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 编辑 ]
搜索更多相关主题的帖子: hello similar message 学习 
2012-10-01 21:25
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:7 
学习了
2012-10-01 21:40
madfrogme
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
接下来又学习了用printf(format, value) 这个格式来打印, 

重点是先push value ,再push format, 从右往左push,顺序很重要

SECTION .data

msg: db    "hello world,this is a message",10,0
msglen: equ $-msg ; ??????????
fmt: db "Msg length = %d",10,0

i: dd 120        ; 32 bits
fmt2: db "Value of myInteger is %d", 10,0

SECTION .text

extern printf
global main

main:
     push ebp
     mov  ebp, esp

     push msg
     call printf

     push DWORD msglen
       ; 这里若不指定DWORD,则错误asm1.asm:27: error: operation size not specified

     push fmt
     call printf

     ;printf(format,value)
     push DWORD [i]         ; i is a memory address,不加方括号则打印地址
     push fmt2
     call printf

     mov esp, ebp
     pop ebp
     ret

编译方法同上上了


[ 本帖最后由 madfrogme 于 2012-10-1 22:57 编辑 ]

The quieter you become, the more you can hear
2012-10-01 21:56
madfrogme
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
回复 2楼 zklhp
不客气,哈哈

The quieter you become, the more you can hear
2012-10-01 22:01
信箱有效
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1102
专家分:4268
注 册:2012-6-19
收藏
得分:7 
学习
2012-10-01 22:03
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:7 
NASM 和 MASM确实不一样啊。

梅尚程荀
马谭杨奚







                                                       
2012-10-01 23:05
madfrogme
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
不过弄懂一个,想弄懂另一个不会是件难事了,花几天时间熟悉一下語法的事情吧

The quieter you become, the more you can hear
2012-10-01 23:10
madfrogme
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
equ 語句
 EQU: Defining Constants
EQU defines a symbol to a given constant value:

when EQU is used, the source line must contain a label.


The action of EQU is to define the given label name to the value of its (only) operand.

This definition is absolute, and cannot change later.

message         db      'hello, world'
msglen          equ     $-message

defines msglen to be the constant 12.

msglen may not then be redefined later.

This is not a preprocessor definition either: the value of msglen is evaluated once,


using the value of $ at the point of definition,

rather than being evaluated wherever it is referenced and using the value of $ at the point of reference.


[ 本帖最后由 madfrogme 于 2012-10-3 23:29 编辑 ]

The quieter you become, the more you can hear
2012-10-02 21:43
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
nasm和masm最大的区别在内存寻址上面 其他的可能差别不大罢
2012-10-02 21:48
madfrogme
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
resb语句

RESB, RESW and RESD are designed to be used in the BSS section of a module:

they declare uninitialised storage space.

resb 语句两点,在BSS section 中, 确保没有被初始化的空间

The quieter you become, the more you can hear
2012-10-03 22:36
快速回复:学习nasm 的各种 语句
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021824 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved