我们使用汇编器as
和链接器ld
即可。
使用如下方法进行汇编和链接:
- as --32 -o test.o test.s
- ld -m elf_i386 -o test test.o
如果要调用libc
,需要先安装32位的libc
,例如sudo apt-get install libc6:i386
。
这时的汇编和链接方法如下:
- as --32 -o test.o test.s
- ld -dynamic-linker /lib/ld-linux.so.2 -m elf_i386 -o test test.o /lib/i386-linux-gnu/libc.so.6
不过最好是给libc做个软链接:sudo ln -s /lib/i386-linux-gnu/libc.so.6 /lib/i386-linux-gnu/libc.so
,链接就直接ld -lc -dynamic-linker /lib/ld-linux.so.2 -m elf_i386 -o test test.o
即可。
几个示例程序:
输出所有命令行参数:
- .text
- .globl _start
- _start:
- popl %ecx
- vnext:
- popl %ecx
- 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
- movl $1, %ebx
- int $0x80
- jmp vnext
- exit:
- movl $1, %eax
- xorl %ebx, %ebx
- int $0x80
perl
输出cpuid信息:
- .section .data
- output:
- .asciz "The processor vendor id is '%s'\n"
- .section .bss
- .lcomm buffer, 12
- .section .text
- .globl _start
- _start:
- movl $0, %eax
- cpuid
- movl $buffer, %edi
- movl %ebx, (%edi)
- movl %edx, 4(%edi)
- movl %ecx, 8(%edi)
- push $buffer
- push $output
- call printf
- addl $8, %esp
- push $0
- call exit
perl
Comments
注:如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理。