Problem 5 on Project Euler with x86 Assembly


In order to practice x86 Assembly (NASM especifically) I am solving some problems on Project Euler with it. Here’s problem 5:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

And the solution:

.text
.globl main

main:
        pushl %ebp
        movl %esp, %ebp

        movl $50, %ecx
        loop:
                movl $2, %esi
                divisionLoop:
                        cmp  $21, %esi
                        je found
                        movl $0, %edx
                        movl %ecx, %eax
                        idiv %esi
                        cmp  $0, %edx
                        je noRemainder
                        add $1, %ecx
                        jmp loop
                        noRemainder:
                        add $1, %esi
                        jmp divisionLoop
        found:
        pushl %ecx
        pushl $string2
        call printf

        movl %ebp, %esp
        popl %ebp

        movl $0, %eax
        ret
.data
string2: .string "result=%dn"

Leave a Reply

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