Using assembly writing algorithm programs

Posted NONE

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Using assembly writing algorithm programs相关的知识,希望对你有一定的参考价值。

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 1-4 2016
# rdx --- i, rax --- min, rcx --- j, rbx --- tmp

.section .data
data_item:
        .quad 3,67,34,222,45,75,54,34,44,33,22,11,66,0
before_sort:
        .asciz "sorted nums:\\n"
sort_output_format:
        .asciz "%d\\n"
.section .text
.globl _start
_start:
        movq $0, %rdx
        out_loop:                                         # outer loop
                movq %rdx, %rax
                movq data_item(,%rdx,8), %rbx             # if arr[i] == 0 print all nums sorted and exit.
                cmp $0, %rbx
                je print_arr
                movq %rdx, %rcx
                incq %rcx
                inner_loop:                               # inner loop, get the min-value
                        cmp $0, data_item(,%rcx,8)
                        je inner_loop_exit
                        movq data_item(,%rax,8), %rbx
                        cmp %ebx, data_item(,%rcx,8)
                        jg not_change_min
                        movq %rcx, %rax
                        not_change_min:
                                incq %rcx
                                jmp inner_loop
                inner_loop_exit:
                        movq data_item(,%rdx,8), %rbx     # swap the value, of arr[i] and the min-value.
                        movq data_item(,%rax,8), %rdi
                        movq %rdi, data_item(,%rdx,8)
                        movq %rbx, data_item(,%rax,8)
                        incq %rdx
                        jmp out_loop                      # inner loop exits.

print_arr:
        movq $0, %rbx                                      # print \'sorted nums:\'
        movq $before_sort, %rdi
        call printf
        print_loop:                                        # print all nums sorted in a loop
                movq data_item(,%rbx,8),%rax
                cmp $0, %rax
                je end
                movq $sort_output_format, %rdi
                movq %rax, %rsi
                call printf
                incq %rbx
                jmp print_loop
end:                                                       # program ends.
        movq $127, %rdi
        movq $60, %rax
        syscall

 

以上是关于Using assembly writing algorithm programs的主要内容,如果未能解决你的问题,请参考以下文章

writing LaTex using Org-mode

xlwings: Write Excel macro using python instead of VBA

matlabError using save Unable to write file pqfile.mat: permission denied.解决

Oracle hidden costs revealed, Part2 – Using DTrace to find why writes in SYSTEM tablespace are slowe

[Transducer + Ramda] Write highly performance / functional code by using transducer-js and ramda.js

《CLR Via C#》使用CSC.exe将module组合成assembly