helloworld-高级版

.section .data#初始化的变量
output:.ascii "hello,world\n"#要打印的字符串,.data为初始化值的变量。output是标签,指示字符串开始的位置,ascii为数据类型
.section .bss#未初始化的变量,由0填充的缓冲区.lcomm num,20#lcomm为本地内存区域,即本地汇编外的不能进行访问。.comm是通用内存区域。
.section .text#汇编语言指令码.globl _start#启动入口_start:movl $4,%eax#调用的系统功能,4为write   movl $output,%ecx#要打印的字符串movl $1,%ebx#文件描述符,屏幕为1   movl $12,%edx#字符串长度int $0x80#显示字符串hello,worldmovl $0,%eaxmovl $num,%edimovl $65,1(%edi)#A 的asciimovl $66,2(%edi)#B 的ascii movl $67,3(%edi)#C 的ascii movl $68,4(%edi)#D 的asciimovl $10,5(%edi)#\n的ascii movl $4,%eax#调用的系统功能,4为write    movl $num,%ecx#要打印的字符串  movl $1,%ebx#文件描述符,屏幕为1   movl $6,%edx#字符串长度int $0x80#显示字符串ABCDmovl $1,%eax#1为退出movl $0,%ebx#返回给shell的退出代码值int $0x80#内核软中断,退出系统

效果及调试, -gstabs用于生成调试信息

xxx-lx@xxx-lx-desktop:~/private/mytest$ as -gstabs -o hello.o hello.s

xxx-lx@xxx-lx-desktop:~/private/mytest$ ld -o hello hello.o

xxx-lx@xxx-lx-desktop:~/private/mytest$ ./hello

hello,world

ABCD

xxx-lx@xxx-lx-desktop:~/private/mytest$ gdb hello

GNU gdb (GDB) 7.1-ubuntu

Copyright © 2010 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html;

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type “show copying”

and “show warranty” for details.

This GDB was configured as “x86_64-linux-gnu”.

For bug reporting instructions, please see:

http://www.gnu.org/software/gdb/bugs/;…

Reading symbols from /home/xxx-lx/private/mytest/hello…done.

(gdb) list

1 .section .data#初始化的变量

2 output:

3 .ascii “hello,world\n”

4 #要打印的字符串,.data为初始化值的变量。output是标签,指示字符串开始的位置,ascii为数据类型

5 .section .bss#未初始化的变量,由0填充的缓冲区

6 .lcomm num,20

7 #lcomm为本地内存区域,即本地汇编外的不能进行访问。.comm是通用内存区域。

8 .section .text#汇编语言指令码

9 .globl _start#启动入口

10 _start:

(gdb) run

Starting program: /home/xxx-lx/private/mytest/hello

hello,world

ABCD

Program exited normally.

(gdb)

linux-汇编-调用C库函数

1、使用GCC编译

.section .data
output:
.asciz “http://xxx.iteye.com\n”
.section .text
.global main
main:
push $output
call printf
addl $4,%esp
push $0
call exit

gcc -o test test.s
./test
http://xxx.iteye.com
2、使用汇编器编译,使用动态链接-dynamic-linker,要求后跟SO库,可使用find / -name ld*.so来寻找链接库,每个LINUX版本不一样,链接库不一样,笔者用的是puppy linux,链接库名为ld-linux.so.2

.section .data
output:
.asciz “http://xxx.iteye.com\n”
.section .text
.global _start
_start:
push $output
call printf
addl $4,%esp
push $0
call exit

as -o test.o test.s
ld -lc -dynamic-linker /lib/ld-linux.so.2 -o test test.o
./test
http://xxx.iteye.com

汇编中通用寄存器的目的

1、EAX和AX:累加器,所有的I/O指令用它来与外部设备传送信息

2、EBX和BX:在计算存储单元地址时常用作基地址寄存器

3、ECX和CX:保存计数值

4、EDX和DX:做四字或二字运算时,可以把EDX(DX)和EAX(AX)组合在一起存放一个四字或二字长的数据,在对某些I/O操作时,DX可以放I/O的端口地址

5、ESP和SP:堆栈栈顶指针。

6、EBP和BP:基址寄存器

7、ESI和SI:源变址

8、EDI和DI:目的变址

LINUX/UNIX一般 都安装了binutils,如果没有安装,在UBUNTU下可以使用apt-get

1.xxx@xxx-laptop:~$ sudo apt-get install binutils

[sudo] password for xxx:

