Checking if a number is prime in x64 Assembly

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm just learning Assembly. This program is a simple attempt to determine if a given number is prime or not. Any advice and all topical comments on code optimization and conventions is appreciated!



Compiled as follows using VS2017 x64 Native Tools Command Prompt:




nasm -g -fwin64 isprime.asm



cl /Zi isprime.obj msvcrt.lib legacy_stdio_definitions.lib




isprime.asm



bits 64
default rel

extern printf
extern scanf

section .data

number: dq 0
isPrime: dq 1
counter: dq 0

question: db "Which number would you like to check? ", 0
fmt: db "%d", 0
numberIsPrime: db "%d is prime", 10, 0
numberIsNotPrime: db "%d is not prime", 10, 0

section .text

global main
main:
push rbp
mov rbp, rsp

sub rsp, 32
mov rcx, question
call printf
add rsp, 32

sub rsp, 32
mov rdx, number
mov rcx, fmt
call scanf
add rsp, 32

mov rcx, [number]
mov [counter], rcx

continue_prime_check:
dec qword [counter]
cmp qword [counter], 2
jge not_reached_1_yet
jmp prime_check_ended

not_reached_1_yet:
mov rax, [number]
cdq
mov rbx, [counter]
idiv rbx
cmp edx, 0
je evenly_divisible
jmp not_evenly_divisible

evenly_divisible:
mov qword [isPrime], 0

not_evenly_divisible:
jmp continue_prime_check

prime_check_ended:
cmp qword [isPrime], 1
je number_was_prime
jmp number_wasnt_prime

number_was_prime:
sub rsp, 32
mov rdx, [number]
mov rcx, numberIsPrime
call printf
add rsp, 32
jmp end_if

number_wasnt_prime:
sub rsp, 32
mov rdx, [number]
mov rcx, numberIsNotPrime
call printf
add rsp, 32

end_if:
mov rbp, rsp
pop rbp
ret









share|improve this question









New contributor




Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Have you run the code, and does it work correctly?
    – 1201ProgramAlarm
    3 hours ago






  • 3




    Yes, otherwise it would be on SO
    – Mr. Vix
    2 hours ago










  • Which assembler are you using?
    – 1201ProgramAlarm
    2 hours ago










  • The x64 Native Tools for VS2017 (NASM version 2.13.03 compiled on Feb 7 2018)
    – Mr. Vix
    2 hours ago















up vote
1
down vote

favorite












I'm just learning Assembly. This program is a simple attempt to determine if a given number is prime or not. Any advice and all topical comments on code optimization and conventions is appreciated!



Compiled as follows using VS2017 x64 Native Tools Command Prompt:




nasm -g -fwin64 isprime.asm



cl /Zi isprime.obj msvcrt.lib legacy_stdio_definitions.lib




isprime.asm



bits 64
default rel

extern printf
extern scanf

section .data

number: dq 0
isPrime: dq 1
counter: dq 0

question: db "Which number would you like to check? ", 0
fmt: db "%d", 0
numberIsPrime: db "%d is prime", 10, 0
numberIsNotPrime: db "%d is not prime", 10, 0

section .text

global main
main:
push rbp
mov rbp, rsp

sub rsp, 32
mov rcx, question
call printf
add rsp, 32

sub rsp, 32
mov rdx, number
mov rcx, fmt
call scanf
add rsp, 32

mov rcx, [number]
mov [counter], rcx

continue_prime_check:
dec qword [counter]
cmp qword [counter], 2
jge not_reached_1_yet
jmp prime_check_ended

not_reached_1_yet:
mov rax, [number]
cdq
mov rbx, [counter]
idiv rbx
cmp edx, 0
je evenly_divisible
jmp not_evenly_divisible

evenly_divisible:
mov qword [isPrime], 0

not_evenly_divisible:
jmp continue_prime_check

prime_check_ended:
cmp qword [isPrime], 1
je number_was_prime
jmp number_wasnt_prime

number_was_prime:
sub rsp, 32
mov rdx, [number]
mov rcx, numberIsPrime
call printf
add rsp, 32
jmp end_if

number_wasnt_prime:
sub rsp, 32
mov rdx, [number]
mov rcx, numberIsNotPrime
call printf
add rsp, 32

end_if:
mov rbp, rsp
pop rbp
ret









share|improve this question









