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. memcache and redis 的区别

    memcache和redis都属于缓存但是memcache的存储大小是收到 限制的memcache的 键值长度是250,内存的大小限制是1M并且memcache不支持数据的持久化缓存 redis支持五 ...

  2. [vue案例的知识点]todo-list

    文章的原材料来自于vue的官方示例:https://cn.vuejs.org/v2/examples/todomvc.html,我们在学习过程中,试着对其中的一些知识点进行记录: 一.浏览器数据存储, ...

  3. c# 如何中List<object>中去掉object对象中的重复列数据?

    //去掉重复 var title = modelList.GroupBy(m => m.Title.ToLower().Trim()).Select(m => new { ID = m.F ...

  4. java复习集合类之List接口

    List 为有序可重复列表 实现List接口的类主要是ArrayList 下面为ArrayList的测试代码 import java.util.ArrayList; public class demo ...

  5. 采用Lambda表达式快速实现实体模型对象转换到DTO

    在项目中,采用code first时建立的模型对象不能直接用于数据传输,需要从新根据需求建立Dto对象 为什么需要建立Dto对象呢? DTO即数据传输对象.之前不明白有些框架中为什么要专门定义DTO来 ...

  6. C++常用特性原理解析

    在我的早期印象中,C++这门语言是软件工程发展过程中,出于对面向对象语言级支持不可或缺的情况下,一群曾经信誓旦旦想要用C统治宇宙的极客们妥协出来的一个高性能怪咖. 它驳杂万分,但引人入胜,出于多(mi ...

  7. js学习笔记 (继续)

    转义字符不断行的空白格 nbsp:1 2半方大的空白 ensp 1 2全方大的空白 emsp 1 2小于号 < <大于号 > >双引号 " "AND符号 & ...

  8. 原生js封装二级城市下拉列表

    闲的蛋疼,封装了个二级城市下拉 先保证html里有 <select id="province" size=1 > </select> <select ...

  9. 整理ViewController的生命周期和加载过程

    按照执行顺序排列 - initWithCoder:通过nib文件初始化时触发 - awakeFromNib:nib文件被加载的时候,会发送一个awakeFromNib的消息到nib文件中的每个对象 p ...

  10. 使用Prerender.io为angular项目做SEO

    现在的项目的为了更好的分工明确,降低耦合都开始采用前后端分离的形式进式开发,我们也采用这种开发形式,前端用angular开发.虽说刚开始也遇各种坑,但是后期熟悉了之后简直爽呆.一个比较大的坑就是SEO ...