0

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?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Soomin Im
  • 11
  • 1
  • 2

1 Answers1

0

The caller of fact_print assumes that the eax register will not be changed, but this is not the case. When fact_print returns, eax will have the value returned by printf (the number of characters that were output).

You'll want to store eax somewhere (in a register that is not modified, or on the stack) before calling fact_print, then restore it upon return.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • ... or use any of the several call-preserved registers in x86-64 calling conventions, instead of the stack. [What registers are preserved through a linux x86-64 function call](https://stackoverflow.com/q/18024672) – Peter Cordes Nov 06 '20 at 02:15