This's my first version.The logic is simple, just the selection sort.

I spent much time learning how to write AT&T assembly on 64-bit Linux.almost all books just talk about 32-bit assembly.

Such as registers, on 64-bit linux, rax, rbx, rcx..... are all 8 bytes. not like eax,ebx,ecx 4 bytes.

And the differences with the use of libraries such as printf.32-bit AT&T assembly push the parameters before calling printf.but 64-bit AT&T assembly saving the parameters in registers such as rsi or rdi before calling printf.

movq     .quad     8bytes 64-bit

movl      .long       4bytes 32-bit

movw     .word     2bytes 16-bit

movb     .byte       1bytes 8-bit

  1. # func: selection sort algorithm
  2. # by whoami
  3. # Oct -
  4. # rdx --- i, rax --- min, rcx --- j, rbx --- tmp
  5.  
  6. .section .data
  7. data_item:
  8. .quad ,,,,,,,,,,,,,
  9. before_sort:
  10. .asciz "sorted nums:\n"
  11. sort_output_format:
  12. .asciz "%d\n"
  13. .section .text
  14. .globl _start
  15. _start:
  16. movq $, %rdx
  17. out_loop: # outer loop
  18. movq %rdx, %rax
  19. movq data_item(,%rdx,), %rbx # if arr[i] == print all nums sorted and exit.
  20. cmp $, %rbx
  21. je print_arr
  22. movq %rdx, %rcx
  23. incq %rcx
  24. inner_loop: # inner loop, get the min-value
  25. cmp $, data_item(,%rcx,)
  26. je inner_loop_exit
  27. movq data_item(,%rax,), %rbx
  28. cmp %ebx, data_item(,%rcx,)
  29. jg not_change_min
  30. movq %rcx, %rax
  31. not_change_min:
  32. incq %rcx
  33. jmp inner_loop
  34. inner_loop_exit:
  35. movq data_item(,%rdx,), %rbx # swap the value, of arr[i] and the min-value.
  36. movq data_item(,%rax,), %rdi
  37. movq %rdi, data_item(,%rdx,)
  38. movq %rbx, data_item(,%rax,)
  39. incq %rdx
  40. jmp out_loop # inner loop exits.
  41.  
  42. print_arr:
  43. movq $, %rbx # print 'sorted nums:'
  44. movq $before_sort, %rdi
  45. call printf
  46. print_loop: # print all nums sorted in a loop
  47. movq data_item(,%rbx,),%rax
  48. cmp $, %rax
  49. je end
  50. movq $sort_output_format, %rdi
  51. movq %rax, %rsi
  52. call printf
  53. incq %rbx
  54. jmp print_loop
  55. end: # program ends.
  56. movq $, %rdi
  57. movq $, %rax
  58. syscall

Using assembly writing algorithm programs的更多相关文章

  1. writing concurrent programs

    Computer Systems A Programmer's Perspective Second Edition To this point in our study of computer sy ...

  2. Practical Go: Real world advice for writing maintainable Go programs

    转自:https://dave.cheney.net/practical-go/presentations/qcon-china.html?from=timeline   1. Guiding pri ...

  3. 32 Profiling Go Programs 分析go语言项目

    Profiling Go Programs  分析go语言项目 24 June 2011 At Scala Days 2011, Robert Hundt presented a paper titl ...

  4. Writing Reentrant and Thread-Safe Code(译:编写可重入和线程安全的代码)

    Writing Reentrant and Thread-Safe Code 编写可重入和线程安全的代码 (http://www.ualberta.ca/dept/chemeng/AIX-43/sha ...

  5. zhihu spark集群,书籍,论文

    spark集群中的节点可以只处理自身独立数据库里的数据,然后汇总吗? 修改 我将spark搭建在两台机器上,其中一台既是master又是slave,另一台是slave,两台机器上均装有独立的mongo ...

  6. The Go Programming Language. Notes.

    Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...

  7. K老在拿图灵奖时的发言:Computer Programming as an Art

    很多话说得很透彻,把一些觉比较精彩的摘抄一下. ... It seems to me that if the authors I studied were writing today, they wo ...

  8. bsdasm

    bsdasm 来源 http://www.int80h.org/bsdasm/ Preface by G. Adam StanislavWhiz Kid Technomagic Assembly la ...

  9. PatentTips - Safe general purpose virtual machine computing system

    BACKGROUND OF THE INVENTION The present invention relates to virtual machine implementations, and in ...

随机推荐

  1. z-stack组网过程

    z-stack组网分:协调器建立网络.路由器和终端加入网络 暂时只记录第一次上电建立网络的过程,至于开启NV_RESTORE后,恢复原有的网络则暂时不分析. 一.协调器建立网络: 1.ZDO层的ZDA ...

  2. 关于利用bat文件调用exe批量处理文件下的文件的问题

    for %%i in (E:\radar_20120721\sjz_sa\*.bin) do start/wait radas.exe -i=%%i -o=E:\longjiang\out 找到 文件 ...

  3. linux查看是什么操作系统是什么命令

    https://zhidao.baidu.com/question/361519585968730492.html

  4. 测试--easymock的使用

    使用场景:对于调用其它类中的方法,但是还没有编写完,使用easymock进行单元测试,它提供这些没有编写完的代码期待的默认值. 使用步骤: step1: pom引入: <dependency&g ...

  5. tomcat -ROOT 与webapps 的关系,关于部署的一些问题

    现象:之前遇到很奇怪的问题,发完版之后没有效果,页面还是读取上一版的. 反复查找原因发现  http://localhost:8080/mobie 这个路径下的页面是正常的, 而  http://lo ...

  6. SQL Server 里的递归查询

    http://www.360doc.com/content/13/0607/11/8463843_291221684.shtml

  7. 定时任务crontab 例子

    查看定时任务格式 [root@centos ~]# vim /etc/crontab 1 SHELL=/bin/bash 2 PATH=/sbin:/bin:/usr/sbin:/usr/bin 3 ...

  8. Odoo 二次开发教程(五)-新API的介绍与应用

    [关于odoo新API的介绍,Internet上资料很少,或者不够完整详实,这会对初学者造成很大的困惑,本篇的目的就是希望能帮助新手了解新API的大概] odoo 新api的实现是借助于python装 ...

  9. *POJ 1222 高斯消元

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9612   Accepted: 62 ...

  10. c++ 宏定义声明类,并在类中实现回调

    #include <iostream> #include <windows.h> #include <string> using namespace std; ty ...