汇编的学习和熟悉
如果直接从理论看起,会太空泛
从程序来进行实践,是加深理论的有效方法
《使用Linux汇编语言》里的例子
exit.s
.section .data
.section .text
.globl _start
_start:
movl $1, %eax
movl $0, %ebx
int $0x80
进行编译
as exit.s -o exit.o
ld exit.o -o exit
./exit
echo $?
0
vi max.s
.section .data
data_items:
.long 3,67,8,9,11,0
.section .text
.globl _start
_start:
movl $0, %edi
movl data_items(,%edi,4), %eax
movl %eax, %ebx
start_loop:
cmpl $0, %eax
je loop_exit
incl %edi
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax
jle start_loop
movl %eax, %ebx
jmp start_loop
loop_exit:
movl $1, %eax
int $0x80
汇编
as max.s -o max.o
当有语法错误的时候,会有类似的报错
max.s: Assembler messages:
max.s:5: Error: junk at end of line, first unrecognized character is `.'
max.s:16: Error: no such instruction: `cmp1 $0,%eax'
这也可以看出,汇编语言较为繁琐
链接
ld max.o -o max
执行
./max
打印程序的返回值
echo $?
67