• 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:
    "
    sort_output_format:
            .asciz "%d
    "
    .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

     

  • 相关阅读:
    Linux基础(Ubuntu16.04):安装vim及配置
    Ubuntu16.04 安装ROS及其IDE
    python格式转换的记录
    python爬虫selenium相关
    【LAMP】搭建Web网站过程中的记录【Ubuntu18.04+Apache2.4+PHP7.2+MySQL5.7】
    【疯狂挖坑】linux服务器尝试中的问题(nohup等)
    逻辑回归与全连接神经网络联系的简单理解记录
    Unity3d开发中遇到的问题记录
    python中numpy库的一些使用
    Deep Learning论文翻译(Nature Deep Review)
  • 原文地址:https://www.cnblogs.com/rixiang/p/5930575.html
Copyright © 2020-2023  润新知