汇编-快速排序
第一趟排序
以第一个数-2为标准
deepfuture@deepfuture-laptop:~/private/mytest$ gcc -o testpx1 testpx1.s
deepfuture@deepfuture-laptop:~/private/mytest$ ./testpx1
-90
-2
4
5
432
3
deepfuture@deepfuture-laptop:~/private/mytest$
#######################################
#program: 2010.09.25 #
# myhaspl@myhaspl.com #
#######################################
.section .data
nums:
.int -2,3,4,5,432,-90
gs:
.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 main
main:
movl $gs,%ecx
sub $nums,%ecx#得到元素总长度
sar $2,%ecx#带符号右移,不带符号为shr ,格式为:sar 右移位数,目标
#deepfuture.iteye.com 长度/4,得到元素个数
dec %ecx
movl %ecx,nsize
#nj初始化
movl %ecx,nj
#ni初始化
movl $0,ni
movl $0,ncur
movl ni,%edx
#初始化x,取第一个数
movl nums(,%edx,4),%ebx
movl %ebx,nx
#####################################################################
searchprev:
movl nj,%ecx
movl nx,%ebx
#deepfuture.iteye.com 由后开始向前搜索
prevs:
movl nums(,%ecx,4),%eax
#快速排序 deepfuture.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,ncur
movl %ecx,nj
movl nj,%edx
cmp ni,%edx
je fnumprint
jmp searchnext #找到比x小的数
spnext:
movl %ecx,nj
movl nj,%edx
cmp ni,%edx
je fnumprint
dec %ecx
cmp $0,%ecx
jge prevs#每次循环,%ecx减1,到0循环结束
searchnext:
#deepfuture.iteye.com 由前开始向后搜索
movl ni,%ecx
movl nx,%ebx
nexts:
inc %ecx#error过界
movl nums(,%ecx,4),%eax
cmp %ebx,%eax
jle 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,%edx
cmp ni,%edx
je fnumprint
jmp searchprev #找到比x大的数
snnext:
movl %ecx,ni
movl nj,%edx
cmp ni,%edx
je fnumprint
cmp nsize,%ecx
jle nexts
################################################################################
fnumprint:
movl $0,ncur
loopnext:
movl ncur,%edi
movl nums(,%edi,4),%edx
push %edx
push $gs
call printf
movl ncur,%edi
inc %edi
movl %edi,ncur
cmp nsize,%edi
jle loopnext
push $0
call exit
#######################################
#program: 2010.09.27 #
# #
# 快速排序-汇编 #
#######################################
.section .data
nums:
.int 2,2,3,4,1,3,1,-11,98,-22,22
gs:
.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 main
main:
movl $gs,%ecx
sub $nums,%ecx#得到元素总长度
sar $2,%ecx#带符号右移,不带符号为shr ,格式为:sar 右移位数,目标
#deepfuture.iteye.com 长度/4,得到元素个数
dec %ecx
movl %ecx,nsize
#deepfuture.iteye.com
#nj初始化
movl %ecx,nj
#ni初始化
movl $0,ni
movl $0,ncur
movl ni,%edx
movl $1,count
#初始化x,取第一个数
movl nums(,%edx,4),%ebx
movl %ebx,nx
push ncur
push ni
push nj
push nx
######################
beginsearch:
pop nx
pop nj
pop ni
pop ncur
#deepfuture.iteye.com
movl ni,%eax
movl %eax,oldni
movl nj,%eax
movl %eax,oldnj
#deepfuture.iteye.com
subl $1,count
#####################################################################
searchprev:
movl nj,%ecx
movl nx,%ebx
#deepfuture.iteye.com 由后开始向前搜索
prevs:
movl nums(,%ecx,4),%eax
#快速排序 deepfuture.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,ncur
movl %ecx,nj
#deepfuture.iteye.com
movl nj,%edx
cmp ni,%edx
je fnumend
jmp searchnext #找到比x小的数
spnext:
movl %ecx,nj
movl nj,%edx
cmp ni,%edx
je fnumend
dec %ecx
cmp $0,%ecx
jge prevs#每次循环,%ecx减1,到0循环结束
searchnext:
#deepfuture.iteye.com 由前开始向后搜索
movl ni,%ecx
movl nx,%ebx
nexts:
inc %ecx#error过界
movl nums(,%ecx,4),%eax
cmp %ebx,%eax
jle 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,%edx
cmp ni,%edx
je fnumend
#deepfuture.iteye.com
jmp searchprev #找到比x大的数
snnext:
movl %ecx,ni
movl nj,%edx
cmp ni,%edx
je fnumend
cmp oldnj,%ecx
jle nexts
################################################################################
fnumend:#递归搜索
###################
#right
rights:
movl ncur,%ebx
movl oldnj,%eax
subl %ebx,%eax#(oldnj-ncur)->%eax
cmp $1,%eax
jle lefts#(oldnj-ncur)<=1,左边只有一个元素,已经处理完,不需要递归
#deepfuture.iteye.com
movl ncur,%eax
inc %eax
push %eax#ncur
push %eax#ni
movl %eax,%edx
push oldnj#nj
#deepfuture.iteye.com
movl nums(,%edx,4),%eax
push %eax#nx
addl $1,count
###################
#left
lefts:
movl ncur,%eax
movl oldni,%ebx
subl %ebx,%eax#(ncur-oldni)->%eax
cmp $1,%eax
jle nextsearch#(ncur-oldni)<=1,右边只有一个元素,已经处理完,不需要递归
push oldni#ncur
push oldni#ni
#deepfuture.iteye.com
movl ncur,%eax
dec %eax
push %eax#nj
movl oldni,%edx
movl nums(,%edx,4),%eax
push %eax#nx
addl $1,count
###################
nextsearch:
cmp $0,count
jg beginsearch
################################################################################
fnumprint:
movl $0,ncur
loopnext:
movl ncur,%edi
movl nums(,%edi,4),%edx
push %edx
push $gs
call printf
movl ncur,%edi
inc %edi
movl %edi,ncur
cmp nsize,%edi
jle loopnext
push $0
call exit
deepfuture@deepfuture-laptop:~/private/mytest$ gcc -o testpx testpx.sbr/>deepfuture@deepfuture-laptop:~/private/mytest$ ./testpx
-22
-11
1
1
2
2
3
3
4
22
98
©著作权归作者所有:来自51CTO博客作者myhaspl的原创作品,如需转载,请注明出处,否则将追究法律责任