Using assembly writing algorithm programs
2016-10-04 15:37
239 查看
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
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 a Simple Action Server using the Execute Callback
- How to properly uninstall VERITAS Backup Exec (tm) 9.x if problems occur using Add/Remove Programs
- Using Stored Programs with MySQLdb
- Reading/Writing text files using C#(转:初学)
- Writing a Message Queuing COM Application using C++
- Writing Web Services Client Applications using Visual C++
- Viewing and Writing XML Data using ADO.NET DataSets
- .net4 wpf App 使用log4net 错误:The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)
- An Efficient Digital Search Algorithm by Using a Double-Array Structure笔记
- python nltk 学习笔记(4) Writing Structured Programs
- Tessnet2 a .NET 2.0 Open Source OCR assembly using Tesseract engine
- [Algorithms] Quicksort algorithm using TypeScript
- Writing and Optimizing ARM Assembly Code 《ARM...》Chapter-6
- A Basic Template For Assembly Language Programs
- Writing multiboot PE kernels using Visual C++
- Implementing iBooks page curling using a conical deformation algorithm
- Writing a boot loader in Assembly and C
- Finding Kth Minimum (partial ordering) – Using Tournament Algorithm
- Using Fusion Logs to Debug .NET Assembly Binding Issues