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. java对象转换成json

    package com.bjs.acrosstime.utils; import java.util.ArrayList; import java.util.Date; import java.uti ...

  2. 单独编译使用WebRTC的音频处理模块

    块,每块个点,(12*64=768采样)即AEC-PC仅能处理48ms的单声道16kHz延迟的数据,而 - 加载编译好的NS模块动态库 接下来只需要按照 此文 的描述在 android 的JAVA代码 ...

  3. Python3的tkinter写一个简单的小程序

    一.这个学期开始学习python,但是看了python2和python3,最后还是选择了python3 本着熟悉python的原因,并且也想做一些小程序来增加自己对python的熟练度.所以写了一个简 ...

  4. mybatis自增长插入id

    第一种: <insert id="insertUser" parameterClass="ibatis.User"> <selectKey r ...

  5. 【MongoDB初识】-结合C#简单使用,驱动2.x

    public static Students GetEntityByName(string conStr, string userName = "bj") { Students s ...

  6. kafka的log存储解析——topic的分区partition分段segment以及索引等

    转自:http://blog.csdn.net/jewes/article/details/42970799 引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相 ...

  7. Shell 快捷键

    输入bind -P可以查看所有的键盘绑定 Ctrl + Shift + '-' 缩小shell框Ctrl + Shift + ‘+' 放大shell框 CTRL相关的快捷键Ctrl-A 相当于HOME ...

  8. Visual Studio 2013执行项目报错:HTTP 错误 500.22

    转至:http://www.codingwhy.com/410.html 具体报错 HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ...

  9. 文本选择问题: css & js

    CSS: /*Disable browser selection*/ .disableselect { -webkit-user-select: none; -moz-user-select: non ...

  10. js中自定义构造函数讲解

    什么是构造函数? 构造函数其实就是一个函数,只是用途和普通函数,不太一样, 构造函数一般用于初始化对象 <script> function Person(){ this.name=&quo ...