欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

C指针原理(22)-C指针基础-att汇编-快速排序

发布时间:2025/3/12 编程问答 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C指针原理(22)-C指针基础-att汇编-快速排序 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

第一趟排序

以第一个数-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个字节.lcomm nj,4#int为32位,4个字节 .lcomm nsize,4#int为32位,4个字节,数组长度 .lcomm nx,4 .lcomm ncur,4 .section .text.globl mainmain:movl $gs,%ecxsub $nums,%ecx#得到元素总长度sar $2,%ecx#带符号右移,不带符号为shr ,格式为:sar 右移位数,目标#xxx.iteye.com 长度/4,得到元素个数dec %ecxmovl %ecx,nsize#nj初始化movl %ecx,nj#ni初始化movl $0,nimovl $0,ncurmovl ni,%edx#初始化x,取第一个数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为有符号数,jae为无符号数,>=#%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小的数 spnext: movl %ecx,njmovl nj,%edxcmp ni,%edx je fnumprint dec %ecx cmp $0,%ecx jge prevs#每次循环,%ecx减1,到0循环结束 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大的数 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个字节.lcomm nj,4#int为32位,4个字节 .lcomm nsize,4#int为32位,4个字节,数组长度 .lcomm nx,4 .lcomm ncur,4 .lcomm oldni,4#int为32位,4个字节.lcomm oldnj,4#int为32位,4个字节 .lcomm oldnsize,4#int为32位,4个字节,数组长度.lcomm count,4 .section .text.globl mainmain:movl $gs,%ecxsub $nums,%ecx#得到元素总长度sar $2,%ecx#带符号右移,不带符号为shr ,格式为:sar 右移位数,目标#xxx.iteye.com 长度/4,得到元素个数dec %ecxmovl %ecx,nsize#xxx.iteye.com #nj初始化movl %ecx,nj#ni初始化movl $0,nimovl $0,ncurmovl ni,%edxmovl $1,count#初始化x,取第一个数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为有符号数,jae为无符号数,>=#%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小的数 spnext: movl %ecx,njmovl nj,%edxcmp ni,%edx je fnumend dec %ecx cmp $0,%ecx jge prevs#每次循环,%ecx减1,到0循环结束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大的数 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,左边只有一个元素,已经处理完,不需要递归 #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,右边只有一个元素,已经处理完,不需要递归 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

总结

以上是生活随笔为你收集整理的C指针原理(22)-C指针基础-att汇编-快速排序的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。