汇编基本语法

汇编程序由三部分组成:

  数据段 bss段 文字

  数据段:

    用于声明初始化数据或常量的数据段,运行时,此数据不改变。

    声明数据段的语法:

    section  .data

  bss段:

    bss段声明变量。

    声明语法:

    section .bss

  文本段:

    保存实际代码。

    本段开头必须的声明 global_start:告诉内核程序开始执行代码。

    声明文本段语法:

    section .text

      global _start

    _start:

注释:

  注释用分号: ; 

汇编语言语句:

  三种类型:

    可执行指令:告诉处理器怎么做

    汇编指令:告诉汇编有关汇编过程的各个方面

    宏:文本替换

汇编语言语法:

  [lable] mnemonic [operands] [;comment]

 

汇编语言输出 hello world :

  输入内容:

  

  vim hello.asm      ;创建一个hello.asm文件

  nasm -f elf hello.asm  ;对程序进行汇编,形成hello.o文件

  ld -m elf_i386 -s -o hello hello.o  ;链接并创建一个可执行的hello

  ./hello          ;执行程序

 

  hello.asm 的内容:

  

section .text
    global _start
_start:
    mov edx,len
    mov ecx,msg
    mov ebx,1
    mov eax,4
    int 0x80

    mov eax,1
    int 0x80

section .data
    msg db 'hello,world', 0xa
    len equ $ - msg

 

 

 

section	.text    ;声明此处为 文本段
    global _start   ;此部分必须声明,用于告诉内核程序开始执行
_start:	            ;告诉链接器此处为程序入口
    mov	edx,len     ;信息长度
    mov	ecx,msg     ;写的信息
    mov	ebx,1       ;文件描述
    mov	eax,4       ;system call number (sys_write)
    int	0x80        ;call kernel
	
    mov	eax,1       ;system call number (sys_exit)
    int	0x80        ;call kernel

section	.data    ;声明此处为 数据段
msg db 'Hello, world!', 0xa  ;信息 
len equ $ - msg              ;信息长度

 

 

本节参考:http://www.yiibai.com/html/assembly/2013/0812118.html

posted @ 2015-10-28 16:46  木易·月  阅读(1450)  评论(0编辑  收藏  举报