linux下汇编实例

代码:

$ cat hello.s
.text
.global _start

msg:
.ascii "Hello World!\n"
msg_end:
.equ len, msg_end - msg
.equ SYS_write, 1
.equ SYS_exit, 60

_start:
mov $SYS_write, %rax    # system call number (sys_write)
mov $1, %rdi            # file descriptor (stdout)
mov $msg, %rsi          # message to write
mov $len, %rdx          # message length.
syscall                 # previous 'int $0x80' in i386

mov $SYS_exit, %rax     # system call number (sys_exit)
mov $0, %rdi            # exit (0)
syscall                 # previous 'int $0x80' in i386

运行:

$ as -o hello.o hello.s
$ ld -o hello hello.o
$ ./hello
Hello World!

反汇编:

$ objdump --disassemble-all hello

hello:     文件格式 elf64-x86-64


Disassembly of section .text:

0000000000400078 <msg>:
  400078:	48                   	rex.W
  400079:	65 6c                	gs insb (%dx),%es:(%rdi)
  40007b:	6c                   	insb   (%dx),%es:(%rdi)
  40007c:	6f                   	outsl  %ds:(%rsi),(%dx)
  40007d:	20 57 6f             	and    %dl,0x6f(%rdi)
  400080:	72 6c                	jb     4000ee <_start+0x69>
  400082:	64 21 0a             	and    %ecx,%fs:(%rdx)

0000000000400085 <_start>:
  400085:	48 c7 c0 01 00 00 00 	mov    $0x1,%rax
  40008c:	48 c7 c7 01 00 00 00 	mov    $0x1,%rdi
  400093:	48 c7 c6 78 00 40 00 	mov    $0x400078,%rsi
  40009a:	48 c7 c2 0d 00 00 00 	mov    $0xd,%rdx
  4000a1:	0f 05                	syscall 
  4000a3:	48 c7 c0 3c 00 00 00 	mov    $0x3c,%rax
  4000aa:	48 c7 c7 00 00 00 00 	mov    $0x0,%rdi
  4000b1:	0f 05                	syscall 
$ od -x hello
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000020 0002 003e 0001 0000 0085 0040 0000 0000
0000040 0040 0000 0000 0000 0238 0000 0000 0000
0000060 0000 0000 0040 0038 0001 0040 0005 0002
0000100 0001 0000 0005 0000 0000 0000 0000 0000
0000120 0000 0040 0000 0000 0000 0040 0000 0000
0000140 00b3 0000 0000 0000 00b3 0000 0000 0000
0000160 0000 0020 0000 0000 6548 6c6c 206f 6f57
0000200 6c72 2164 480a c0c7 0001 0000 c748 01c7
0000220 0000 4800 c6c7 0078 0040 c748 0dc2 0000
0000240 0f00 4805 c0c7 003c 0000 c748 00c7 0000
0000260 0f00 0005 0000 0000 0000 0000 0000 0000
0000300 0000 0000 0000 0000 0000 0000 0000 0000
0000320 0000 0000 0003 0001 0078 0040 0000 0000
0000340 0000 0000 0000 0000 0001 0000 0004 fff1
0000360 0000 0000 0000 0000 0000 0000 0000 0000
0000400 0009 0000 0000 0001 0078 0040 0000 0000
0000420 0000 0000 0000 0000 000d 0000 0000 0001
0000440 0085 0040 0000 0000 0000 0000 0000 0000
0000460 0015 0000 0000 fff1 000d 0000 0000 0000
0000500 0000 0000 0000 0000 0019 0000 0000 fff1
0000520 0001 0000 0000 0000 0000 0000 0000 0000
0000540 0023 0000 0000 fff1 003c 0000 0000 0000
0000560 0000 0000 0000 0000 0031 0000 0010 0001
0000600 0085 0040 0000 0000 0000 0000 0000 0000
0000620 002c 0000 0010 0001 00b3 0060 0000 0000
0000640 0000 0000 0000 0000 0038 0000 0010 0001
0000660 00b3 0060 0000 0000 0000 0000 0000 0000
0000700 0010 0000 0010 0001 00b8 0060 0000 0000
0000720 0000 0000 0000 0000 6800 6c65 6f6c 6f2e
0000740 6d00 6773 6d00 6773 655f 646e 6c00 6e65
0000760 5300 5359 775f 6972 6574 5300 5359 655f
0001000 6978 0074 5f5f 7362 5f73 7473 7261 0074
0001020 655f 6164 6174 0000 732e 6d79 6174 0062
0001040 732e 7274 6174 0062 732e 7368 7274 6174
0001060 0062 742e 7865 0074 0000 0000 0000 0000
0001100 0000 0000 0000 0000 0000 0000 0000 0000
*
0001160 0000 0000 0000 0000 001b 0000 0001 0000
0001200 0006 0000 0000 0000 0078 0040 0000 0000
0001220 0078 0000 0000 0000 003b 0000 0000 0000
0001240 0000 0000 0000 0000 0001 0000 0000 0000
0001260 0000 0000 0000 0000 0011 0000 0003 0000
0001300 0000 0000 0000 0000 0000 0000 0000 0000
0001320 0217 0000 0000 0000 0021 0000 0000 0000
0001340 0000 0000 0000 0000 0001 0000 0000 0000
0001360 0000 0000 0000 0000 0001 0000 0002 0000
0001400 0000 0000 0000 0000 0000 0000 0000 0000
0001420 00b8 0000 0000 0000 0120 0000 0000 0000
0001440 0004 0000 0008 0000 0008 0000 0000 0000
0001460 0018 0000 0000 0000 0009 0000 0003 0000
0001500 0000 0000 0000 0000 0000 0000 0000 0000
0001520 01d8 0000 0000 0000 003f 0000 0000 0000
0001540 0000 0000 0000 0000 0001 0000 0000 0000
0001560 0000 0000 0000 0000
0001570

 

转载于:https://my.oschina.net/u/2245781/blog/913931

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值