I'm new to the assembly language. I'm creating a prime factorization function in AT&T x64/GNU syntax.
Here is..
.data
.text
.global main
.prime_end: .string "End\n"
.fact_end: .string "Finish\n"
.fact_print: .string "%d\n"
prime:
mov %rsi, %rax
mov $0, %rdi
cmp $2, %rax
jl prime_end
jmp prime_base
prime_base: # r10 = 1
mov $1, %r10
jmp prime_start
prime_start: # r10 + 1 = 2
inc %r10
jmp prime_check
prime_check:
cqto
div %r10 # rsi = 10, rax = 10, rax / r10 = 10/2
cmp $0, %rdx # rdx = 0, rax = 5
jne prime_start
jmp fact_check
fact_check: # rsi = 10, rax = 10
mov %rsi, %rax
cqto
div %r10 # 10 / 2
jmp prime_fact
prime_fact:
cmp %r10, %rax # to end function
jl fact_end
call fact_print # where I think it should be printed
mov %rax, %rsi # rax = 5, rsi = 5
add %rdx, %rsi # rdx = 0, rsi = 5
cmp $1, %rsi # if 5 = 1, end function
jne prime_base
jmp fact_end
fact_print:
mov $.fact_print, %rdi
mov %r10, %rsi
xor %rax, %rax
call printf
ret
fact_end:
mov $.fact_end, %rdi
xor %rax, %rax
call printf
ret
prime_end:
mov $.prime_end, %rdi
xor %rax, %rax
call printf
ret
main:
mov $10, %rsi # check 10 ... expect 2 5
call prime
When I try to print a prime factorization number, it says
2 2 2 2 2 2 2 2 ...
I'm sorry. I'm very new. Can you explain why this is printing only 2 infinitely? What do I have to do to print the prime factorization?