Mi humilde aporte:
assembly
.globl main
.section .data
main_string:
mstr_len:.quad mstr_end - mstr -1
mstr:.string "asuka"
mstr_end:
string_buffer:
str1_len:.quad str1_end - str1 -1
str1:.string "haruhi"
str1_end:
str2_len: .quad str2_end - str2 -1
str2:.string "azuka"
str2_end:
str3_len: .quad str3_end - str3 -1
str3:.string "aakus"
str3_end:
str4_len: .quad str4_end - str4 -1
str4:.string "asuki"
str4_end:
str5_len: .quad str5_end - str5 -1
str5:.string "asuka"
str5_end:
str0_len:.quad 0
str0:.ascii "\0"
str_fmt:
.string "%d anagramas\n"
.section .text
.equ STR_BUFF_PTR,-0x08
.equ NUM_ANAGRAMAS,-0x10
#.equ CURRENT_STR_PTR, -0x18
.equ NUM, -0x08
main:
enter $0x20, $0
movq $str3, %r10
movq $str3_len, %r11
movq $str0, %r12
movq $str0_len, %r13
# Ordenamos la cadena principal
movq $mstr, %rdi
movq mstr_len, %rsi
call sort_str
movq $string_buffer, STR_BUFF_PTR(%rbp)
movq $0, NUM_ANAGRAMAS(%rbp)
# Ordenamos y comparamos el resto de cadenas
main__loop:
movq STR_BUFF_PTR(%rbp), %rdx
leaq 8(%rdx), %rdi
movq (%rdx), %rsi
cmpq $0, (%rdi)
je main__print
cmpq $0, %rsi
je main__print
call sort_str
movq STR_BUFF_PTR(%rbp), %r8
leaq 8(%r8), %rdi
movq $mstr, %rsi
movq (%r8), %rdx
movq mstr_len, %rcx
call str_eq
cmpq $0, %rax
je main__loop_end
incq NUM_ANAGRAMAS(%rbp)
main__loop_end:
movq STR_BUFF_PTR(%rbp), %rdx
movq (%rdx), %rax
addq %rax, STR_BUFF_PTR(%rbp)
addq $(0x1 +0x08), STR_BUFF_PTR(%rbp)
movq STR_BUFF_PTR(%rbp), %rax
jmp main__loop
main__print:
movq $0, %rax
movq $str_fmt, %rdi
movq NUM_ANAGRAMAS(%rbp), %rsi
call printf
main__return:
movq $0, %rax
leave
ret
# %rdi - str ptr
# %rsi - str len without null
sort_str:
enter $0, $0
movq $0, %r8
sort_str__main_loop:
cmpq %rsi, %r8
jae sort_str__return
movq $0, %r9
sort_str__order_subarray:
cmpq %r8, %r9
jae sort_str__main_loop_end
movb (%rdi, %r8), %al
movb (%rdi, %r9), %bl
cmpb %al, %bl
jbe sort_str__order_subarray_end
sort_str__swap_values:
movb %bl, (%rdi, %r8)
movb %al, (%rdi, %r9)
sort_str__order_subarray_end:
incq %r9
jmp sort_str__order_subarray
sort_str__main_loop_end:
incq %r8
jmp sort_str__main_loop
sort_str__return:
leave
ret
# %rdi -> ptr to str1
# %rsi -> ptr to str2
# %rdx -> str1 len
# %rcx -> str2 len
str_eq:
enter $0, $0
cmpq %rdx, %rcx
jne str_eq__not_equal
str_eq__pre_loop:
movq $1, %rax
movq $0, %rcx
str_eq__loop:
cmpq %rdx, %rcx
jae str_eq__return
movb (%rdi, %rcx), %bl
cmpb (%rsi, %rcx), %bl
je str_eq__loop_end
str_eq__not_equal:
movq $0, %rax
jmp str_eq__return
str_eq__loop_end:
incq %rcx
jmp str_eq__loop
str_eq__return:
leave
ret