New contributor




Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Have you run the code, and does it work correctly?
    – 1201ProgramAlarm
    3 hours ago






  • 3




    Yes, otherwise it would be on SO
    – Mr. Vix
    2 hours ago










  • Which assembler are you using?
    – 1201ProgramAlarm
    2 hours ago










  • The x64 Native Tools for VS2017 (NASM version 2.13.03 compiled on Feb 7 2018)
    – Mr. Vix
    2 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm just learning Assembly. This program is a simple attempt to determine if a given number is prime or not. Any advice and all topical comments on code optimization and conventions is appreciated!



Compiled as follows using VS2017 x64 Native Tools Command Prompt:




nasm -g -fwin64 isprime.asm



cl /Zi isprime.obj msvcrt.lib legacy_stdio_definitions.lib




isprime.asm



bits 64
default rel

extern printf
extern scanf

section .data

number: dq 0
isPrime: dq 1
counter: dq 0

question: db "Which number would you like to check? ", 0
fmt: db "%d", 0
numberIsPrime: db "%d is prime", 10, 0
numberIsNotPrime: db "%d is not prime", 10, 0

section .text

global main
main:
push rbp
mov rbp, rsp

sub rsp, 32
mov rcx, question
call printf
add rsp, 32

sub rsp, 32
mov rdx, number
mov rcx, fmt
call scanf
add rsp, 32

mov rcx, [number]
mov [counter], rcx

continue_prime_check:
dec qword [counter]
cmp qword [counter], 2
jge not_reached_1_yet
jmp prime_check_ended

not_reached_1_yet:
mov rax, [number]
cdq
mov rbx, [counter]
idiv rbx
cmp edx, 0
je evenly_divisible
jmp not_evenly_divisible

evenly_divisible:
mov qword [isPrime], 0

not_evenly_divisible:
jmp continue_prime_check

prime_check_ended:
cmp qword [isPrime], 1
je number_was_prime
jmp number_wasnt_prime

number_was_prime:
sub rsp, 32
mov rdx, [number]
mov rcx, numberIsPrime
call printf
add rsp, 32
jmp end_if

number_wasnt_prime:
sub rsp, 32
mov rdx, [number]
mov rcx, numberIsNotPrime
call printf
add rsp, 32

end_if:
mov rbp, rsp
pop rbp
ret









share|improve this question









New contributor




Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm just learning Assembly. This program is a simple attempt to determine if a given number is prime or not. Any advice and all topical comments on code optimization and conventions is appreciated!



Compiled as follows using VS2017 x64 Native Tools Command Prompt:




nasm -g -fwin64 isprime.asm



cl /Zi isprime.obj msvcrt.lib legacy_stdio_definitions.lib




isprime.asm



bits 64
default rel

extern printf
extern scanf

section .data

number: dq 0
isPrime: dq 1
counter: dq 0

question: db "Which number would you like to check? ", 0
fmt: db "%d", 0
numberIsPrime: db "%d is prime", 10, 0
numberIsNotPrime: db "%d is not prime", 10, 0

section .text

global main
main:
push rbp
mov rbp, rsp

sub rsp, 32
mov rcx, question
call printf
add rsp, 32

sub rsp, 32
mov rdx, number
mov rcx, fmt
call scanf
add rsp, 32

mov rcx, [number]
mov [counter], rcx

continue_prime_check:
dec qword [counter]
cmp qword [counter], 2
jge not_reached_1_yet
jmp prime_check_ended

not_reached_1_yet:
mov rax, [number]
cdq
mov rbx, [counter]
idiv rbx
cmp edx, 0
je evenly_divisible
jmp not_evenly_divisible

evenly_divisible:
mov qword [isPrime], 0

not_evenly_divisible:
jmp continue_prime_check

prime_check_ended:
cmp qword [isPrime], 1
je number_was_prime
jmp number_wasnt_prime

number_was_prime:
sub rsp, 32
mov rdx, [number]
mov rcx, numberIsPrime
call printf
add rsp, 32
jmp end_if

number_wasnt_prime:
sub rsp, 32
mov rdx, [number]
mov rcx, numberIsNotPrime
call printf
add rsp, 32

end_if:
mov rbp, rsp
pop rbp
ret






primes assembly






share|improve this question









New contributor




Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago





















New contributor




Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Mr. Vix

183




183