正在读取软件包列表… 完成

正在分析软件包的依赖关系树

正在读取状态信息… 完成

binutils 已经是最新的版本了。

下列软件包是自动安装的并且现在不需要了:

libany-moose-perl libtest-exception-perl libmouse-perl gcc-4.4-multilib

ibus-pinyin-db-open-phrase libclass-c3-perl libdata-optlist-perl

libclass-c3-xs-perl libparams-util-perl lib64gomp1 libmro-compat-perl

lib64gcc1 pinyin-database libsub-install-perl libc6-amd64 libc6-dev-amd64

gcc-multilib libclass-method-modifiers-perl libsub-uplevel-perl lib64stdc++6

libsub-exporter-perl libalgorithm-c3-perl

使用’apt-get autoremove’来删除它们

升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。

2.也可以在其主页上用源代码安装

http://www.gnu.org/software/binutils/

1、调试hello,要求编译时指定了-gstabs选项

2、运行hello

xxx-lx@xxx-lx-desktop:~/private/mytest$ ./hello

hello,world

ABCD

3、用gdb打开hello

xxx-lx@xxx-lx-desktop:~/private/mytest$ gdb hello

GNU gdb (GDB) 7.1-ubuntu

Copyright © 2010 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html;

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type “show copying”

and “show warranty” for details.

This GDB was configured as “x86_64-linux-gnu”.

For bug reporting instructions, please see:

http://www.gnu.org/software/gdb/bugs/;…

Reading symbols from /home/xxx-lx/private/mytest/hello…done.

4、列出源代码

(gdb) list

warning: Source file is more recent than executable.

1 .section .data#初始化的变量

2 output:

3 .ascii “hello,world\n”

4 #要打印的字符串,.data为初始化值的变量。output是标签,指示字符串开始的位置,ascii为数据类型

5 .section .bss#未初始化的变量,由0填充的缓冲区

6 .lcomm num,20

7 #lcomm为本地内存区域,即本地汇编外的不能进行访问。.comm是通用内存区域。

8 .section .text#汇编语言指令码

9 .globl _start#启动入口

10 _start:

(gdb) list

11 movl $4,%eax#调用的系统功能,4为write

12 movl $output,%ecx#要打印的字符串

13 movl $1,%ebx#文件描述符,屏幕为1

14 movl $12,%edx#字符串长度

15 int $0x80#显示字符串hello,world

16

17 movl $0,%eax

18 movl $num,%edi

19 movl $65,1(%edi)#A 的ascii

20 movl $66,2(%edi)#B 的ascii

5、设置断点

(gdb) break 17

Breakpoint 1 at 0x4000c6: file hello.s, line 17.

6、运行至断点

(gdb) run

Starting program: /home/xxx-lx/private/mytest/hello

hello,world

Breakpoint 1, _start () at hello.s:17

7、运行下条语句

17 movl $0,%eax

(gdb) next

18 movl $num,%edi

8、显示所有寄存器的值

(gdb) info registers

rax 0x0 0

rbx 0x1 1

rcx 0x60011c 6291740

rdx 0xc 12

rsi 0x0 0

rdi 0x0 0

rbp 0x0 0x0

rsp 0x7fffffffe2d0 0x7fffffffe2d0

r8 0x0 0

r9 0x0 0

r10 0x0 0

r11 0x0 0

r12 0x0 0

r13 0x0 0

r14 0x0 0

r15 0x0 0

rip 0x4000cb 0x4000cb <_start+27>

eflags 0x202 [ IF ]

cs 0x33 51

ss 0x2b 43

ds 0x0 0

es 0x0 0

fs 0x0 0

—Type to continue, or q to quit—

gs 0x0 0

(gdb) next

19 movl $65,1(%edi)#A 的ascii
9、按十六进制格式输出edi寄存器的值。/x表示16进制,/d表示10进制,/t表示二进制

(gdb) print/x $rdi

$3 = 0x600128

10、显示所有寄存器值

(gdb) info registers

rax 0x0 0

rbx 0x1 1

rcx 0x60011c 6291740

rdx 0xc 12

rsi 0x0 0

rdi 0x600128 6291752

rbp 0x0 0x0

rsp 0x7fffffffe2d0 0x7fffffffe2d0

r8 0x0 0

r9 0x0 0

r10 0x0 0

r11 0x0 0

r12 0x0 0

r13 0x0 0

r14 0x0 0

r15 0x0 0

rip 0x4000d0 0x4000d0 <_start+32>

