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

# func: selection sort algorithm
# by whoami
# Oct -
# rdx --- i, rax --- min, rcx --- j, rbx --- tmp .section .data
data_item:
.quad ,,,,,,,,,,,,,
before_sort:
.asciz "sorted nums:\n"
sort_output_format:
.asciz "%d\n"
.section .text
.globl _start
_start:
movq $, %rdx
out_loop: # outer loop
movq %rdx, %rax
movq data_item(,%rdx,), %rbx # if arr[i] == print all nums sorted and exit.
cmp $, %rbx
je print_arr
movq %rdx, %rcx
incq %rcx
inner_loop: # inner loop, get the min-value
cmp $, data_item(,%rcx,)
je inner_loop_exit
movq data_item(,%rax,), %rbx
cmp %ebx, data_item(,%rcx,)
jg not_change_min
movq %rcx, %rax
not_change_min:
incq %rcx
jmp inner_loop
inner_loop_exit:
movq data_item(,%rdx,), %rbx # swap the value, of arr[i] and the min-value.
movq data_item(,%rax,), %rdi
movq %rdi, data_item(,%rdx,)
movq %rbx, data_item(,%rax,)
incq %rdx
jmp out_loop # inner loop exits. print_arr:
movq $, %rbx # print 'sorted nums:'
movq $before_sort, %rdi
call printf
print_loop: # print all nums sorted in a loop
movq data_item(,%rbx,),%rax
cmp $, %rax
je end
movq $sort_output_format, %rdi
movq %rax, %rsi
call printf
incq %rbx
jmp print_loop
end: # program ends.
movq $, %rdi
movq $, %rax
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. mysql乐观锁总结和实践

    乐观锁介绍: 乐观锁( Optimistic Locking ) 相对悲观锁而言,乐观锁假设认为数据一般情况下不会造成冲突,所以在数据进行提交更新的时候,才会正式对数据的冲突与否进行检测,如果发现冲突 ...

  2. Vue - 在v-repeat中使用计算属性

    1.从后端获取JSON数据集合后,对单条数据应用计算属性,在Vue.js 0.12版本之前可以在v-repeat所在元素上使用v-component指令 在Vue.js 0.12版本之后使用自定义元素 ...

  3. 【虚拟机】oracle Virtual Box4.2.6虚拟机正在运行的过程中删除了其上的一个备份,之后虚拟机就无法使用了

    原因未知,解决方法没有,网上也没有找到相关的解决方法.偶然的操作导致

  4. [BZOJ1131][POI2008] Sta 树的深度

    Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...

  5. Nodejs 创建web服务

    // 加载http服务包 var http = require("http"); // 创建服务 var server = http.createServer(requestLis ...

  6. 利用浏览器LocalStorage缓存图片,视频文件

    文章路径:https://hacks.mozilla.org/2012/02/saving-images-and-files-in-localstorage/

  7. 关于C#调用非托管动态库方式的性能疑问

    最近的项目中,因为一些原因,需要C#调用非托管(这里为C++)的动态库.网上喜闻乐见的方式是采用静态(DllImport)方式进行调用.偶然在园子里看到可以用动态(LoadLibrary,GetPro ...

  8. flex的兼容

    父容器的 display 属性: .box{ display: -webkit-box; /* 老版本语法: Safari, iOS, Android browser, older WebKit br ...

  9. bzoj4325: NOIP2015 斗地主(爆搜+模拟)

    去年的我还不会打斗地主呵呵 觉得这道题挺难的..抄了一遍题解,感触挺多的= = 首先出牌的方式太多了不能每次都枚举所有的出牌方式, 于是分成两部分:1.顺子 2.带牌等其他 每次dfs都搜顺子,而且顺 ...

  10. php 图片上传的公共方法(按图片宽高缩放或原图)

    写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...