Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to create a program that determines if a number is prime or not in assembly x64 using wsl this is the code:

global main
section .data
    input_number dq 17
    newline db 0xA
    prime_msg db "The number is prime"
    not_prime_msg db "The number is not prime"

section .text
    
main:
    mov rdi, input_number

    ; Preserve rdi
    push rdi

    call is_prime

    ; Restore rdi
    pop rdi

    ; Exit the program
    mov rax, 60
    xor rdi, rdi
    syscall

is_prime:
    ; Function to check if a number is prime
    ; Check if the number is less than 2
    cmp rdi, 2
    jl not_prime

    ; Loop to check divisors from 2 to the square root of the number
    mov rcx, 2
check_next_divisor:
    mov rax, rdi  ; Copy the number to rax
    xor rdx, rdx  ; Clear any previous remainder
    div rcx       ; Divide rax by rcx, quotient in rax, remainder in rdx

    ; Check if the remainder is zero
    test rdx, rdx
    je not_prime  ; If yes, it's not a prime number

    inc rcx

    ; Check if the divisor is greater than the square root of the number
    mov rax, rcx
    mul rax
    cmp rax, rdi
    jg prime  ; If the divisor is greater than the square root, it's a prime number

    jmp check_next_divisor

prime:
    ; Print message for a prime number
    mov rax, 1
    mov rdi, 1
    mov rsi, prime_msg
    mov rdx, 40  ; Adjust the length of the message if needed
    syscall

    ; Set zero flag to indicate it is prime
    ret

not_prime:
    ; Print message for a non-prime number
    mov rax, 1
    mov rdi, 1
    mov rsi, not_prime_msg
    mov rdx, 40  ; Adjust the length of the message if needed
    syscall

    ; Set carry flag to indicate it is not prime
    stc
    ret
and i got these errors:
ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000
1.o: in function `prime_msg':
1.asm:(.data+0x1c): relocation truncated to fit: R_X86_64_8 against `.data'
1.o: in function `not_prime_msg':
1.asm:(.data+0x34): relocation truncated to fit: R_X86_64_8 against `.data'


What I have tried:

i got this errors:
ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000
1.o: in function `prime_msg':
1.asm:(.data+0x1c): relocation truncated to fit: R_X86_64_8 against `.data'
1.o: in function `not_prime_msg':
1.asm:(.data+0x34): relocation truncated to fit: R_X86_64_8 against `.data'
Posted
Comments
Richard Deeming 21-Dec-23 8:09am    
Relocation truncated to fit error in assembly[^]

If you want to update your question, click the green "Improve question" link and edit your question.

Do not post your update as a "new" question.
Member 16165977 21-Dec-23 8:14am    
i am sorry i am new to this web site
Richard MacCutchan 21-Dec-23 8:11am    
I have deleted your duplicate of this question.
Member 16165977 21-Dec-23 8:14am    
thank you

1 solution

The first message is telling you that you have not defined a start label for the program's main entry point. The others are indicating that the operands are too long for register rsi. You should check the assembler and cpu documentation to find out why. It is some time since I wrote much assembler but if I recall correctly you need to use the offset operator on those lines:
ASM
    mov rsi, offset prime_msg

; ...

    mov rsi, offset not_prime_msg
 
Share this answer
 
Comments
Member 16165977 21-Dec-23 8:16am    
i still have the same errors
Richard MacCutchan 21-Dec-23 8:46am    
Which version of assembler language is this, and which compiler are you using to build it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900