eflags 0x202 [ IF ]

cs 0x33 51

ss 0x2b 43

ds 0x0 0

es 0x0 0

fs 0x0 0

—Type to continue, or q to quit—

gs 0x0 0

(gdb) next

20 movl $66,2(%edi)#B 的ascii
11、显示某个内存位置的值,x/nyz,其中n为字段数,y为格式(c为字符,d为10进制,x为16进制),z为字段长度(b为字节,n为16位字,w为32位字)

(gdb) next

21 movl $67,3(%edi)#C 的ascii

(gdb) x/3cb &num

0x600128 : 0 '\000’65 ‘A’ 66 ‘B’

(gdb) next

22 movl $68,4(%edi)#D 的ascii

(gdb) next

23 movl $10,5(%edi)#\n的ascii

(gdb) next

25 movl $4,%eax#调用的系统功能,4为write

(gdb) x/4cb &num

0x600128 : 0 '\000’65 ‘A’ 66 'B’67 ‘C’

12、退出gdb

(gdb)quit

linux-汇编静态符号
.equ 符号名 值

比如

.equ PI 3.1415

使用方式 为:

movl $PI,%eax

汇编数据类型

.ascii 文本字符串

.asciz 以空字符结尾的文本字符串

.byte 字节值

.double 双精度符点数

.float 单精度符点数

.int 32位整数

.long 64位整数

.octa 16字节整数

.quad 8字节整数

.short 16位整数

.single 单精度整数与float相同

汇编传送指令

传送数据元素

movl:32位长度

movw:16位长度

movb:8位长度

比如:

movl 源,目标

汇编-多值内存位置访问(1)

.section .datamyvalue:.byte 67,68,69,70,0mygs:.asciz "%s\n".section .text
.globl mainmain:movl $myvalue,%ecxinc %ecx#本来应输出CDEF,68代表Dpush %ecxpush $mygs    call printfpush $0call exitxxx@xxx-laptop:~/private/mytest$ gcc -o test12 test12.s

xxx@xxx-laptop:~/private/mytest$ ./test12

DEF

xxx@xxx-laptop:~/private/mytest$

xxx@xxx-laptop:~/private/mytest$ gcc -o test12 test12.s

xxx@xxx-laptop:~/private/mytest$ ./test12

E

xxx@xxx-laptop:~/private/mytest$

.section .datamyvalue:.byte 67,68,69,70,0mygs:.asciz "%c\n".section .text
.globl mainmain:#基地址(偏移地址[必须为寄存器],数据元素变址,数据元素长度[必须为寄存器],)#基地址+偏移地址+数据元素变址数据元素长度movl $2,%ecxmovl myvalue(,%ecx,1),%ebx #将myvalue的变址为2,长度为1的数据值移到ebx中push %ebxpush $mygs    call printfpush $0call exit.section .datamyvalue:.byte 67,68,69,70,0mygs:.asciz "%c\n"mygss:.asciz "%s\n"
.section .text
.globl mainmain:#以下为传数值#基地址(偏移地址[必须为寄存器],数据元素变址,数据元素长度[必须为寄存器],)
#基地址+偏移地址+数据元素变址数据元素长度
movl $2,%ecx
movl myvalue(,%ecx,1),%ebx #将myvalue的变址为2,长度为1的数据值移到ebx中
push %ebx
push $mygs
call printf
#以下为传地址
movl $2,%ecx
movl $myvalue,%ebx#传myvalue的地址,变量前加上$
push %ebx
push $mygss
call printf
#以下为传数值
movl $2,%ecx
movl myvalue,%ebx#传myvalue第一个内存位置的数值
push %ebx
push $mygs
call printf
#以下为传地址,mov %eax,(%ebx)表示把eax值复制到寄存器ebx所代表内存位置中,寄存器中存放着地址movl $71,%edxmovl $myvalue,%eax;movb %dl,2(%eax)#eax指向位置后的第2个字节,将69改为71movl $myvalue,%ebxpush %ebxpush $mygss    call printf  push $0call exit

xxx@xxx-laptop:~/private/mytest$ gcc -o test12 test12.s

test12.s: Assembler messages:test12.s:0: Warning: end of file not at end of a line; newline insertedxxx@xxx-laptop:~/private/mytest$ ./test12ECDEFCCDGFxxx@xxx-laptop:~/private/mytest$ 汇编的movl使用.section .datamyvalue:.int 67.section .text
.globl mainmain:movl $myvalue,%ecxpush $myvaluecall printfpush $0call exitxxx@xxx-laptop:~/private/mytest$ gcc -o test12 test12.sxxx@xxx-laptop:~/private/mytest$ ./test12Cxxx@xxx-laptop:~/private/mytest$