New contributor




Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Mr. Vix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Have you run the code, and does it work correctly?
    – 1201ProgramAlarm
    3 hours ago






  • 3




    Yes, otherwise it would be on SO
    – Mr. Vix
    2 hours ago










  • Which assembler are you using?
    – 1201ProgramAlarm
    2 hours ago










  • The x64 Native Tools for VS2017 (NASM version 2.13.03 compiled on Feb 7 2018)
    – Mr. Vix
    2 hours ago

















  • Have you run the code, and does it work correctly?
    – 1201ProgramAlarm
    3 hours ago






  • 3




    Yes, otherwise it would be on SO
    – Mr. Vix
    2 hours ago










  • Which assembler are you using?
    – 1201ProgramAlarm
    2 hours ago










  • The x64 Native Tools for VS2017 (NASM version 2.13.03 compiled on Feb 7 2018)
    – Mr. Vix
    2 hours ago
















Have you run the code, and does it work correctly?
– 1201ProgramAlarm
3 hours ago




Have you run the code, and does it work correctly?
– 1201ProgramAlarm
3 hours ago




3




3




Yes, otherwise it would be on SO
– Mr. Vix
2 hours ago




Yes, otherwise it would be on SO
– Mr. Vix
2 hours ago












Which assembler are you using?
– 1201ProgramAlarm
2 hours ago




Which assembler are you using?
– 1201ProgramAlarm
2 hours ago












The x64 Native Tools for VS2017 (NASM version 2.13.03 compiled on Feb 7 2018)
– Mr. Vix
2 hours ago





The x64 Native Tools for VS2017 (NASM version 2.13.03 compiled on Feb 7 2018)
– Mr. Vix
2 hours ago











2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










There are numerous small things.



  1. You don't have to keep subtracting and adding 32 from rsp. Allocate the space once at the start of the function (main), reuse it for the duration, and add it back at the end (but see below).

  2. My personal preference would be to use mov ecx,offset question to make it clear that I want the address of the variable, and not the contents of it.

  3. Start your prime check with the low numbers, not the high ones. They're far more likely to be divisors.

  4. Check for divisible by 2 first (before your loop), then you only need to check the odd numbers in your loop.


  5. Instead of a conditional jump around an unconditional jump, negate the condition. So you'd change



     jge not_reached_1_yet
    jmp prime_check_ended
    not_reached_1_yet:


    to



     jnge prime_check_ended ; or j


  6. Instead of cmp edx,0, you can use test edx,edx. The test will do a bit-wise logical and of the two operands and set the flags accordingly without storing the result of the and. This is a common way to check for zero.


  7. Once you find that your number is not prime, you can stop looping.

  8. You can also stop looping once you get to the square root of number. This is often done by comparing the square of counter with number, but you can easily check it by comparing eax with counter after the division. If eax is less or equal to counter you can stop looping.

  9. Your mov rbp,rsp at the end is backwards. It should be mov rsp,rbp. This will also remove the 32 bytes of stack space you reserved for argument storage during function calls, so you don't need to explicitly add those 32 bytes back to the stack pointer.





share|improve this answer






















  • For point one, do you mean rsp? I'm also guessing you're referring to the main ftn. If you may also give some more info on point six I'd appreciate it!
    – Mr. Vix
    2 hours ago











  • @Mr.Vix I've rephrased point one and expanded on point six.
    – 1201ProgramAlarm
    1 hour ago










  • So just to clarify, you mean all I need to do is have sub rsp in place of the first sub rsp, 32 in my main ftn, and then make the mov rsp, rbp change? All of the other sub rsp, 32 and add rsp, 32 calls can be deleted?
    – Mr. Vix
    1 hour ago











  • @Mr.Vix Yes. Only one sub and no adds.
    – 1201ProgramAlarm
    1 hour ago










  • Great thank you!
    – Mr. Vix
    54 mins ago

















up vote
0
down vote














  • The printing branches are identical, except that they load rcx with different addresses. Better setup rcx when you arrive to the conclusion, and unify printing.



    Expanding on that, an idiomatic assembly program would first guess the right string, and correct it if it was wrong:



    prime_check_ended:
    mov rcx, numberIsPrime
    cmp cmp qword [isPrime], 1
    je print_result
    mov rcx, numberIsNotPrime

    print_result:
    mov rdx, [number]
    call printf


  • I don't know wether NASM supports local labels. If it does, it is a good habit to use them. Otherwise you risk polluting the label space.






