假如汇编语言要实现如下C语言的功能,编译环境Ubuntu14.04(32位)。
#include<stdio.h> int main(){ int a[] = {3, 0, 5, 1, 4, 6, 2, 9, 8, 7}; int i; for(i = 0; i< 10; i++) printf("%d ", a[i]); return 0; }
- 代码
.section .data array: .int 3, 0, 5, 1, 4, 6, 2, 9, 8, 7 len: .int 10 format: .asciz "%d " .section .text .global _start _start: pushl %ebp movl %esp, %ebp subl $20, %esp #allocate space movl $array, %edx movl %edx, (%esp) #store &array on the stack movl len, %ecx movl %ecx, 4(%esp) #store len on the stack call parray addl $20, %esp popl %ebp movl $0, (%esp) #deallocate space call exit parray: pushl %ebp movl %esp, %ebp push %ebx movl 8(%ebp), %edx #get &array movl 12(%ebp), %ebx #get len movl $0, %ecx #计数器i初始化为0 cmp %ecx, %ebx jle .done .loop: movl (%edx, %ecx, 4), %eax call print inc %ecx #i每次加1 cmp %ecx, %ebx jg .loop .done: popl %ebx popl %ebp ret print: pushl %edx pushl %ecx pushl %eax pushl $format call printf addl $8, %esp popl %ecx popl %edx ret
- 编译
as parray.s -o parray.o
- 链接
ld -lc -I /lib/ld-linux.so.2 parray.o -o parray
- 执行
./parray