C指针原理(21)-C指针基础-ATT汇编相关推荐

  1. C指针原理(15)-C指针基础

    简单C指针 指向整数的指针,以及指针的指针 myhaspl@myhaspl:~ % vim test1.c#include <stdio.h>int main(void){int x;x= ...

  2. C指针原理(22)-C指针基础-att汇编-快速排序

    第一趟排序 以第一个数-2为标准 xxx@xxx-laptop:~/private/mytest$ gcc -o testpx1 testpx1.s xxx@xxx-laptop:~/private/ ...

  3. C指针原理(24)-C指针基础

    取自netbsd中的源码,检查运算溢出,用C语言实现,方法很精妙 /* hide bintime for _STANDALONE because this header is used for hpc ...

  4. C指针原理(17)-C指针基础

    指针本身也是一种变量,支持常用的运算.比如加.减 #include <stdio.h>int main(void){int i;char x[20]="0123456789ABC ...

  5. C指针原理(16)-C指针基础

    2.指向数组的指针 #include <stdio.h>int main(void){int i;char x[20]="0123456789ABCDEFGHIJ";f ...

  6. C指针原理(13)-C指针基础

    规范路径格式,win32(windows环境下,路径之间的各个目录分隔将"\"改为"/",用"/"分隔,这样的好处是在UNIX和WINDOW ...

  7. C指针原理(14)-C指针基础

    make与makefile make是一个工具程序(Utility software),经由读取叫做"makefile"的文件,自动化建构软件.它是一种转化文件形式的工具,转换的目 ...

  8. C指针原理(12)-C指针基础

    tcctok.h定义了C语言的词法分析的基本元素,主要定义了关键字. / keywords /DEF(TOK_INT, "int")DEF(TOK_VOID, "void ...

  9. C语言-第21课 - 指针基础

    第21课 - 指针基础 口诀:加*符号变大门 变量回顾 既然程序中的变量只是一段储存空间的别名,那么是不是必须通过这个别名才能使用这段存储空间?我们看下面的例子: #include<stdio. ...

最新文章

  1. 设置默认settings文件_Django 学习笔记系列 之 settings.py 设定
  2. 利用LSTM(长短期记忆网络)来处理脑电数据
  3. 我们真的仍然需要32位JVM吗?
  4. 【牛客 - 331G】炫酷数字(反素数打表 或 扩展埃式筛法,结论)
  5. 设计模式笔记8: 观察者模式
  6. Python中FileIO
  7. 官宣!苹果3月25日发布会邀请函派出:服务为主硬件为辅
  8. SCCM2007系列教程之八资产管理
  9. linux服务器配置python环境_服务器python环境配置福利,CentOS ,Linux 一键下载python3和环境配置...
  10. 一个tile布局的下拉框
  11. [重要] Cocos2dx 3.0 PageView ListView 在Android设备下背景显示为绿色的问题的解决方案
  12. 文本生成系列之transformer结构扩展(三)
  13. Java中 intValue,parseInt,Valueof 这三个关键字的区别
  14. MQTT自定义透传_DTU连接阿里云
  15. matlab中画三瓣花瓣,如何绘制漂亮的“花瓣”韦恩图?
  16. PyTorch学习笔记(10)——上采样和PixelShuffle
  17. mybatis-plus配置(包含分页插件)
  18. C++实验4-项目7穷举法解决组合问题-百钱百鸡问题
  19. 嵌入式系统与通用计算机系统的区别,嵌入式操作系统和通用计算机系统两者有什么不同之处...
  20. 遗传算法(python实现,虚拟机中运行)

热门文章

  1. 实时滚动图表绘制方法: LightningChart教程 + 源码下载
  2. Open/Close Port in Centos
  3. MS SQL数据库日志压缩方法[转]
  4. 使用IntelliJ IDEA 2016创建maven管理的Java Web项目
  5. arcgis api for js图层显示控制
  6. 云计算学习(2-4)云计算的案例
  7. 【filezilla】 ubuntu下安装filezilla
  8. Python快速学习09: 函数的参数
  9. java发送jsp表格邮件_javaweb收发邮件 servler+jsp实现(一)
  10. Python中常用的高阶函数