您的位置:首页 > 编程语言 > Go语言

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

# 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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: