Problem 7 on Project Euler with x86 Assembly

The problem:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

My solution:

.globl main

  /*-enter stack frame-*/
  pushl %ebp
  movl %esp, %ebp

  movl $3, %edi
  movl $1, %esi
    pushl %edi
    call isPrime
    pop %ebx
    cmp $1, %eax
    jne notPrime
    addl $1, %esi
    cmp $10001, %esi
                je found
    addl $2, %edi
    jmp mainLoop

  pushl %edi
  pushl $string2
  call printf

  /*-leave stack frame-*/
  movl %ebp, %esp
  popl %ebp  

  movl $0, %eax

  pushl %ebx
  pushl %edi
  movl 12(%esp), %edi
  cmp $1, %edi
  je returnYes
  cmp $2, %edi
  je returnYes
  movl $0, %edx
  movl %edi, %eax
  movl $2, %ebx
  idiv %ebx
  cmp $0, %edx
  je returnNo
  movl $3, %ecx
    cmp %ecx, %edi
    jle returnYes
    movl $0, %edx
    movl %edi, %eax
    idiv %ecx
    cmp $0, %edx
    je returnNo
    addl $2, %ecx
    jmp divisionLoop
  movl $0, %eax
  jmp end
  movl $1, %eax
  popl %edi
  popl %ebx

string2: .string "result=%dn"

Leave a Reply

Your email address will not be published. Required fields are marked *