C/C++知识点之C指针原理(22)-C指针基础-at&t汇编-快速排序
小标 2019-02-19 来源 : 阅读 1155 评论 0

摘要:本文主要向大家介绍了 C/C++知识点之C指针原理(22)-C指针基础-at&t汇编-快速排序,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了 C/C++知识点之C指针原理(22)-C指针基础-at&t汇编-快速排序,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

C/C++知识点之C指针原理(22)-C指针基础-at&t汇编-快速排序

汇编-快速排序


第一趟排序


以第一个数-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
     

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved