AT&T
格式
|
Intel
格式
|
pushl %eax
|
push eax
|
AT&T
格式
|
Intel
格式
|
pushl $1
|
push 1
|
AT&T
格式
|
Intel
格式
|
addl $1, %eax
|
add eax, 1
|
AT&T
格式
|
Intel
格式
|
movb val, %al
|
mov al, byte ptr val
|
AT&T
格式
|
Intel
格式
|
ljump $section, $offset
|
jmp far section:offset
|
lcall $section, $offset
|
call far section:offset
|
AT&T
格式
|
Intel
格式
|
lret $stack_adjust
|
ret far stack_adjust
|
section:disp(base, index, scale)
|
section:[base + index*scale + disp]
|
disp + base + index * scale
|
AT&T
格式
|
Intel
格式
|
movl -4(%ebp), %eax
|
mov eax, [ebp - 4]
|
movl array(, %eax, 4), %eax
|
mov eax, [eax*4 + array]
|
movw array(%ebx, %eax, 4), %cx
|
mov cx, [ebx + 4*eax + array]
|
movb $4, %fs:(%eax)
|
mov fs:eax, 4
|
#hello.s
.data # 数据段声明
msg : .string "Hello, world!//n" # 要输出的字符串
len = . - msg # 字串长度
.text # 代码段声明
.global _start # 指定入口函数
_start: # 在屏幕上显示一个字符串
movl $len, %edx # 参数三:字符串长度
movl $msg, %ecx # 参数二:要显示的字符串
movl $1, %ebx # 参数一:文件描述符(stdout)
movl $4, %eax # 系统调用号(sys_write)
int $0x80 # 调用内核功能
# 退出程序
movl $0,%ebx # 参数一:退出代码
movl $1,%eax # 系统调用号(sys_exit)
int $0x80 # 调用内核功能
|
; hello.asm
section .data ; 数据段声明
msg db "Hello, world!", 0xA ; 要输出的字符串
len equ $ - msg ; 字串长度
section .text ; 代码段声明
global _start ; 指定入口函数
_start: ; 在屏幕上显示一个字符串
mov edx, len ; 参数三:字符串长度
mov ecx, msg ; 参数二:要显示的字符串
mov ebx, 1 ; 参数一:文件描述符(stdout)
mov eax, 4 ; 系统调用号(sys_write)
int 0x80 ; 调用内核功能
; 退出程序
mov ebx, 0 ; 参数一:退出代码
mov eax, 1 ; 系统调用号(sys_exit)
int 0x80 ; 调用内核功能
|
[xiaowp@gary code]$ as -o hello.o hello.s
|
[xiaowp@gary code]$ nasm -f elf hello.asm
|
[xiaowp@gary code]$ ld -s -o hello hello.o
|
[xiaowp@gary code]$ as --gstabs -o hello.o hello.s
[xiaowp@gary code]$ ld -o hello hello.o
|
[xiaowp@gary doc]$ ald hello
Assembly Language Debugger 0.1.3
Copyright (C) 2000-2002 Patrick Alken
hell ELF Intel 80386 (32 bit), LSB, Executable, Version 1 (current)
Loading debugging symbols...(15 symbols loaded)
ald>
|
ald> disassemble -s .text
Disassembling section .text (0x08048074 - 0x08048096)
08048074 BA0F000000 mov edx, 0xf
08048079 B998900408 mov ecx, 0x8049098
0804807E BB01000000 mov ebx, 0x1
08048083 B804000000 mov eax, 0x4
08048088 CD80 int 0x80
0804808A BB00000000 mov ebx, 0x0
0804808F B801000000 mov eax, 0x1
08048094 CD80 int 0x80
|
ald> break 0x08048088
Breakpoint 1 set for 0x08048088
|
ald> run
Starting program: hello
Breakpoint 1 encountered at 0x08048088
eax = 0x00000004 ebx = 0x00000001 ecx = 0x08049098 edx = 0x0000000F
esp = 0xBFFFF6C0 ebp = 0x00000000 esi = 0x00000000 edi = 0x00000000
ds = 0x0000002B es = 0x0000002B fs = 0x00000000 gs = 0x00000000
ss = 0x0000002B cs = 0x00000023 eip = 0x08048088 eflags = 0x00000246
Flags: PF ZF IF
08048088 CD80 int 0x80
|
ald> next
Hello, world!
eax = 0x0000000F ebx = 0x00000000 ecx = 0x08049098 edx = 0x0000000F
esp = 0xBFFFF6C0 ebp = 0x00000000 esi = 0x00000000 edi = 0x00000000
ds = 0x0000002B es = 0x0000002B fs = 0x00000000 gs = 0x00000000
ss = 0x0000002B cs = 0x00000023 eip = 0x0804808F eflags = 0x00000346
Flags: PF ZF TF IF
0804808F B801000000 mov eax, 0x1
|
ald> help
Commands may be abbreviated.
If a blank command is entered, the last command is repeated.
Type `help
General commands
attach clear continue detach disassemble
enter examine file help load
next quit register run set
step unload window write
Breakpoint related commands
break delete disable enable ignore
lbreak tbreak
|
ssize_t write(int fd, const void *buf, size_t count);
|
void * mmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);c
|
# args.s
.text
.globl _start
_start:
popl %ecx # argc
vnext:
popl %ecx # argv
test %ecx, %ecx # 空指针表明结束
jz exit
movl %ecx, %ebx
xorl %edx, %edx
strlen:
movb (%ebx), %al
inc %edx
inc %ebx
test %al, %al
jnz strlen
movb $10, -1(%ebx)
movl $4, %eax # 系统调用号(sys_write)
movl $1, %ebx # 文件描述符(stdout)
int $0x80
jmp vnext
exit: movl $1,%eax # 系统调用号(sys_exit)
xorl %ebx, %ebx # 退出代码
int $0x80
ret
|
__asm__("asm statements");
|
__asm__("nop");
|
__asm__( "pushl %%eax //n//t"
"movl $0, %%eax //n//t"
"popl %eax");
|
__asm__("asm statements" : outputs : inputs : registers-modified);
|
/* inline.c */
int main()
{
int a = 10, b = 0;
__asm__ __volatile__("movl %1, %%eax;//n//r"
"movl %%eax, %0;"
:"=r"(b) /* 输出 */
:"r"(a) /* 输入 */
:"%eax"); /* 不受影响的寄存器 */
printf("Result: %d, %d//n", a, b);
}
|
限定符
|
意义
|
"m"、"v"、"o"
|
内存单元
|
"r"
|
任何寄存器
|
"q"
|
寄存器eax、ebx、ecx、edx之一
|
"i"、"h"
|
直接操作数
|
"E"和"F"
|
浮点数
|
"g"
|
任意
|
"a"、"b"、"c"、"d"
|
分别表示寄存器eax、ebx、ecx和edx
|
"S"和"D"
|
寄存器esi、edi
|
"I"
|
常数(0至31)
|