zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    Cpp Singleton
    回溯法( Backtracking Algorithms ) :C语言Maze迷宫问题(自己实现)
    First wxWidgets Demo, wxWidgets简单示例
    copy contructor拷贝构造函数 (Copy Control)
    UPX (Ultimate Packer for eXecutables) 多平台可执行文件压缩
    wxWidgets Event Handling
    WxWidgets 安装与测试
    顺序查找 sequential find
    HDU1171 Big Event in HDU 背包
    HDU2091 水题
  • 原文地址:https://www.cnblogs.com/rixiang/p/5930575.html
Copyright © 2011-2022 走看看