linux-汇编-快速排序(2)

#######################################
#program:  liuxing     2010.09.27     # 
#          deepfuture.iteye.com     #
#           快速排序-汇编            # 
#######################################
.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.s
deepfuture@deepfuture-laptop:~/private/mytest$ ./testpx
-22
-11
1
1
2
2
3
3
4
22
98
deepfuture@deepfuture-laptop:~/private/mytest$

 

你可能感兴趣的:(C++/C/lisp)