If the assembly language to achieve the following C language functions, the compilation environment Ubuntu14.04 (32-bit).
#include <stdio.h>intMain () {intA[] = {3,0,5,1,4,6,2,9,8,7}; inti; for(i =0; i<Ten; i++) printf ("%d\n", A[i]); return 0;}
. Section. Dataarray:.int 3,0,5,1,4,6,2,9,8,7 Len:.int Ten format:. Asciz"%d\n". Section. Text.global _start_start:PUSHL%ebp movl%esp,%EBP Subl $ -,%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 stackPagerParray Addl $ -,%esp popl%ebp MOVL $0, (%ESP) #deallocate spacePagerExitParray:PUSHL%ebp movl%esp,%EBPPush%EBX MOVL8(%EBP),%edx #get &array movl A(%EBP),%ebx #get len Movl $0,%ecx #计数器i初始化为0CMP%ecx,%ebxJle. Done.Loop:movl (%edx,%ECX,4),%eaxPagerPrintInc%ecx #i每次加1CMP%ecx,%ebxJG. Loop.Done :popl%ebx popl%ebpret Print:pushl%edx pushl%ecx pushl%eax pushl $formatPagerprintf Addl $8,%esp popl%ecx popl%edxret
As Parray.s-o parray.o
Ld-lc-i/lib/ld-linux.so.2 Parray.o-o Parray
./parray
Assembly Language Learning Series print array implementation