How to resolve the algorithm Factors of an integer step by step in the X86 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Factors of an integer step by step in the X86 Assembly programming language

Table of Contents

Problem Statement

Compute the   factors   of a positive integer. These factors are the positive integers by which the number being factored can be divided to yield a positive integer result. (Though the concepts function correctly for zero and negative integers, the set of factors of zero has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty;   this task does not require handling of either of these cases). Note that every prime number has two factors:   1   and itself.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factors of an integer step by step in the X86 Assembly programming language

Source code in the x86 programming language

section .bss 
    factorArr resd 250 ;big buffer against seg fault
    
section .text
global _main
_main:
    mov ebp, esp; for correct debugging
    mov eax, 0x7ffffffe ;number of which we want to know the factors, max num this program works with
    mov ebx, eax ;save eax
    mov ecx, 1 ;n, factor we test for
    mov [factorArr], dword 0
    looping:
        mov eax, ebx ;restore eax
        xor edx, edx ;clear edx
        div ecx
        cmp edx, 0 ;test if our number % n == 0
        jne next
        mov edx, [factorArr] ;if yes, we increment the size of the array and append n
        inc edx
        mov [factorArr+edx*4], ecx ;appending n
        mov [factorArr], edx ;storing the new size
    next:
        mov eax, ecx
        cmp eax, ebx ;is n bigger then our number ?
        jg end ;if yes we end
        inc ecx
        jmp looping
    end:
        mov ecx, factorArr ;pass arr address by ecx  
        xor eax, eax ;clear eax
        mov esp, ebp ;garbage collecting
        ret

  

You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Inform 6 programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Balanced ternary step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm JSON step by step in the Sidef programming language