share|improve this answer




















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Mr. Vix is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f204902%2fchecking-if-a-number-is-prime-in-x64-assembly%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    There are numerous small things.



    1. You don't have to keep subtracting and adding 32 from rsp. Allocate the space once at the start of the function (main), reuse it for the duration, and add it back at the end (but see below).

    2. My personal preference would be to use mov ecx,offset question to make it clear that I want the address of the variable, and not the contents of it.

    3. Start your prime check with the low numbers, not the high ones. They're far more likely to be divisors.

    4. Check for divisible by 2 first (before your loop), then you only need to check the odd numbers in your loop.


    5. Instead of a conditional jump around an unconditional jump, negate the condition. So you'd change



       jge not_reached_1_yet
      jmp prime_check_ended
      not_reached_1_yet:


      to



       jnge prime_check_ended ; or j


    6. Instead of cmp edx,0, you can use test edx,edx. The test will do a bit-wise logical and of the two operands and set the flags accordingly without storing the result of the and. This is a common way to check for zero.


    7. Once you find that your number is not prime, you can stop looping.

    8. You can also stop looping once you get to the square root of number. This is often done by comparing the square of counter with number, but you can easily check it by comparing eax with counter after the division. If eax is less or equal to counter you can stop looping.

    9. Your mov rbp,rsp at the end is backwards. It should be mov rsp,rbp. This will also remove the 32 bytes of stack space you reserved for argument storage during function calls, so you don't need to explicitly add those 32 bytes back to the stack pointer.





    share|improve this answer






















    • For point one, do you mean rsp? I'm also guessing you're referring to the main ftn. If you may also give some more info on point six I'd appreciate it!
      – Mr. Vix
      2 hours ago











    • @Mr.Vix I've rephrased point one and expanded on point six.
      – 1201ProgramAlarm
      1 hour ago










    • So just to clarify, you mean all I need to do is have sub rsp in place of the first sub rsp, 32 in my main ftn, and then make the mov rsp, rbp change? All of the other sub rsp, 32 and add rsp, 32 calls can be deleted?
      – Mr. Vix
      1 hour ago











    • @Mr.Vix Yes. Only one sub and no adds.
      – 1201ProgramAlarm
      1 hour ago










    • Great thank you!
      – Mr. Vix
      54 mins ago














    up vote
    2
    down vote



    accepted










    There are numerous small things.



    1. You don't have to keep subtracting and adding 32 from rsp. Allocate the space once at the start of the function (main), reuse it for the duration, and add it back at the end (but see below).

    2. My personal preference would be to use mov ecx,offset question to make it clear that I want the address of the variable, and not the contents of it.

    3. Start your prime check with the low numbers, not the high ones. They're far more likely to be divisors.

    4. Check for divisible by 2 first (before your loop), then you only need to check the odd numbers in your loop.


    5. Instead of a conditional jump around an unconditional jump, negate the condition. So you'd change



       jge not_reached_1_yet
      jmp prime_check_ended
      not_reached_1_yet:


      to



       jnge prime_check_ended ; or j


    6. Instead of cmp edx,0, you can use test edx,edx. The test will do a bit-wise logical and of the two operands and set the flags accordingly without storing the result of the and. This is a common way to check for zero.


    7. Once you find that your number is not prime, you can stop looping.

    8. You can also stop looping once you get to the square root of number. This is often done by comparing the square of counter with number, but you can easily check it by comparing eax with counter after the division. If eax is less or equal to counter you can stop looping.

    9. Your mov rbp,rsp at the end is backwards. It should be mov rsp,rbp. This will also remove the 32 bytes of stack space you reserved for argument storage during function calls, so you don't need to explicitly add those 32 bytes back to the stack pointer.





    share|improve this answer






















    • For point one, do you mean rsp? I'm also guessing you're referring to the main ftn. If you may also give some more info on point six I'd appreciate it!
      – Mr. Vix
      2 hours ago











    • @Mr.Vix I've rephrased point one and expanded on point six.
      – 1201ProgramAlarm
      1 hour ago










    • So just to clarify, you mean all I need to do is have sub rsp in place of the first sub rsp, 32 in my main ftn, and then make the mov rsp, rbp change? All of the other sub rsp, 32 and add rsp, 32 calls can be deleted?
      – Mr. Vix
      1 hour ago











    • @Mr.Vix Yes. Only one sub and no adds.
      – 1201ProgramAlarm
      1 hour ago










    • Great thank you!
      – Mr. Vix
      54 mins ago












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    There are numerous small things.



    1. You don't have to keep subtracting and adding 32 from rsp. Allocate the space once at the start of the function (main), reuse it for the duration, and add it back at the end (but see below).

    2. My personal preference would be to use mov ecx,offset question to make it clear that I want the address of the variable, and not the contents of it.

    3. Start your prime check with the low numbers, not the high ones. They're far more likely to be divisors.

    4. Check for divisible by 2 first (before your loop), then you only need to check the odd numbers in your loop.


    5. Instead of a conditional jump around an unconditional jump, negate the condition. So you'd change



       jge not_reached_1_yet
      jmp prime_check_ended
      not_reached_1_yet:


      to



       jnge prime_check_ended ; or j


    6. Instead of cmp edx,0, you can use test edx,edx. The test will do a bit-wise logical and of the two operands and set the flags accordingly without storing the result of the and. This is a common way to check for zero.


    7. Once you find that your number is not prime, you can stop looping.

    8. You can also stop looping once you get to the square root of number. This is often done by comparing the square of counter with number, but you can easily check it by comparing eax with counter after the division. If eax is less or equal to counter you can stop looping.

    9. Your mov rbp,rsp at the end is backwards. It should be mov rsp,rbp. This will also remove the 32 bytes of stack space you reserved for argument storage during function calls, so you don't need to explicitly add those 32 bytes back to the stack pointer.





    share|improve this answer














    There are numerous small things.



    1. You don't have to keep subtracting and adding 32 from rsp. Allocate the space once at the start of the function (main), reuse it for the duration, and add it back at the end (but see below).

    2. My personal preference would be to use mov ecx,offset question to make it clear that I want the address of the variable, and not the contents of it.

    3. Start your prime check with the low numbers, not the high ones. They're far more likely to be divisors.

    4. Check for divisible by 2 first (before your loop), then you only need to check the odd numbers in your loop.


    5. Instead of a conditional jump around an unconditional jump, negate the condition. So you'd change



       jge not_reached_1_yet
      jmp prime_check_ended
      not_reached_1_yet:


      to



       jnge prime_check_ended ; or j


    6. Instead of cmp edx,0, you can use test edx,edx. The test will do a bit-wise logical and of the two operands and set the flags accordingly without storing the result of the and. This is a common way to check for zero.


    7. Once you find that your number is not prime, you can stop looping.

    8. You can also stop looping once you get to the square root of number. This is often done by comparing the square of counter with number, but you can easily check it by comparing eax with counter after the division. If eax is less or equal to counter you can stop looping.

    9. Your mov rbp,rsp at the end is backwards. It should be mov rsp,rbp. This will also remove the 32 bytes of stack space you reserved for argument storage during function calls, so you don't need to explicitly add those 32 bytes back to the stack pointer.






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 2 hours ago









    1201ProgramAlarm

    2,7152720




    2,7152720











    • For point one, do you mean rsp? I'm also guessing you're referring to the main ftn. If you may also give some more info on point six I'd appreciate it!
      – Mr. Vix
      2 hours ago











    • @Mr.Vix I've rephrased point one and expanded on point six.
      – 1201ProgramAlarm
      1 hour ago










    • So just to clarify, you mean all I need to do is have sub rsp in place of the first sub rsp, 32 in my main ftn, and then make the mov rsp, rbp change? All of the other sub rsp, 32 and add rsp, 32 calls can be deleted?
      – Mr. Vix
      1 hour ago











    • @Mr.Vix Yes. Only one sub and no adds.
      – 1201ProgramAlarm
      1 hour ago










    • Great thank you!
      – Mr. Vix
      54 mins ago
















    • For point one, do you mean rsp? I'm also guessing you're referring to the main ftn. If you may also give some more info on point six I'd appreciate it!
      – Mr. Vix
      2 hours ago











    • @Mr.Vix I've rephrased point one and expanded on point six.
      – 1201ProgramAlarm
      1 hour ago










    • So just to clarify, you mean all I need to do is have sub rsp in place of the first sub rsp, 32 in my main ftn, and then make the mov rsp, rbp change? All of the other sub rsp, 32 and add rsp, 32 calls can be deleted?
      – Mr. Vix
      1 hour ago











    • @Mr.Vix Yes. Only one sub and no adds.
      – 1201ProgramAlarm
      1 hour ago










    • Great thank you!
      – Mr. Vix
      54 mins ago















    For point one, do you mean rsp? I'm also guessing you're referring to the main ftn. If you may also give some more info on point six I'd appreciate it!
    – Mr. Vix
    2 hours ago





    For point one, do you mean rsp? I'm also guessing you're referring to the main ftn. If you may also give some more info on point six I'd appreciate it!
    – Mr. Vix
    2 hours ago













    @Mr.Vix I've rephrased point one and expanded on point six.
    – 1201ProgramAlarm
    1 hour ago




    @Mr.Vix I've rephrased point one and expanded on point six.
    – 1201ProgramAlarm
    1 hour ago












    So just to clarify, you mean all I need to do is have sub rsp in place of the first sub rsp, 32 in my main ftn, and then make the mov rsp, rbp change? All of the other sub rsp, 32 and add rsp, 32 calls can be deleted?
    – Mr. Vix
    1 hour ago





    So just to clarify, you mean all I need to do is have sub rsp in place of the first sub rsp, 32 in my main ftn, and then make the mov rsp, rbp change? All of the other sub rsp, 32 and add rsp, 32 calls can be deleted?
    – Mr. Vix
    1 hour ago













    @Mr.Vix Yes. Only one sub and no adds.
    – 1201ProgramAlarm
    1 hour ago




    @Mr.Vix Yes. Only one sub and no adds.
    – 1201ProgramAlarm
    1 hour ago












    Great thank you!
    – Mr. Vix
    54 mins ago




    Great thank you!
    – Mr. Vix
    54 mins ago












    up vote
    0
    down vote














    • The printing branches are identical, except that they load rcx with different addresses. Better setup rcx when you arrive to the conclusion, and unify printing.



      Expanding on that, an idiomatic assembly program would first guess the right string, and correct it if it was wrong:



      prime_check_ended:
      mov rcx, numberIsPrime
      cmp cmp qword [isPrime], 1
      je print_result
      mov rcx, numberIsNotPrime

      print_result:
      mov rdx, [number]
      call printf


    • I don't know wether NASM supports local labels. If it does, it is a good habit to use them. Otherwise you risk polluting the label space.






    share|improve this answer
























      up vote
      0
      down vote














      • The printing branches are identical, except that they load rcx with different addresses. Better setup rcx when you arrive to the conclusion, and unify printing.



        Expanding on that, an idiomatic assembly program would first guess the right string, and correct it if it was wrong:



        prime_check_ended:
        mov rcx, numberIsPrime
        cmp cmp qword [isPrime], 1
        je print_result
        mov rcx, numberIsNotPrime

        print_result:
        mov rdx, [number]
        call printf


      • I don't know wether NASM supports local labels. If it does, it is a good habit to use them. Otherwise you risk polluting the label space.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote










        • The printing branches are identical, except that they load rcx with different addresses. Better setup rcx when you arrive to the conclusion, and unify printing.



          Expanding on that, an idiomatic assembly program would first guess the right string, and correct it if it was wrong:



          prime_check_ended:
          mov rcx, numberIsPrime
          cmp cmp qword [isPrime], 1
          je print_result
          mov rcx, numberIsNotPrime

          print_result:
          mov rdx, [number]
          call printf


        • I don't know wether NASM supports local labels. If it does, it is a good habit to use them. Otherwise you risk polluting the label space.






        share|improve this answer













        • The printing branches are identical, except that they load rcx with different addresses. Better setup rcx when you arrive to the conclusion, and unify printing.



          Expanding on that, an idiomatic assembly program would first guess the right string, and correct it if it was wrong:



          prime_check_ended:
          mov rcx, numberIsPrime
          cmp cmp qword [isPrime], 1
          je print_result
          mov rcx, numberIsNotPrime

          print_result:
          mov rdx, [number]
          call printf


        • I don't know wether NASM supports local labels. If it does, it is a good habit to use them. Otherwise you risk polluting the label space.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 31 mins ago









        vnp

        37.3k12993




        37.3k12993




















            Mr. Vix is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Mr. Vix is a new contributor. Be nice, and check out our Code of Conduct.












            Mr. Vix is a new contributor. Be nice, and check out our Code of Conduct.











            Mr. Vix is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f204902%2fchecking-if-a-number-is-prime-in-x64-assembly%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke