C指针原理(22)-C指针基础-att汇编-快速排序
生活随笔
收集整理的這篇文章主要介紹了
C指针原理(22)-C指针基础-att汇编-快速排序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
第一趟排序
以第一個數(shù)-2為標準
xxx@xxx-laptop:~/private/mytest$ gcc -o testpx1 testpx1.s
xxx@xxx-laptop:~/private/mytest$ ./testpx1
-90
-2
4
5
432
3
xxx@xxx-laptop:~/private/mytest$
####################################### #program: 2010.09.25 # # # ####################################### .section .datanums:.int -2,3,4,5,432,-90gs:.ascii "%d\n" .section .bss.lcomm ni,4#int為32位,4個字節(jié).lcomm nj,4#int為32位,4個字節(jié) .lcomm nsize,4#int為32位,4個字節(jié),數(shù)組長度 .lcomm nx,4 .lcomm ncur,4 .section .text.globl mainmain:movl $gs,%ecxsub $nums,%ecx#得到元素總長度sar $2,%ecx#帶符號右移,不帶符號為shr ,格式為:sar 右移位數(shù),目標#xxx.iteye.com 長度/4,得到元素個數(shù)dec %ecxmovl %ecx,nsize#nj初始化movl %ecx,nj#ni初始化movl $0,nimovl $0,ncurmovl ni,%edx#初始化x,取第一個數(shù)movl nums(,%edx,4),%ebx movl %ebx,nx#####################################################################searchprev:movl nj,%ecxmovl nx,%ebx #xxx.iteye.com 由后開始向前搜索 prevs:movl nums(,%ecx,4),%eax#快速排序 xxx.iteye.com cmp %ebx,%eax#%eax與%ebx比較,比如%eax比%ebx大jge spnext#jge為有符號數(shù),jae為無符號數(shù),>=#%eax比%ebx小xchg %ebx,%eax#交換值movl %eax,nums(,%ecx,4)movl ncur,%edx movl %ebx,nums(,%edx,4)movl %ecx,ncurmovl %ecx,njmovl nj,%edxcmp ni,%edx je fnumprint jmp searchnext #找到比x小的數(shù) spnext: movl %ecx,njmovl nj,%edxcmp ni,%edx je fnumprint dec %ecx cmp $0,%ecx jge prevs#每次循環(huán),%ecx減1,到0循環(huán)結(jié)束 searchnext:#xxx.iteye.com 由前開始向后搜索 movl ni,%ecx movl nx,%ebx nexts: inc %ecx#error過界movl nums(,%ecx,4),%eaxcmp %ebx,%eaxjle snnext#jle,<=#%eax比%ebx大xchg %ebx,%eax#交換值movl %eax,nums(,%ecx,4)movl ncur,%edx movl %ebx,nums(,%edx,4)movl %ecx,ncur movl %ecx,ni movl nj,%edxcmp ni,%edx je fnumprintjmp searchprev #找到比x大的數(shù) snnext: movl %ecx,ni movl nj,%edxcmp ni,%edx je fnumprintcmp nsize,%ecxjle nexts################################################################################ fnumprint:movl $0,ncur loopnext: movl ncur,%edi movl nums(,%edi,4),%edx push %edxpush $gs call printfmovl ncur,%ediinc %edimovl %edi,ncur cmp nsize,%edi jle loopnext push $0call exit####################################### #program: 2010.09.27 # # # # 快速排序-匯編 # ####################################### .section .datanums:.int 2,2,3,4,1,3,1,-11,98,-22,22gs:.ascii "%d\n" .section .bss.lcomm ni,4#int為32位,4個字節(jié).lcomm nj,4#int為32位,4個字節(jié) .lcomm nsize,4#int為32位,4個字節(jié),數(shù)組長度 .lcomm nx,4 .lcomm ncur,4 .lcomm oldni,4#int為32位,4個字節(jié).lcomm oldnj,4#int為32位,4個字節(jié) .lcomm oldnsize,4#int為32位,4個字節(jié),數(shù)組長度.lcomm count,4 .section .text.globl mainmain:movl $gs,%ecxsub $nums,%ecx#得到元素總長度sar $2,%ecx#帶符號右移,不帶符號為shr ,格式為:sar 右移位數(shù),目標#xxx.iteye.com 長度/4,得到元素個數(shù)dec %ecxmovl %ecx,nsize#xxx.iteye.com #nj初始化movl %ecx,nj#ni初始化movl $0,nimovl $0,ncurmovl ni,%edxmovl $1,count#初始化x,取第一個數(shù)movl nums(,%edx,4),%ebxmovl %ebx,nxpush ncurpush nipush njpush nx######################beginsearch: pop nx pop njpop nipop ncur#xxx.iteye.com movl ni,%eaxmovl %eax,oldnimovl nj,%eaxmovl %eax,oldnj#xxx.iteye.com subl $1,count#####################################################################searchprev:movl nj,%ecxmovl nx,%ebx #xxx.iteye.com 由后開始向前搜索 prevs:movl nums(,%ecx,4),%eax#快速排序 xxx.iteye.com cmp %ebx,%eax#%eax與%ebx比較,比如%eax比%ebx大jge spnext#jge為有符號數(shù),jae為無符號數(shù),>=#%eax比%ebx小xchg %ebx,%eax#交換值movl %eax,nums(,%ecx,4)movl ncur,%edx movl %ebx,nums(,%edx,4)movl %ecx,ncurmovl %ecx,nj#xxx.iteye.com movl nj,%edxcmp ni,%edx je fnumend jmp searchnext #找到比x小的數(shù) spnext: movl %ecx,njmovl nj,%edxcmp ni,%edx je fnumend dec %ecx cmp $0,%ecx jge prevs#每次循環(huán),%ecx減1,到0循環(huán)結(jié)束searchnext:#xxx.iteye.com 由前開始向后搜索 movl ni,%ecx movl nx,%ebx nexts: inc %ecx#error過界movl nums(,%ecx,4),%eaxcmp %ebx,%eaxjle snnext#jle,<=#%eax比%ebx大xchg %ebx,%eax#交換值movl %eax,nums(,%ecx,4)movl ncur,%edx movl %ebx,nums(,%edx,4)movl %ecx,ncur movl %ecx,ni movl nj,%edxcmp ni,%edx je fnumend#xxx.iteye.com jmp searchprev #找到比x大的數(shù) snnext: movl %ecx,ni movl nj,%edxcmp ni,%edx je fnumendcmp oldnj,%ecxjle nexts################################################################################ fnumend:#遞歸搜索####################rightrights:movl ncur,%ebxmovl oldnj,%eaxsubl %ebx,%eax#(oldnj-ncur)->%eaxcmp $1,%eaxjle lefts#(oldnj-ncur)<=1,左邊只有一個元素,已經(jīng)處理完,不需要遞歸 #xxx.iteye.com movl ncur,%eax inc %eax push %eax#ncurpush %eax#nimovl %eax,%edx push oldnj#nj#xxx.iteye.com movl nums(,%edx,4),%eaxpush %eax#nxaddl $1,count####################leftlefts:movl ncur,%eaxmovl oldni,%ebxsubl %ebx,%eax#(ncur-oldni)->%eaxcmp $1,%eaxjle nextsearch#(ncur-oldni)<=1,右邊只有一個元素,已經(jīng)處理完,不需要遞歸 push oldni#ncurpush oldni#ni#xxx.iteye.com movl ncur,%eaxdec %eaxpush %eax#njmovl oldni,%edxmovl nums(,%edx,4),%eaxpush %eax#nxaddl $1,count ###################nextsearch:cmp $0,countjg beginsearch ################################################################################ fnumprint:movl $0,ncur loopnext: movl ncur,%edi movl nums(,%edi,4),%edx push %edxpush $gs call printfmovl ncur,%ediinc %edimovl %edi,ncur cmp nsize,%edi jle loopnext push $0call exitxxx@xxx-laptop:~/private/mytest$ gcc -o testpx testpx.sbr/>xxx@xxx-laptop:~/private/mytest$ ./testpx-22
-11
1
1
2
2
3
3
4
22
98
總結(jié)
以上是生活随笔為你收集整理的C指针原理(22)-C指针基础-att汇编-快速排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: websocket 学习--简单使用,n
- 下一篇: Java中String转Long方式及注