How to resolve the algorithm Abundant odd numbers step by step in the X86 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abundant odd numbers step by step in the X86 Assembly programming language

Table of Contents

Problem Statement

An Abundant number is a number n for which the   sum of divisors   σ(n) > 2n, or,   equivalently,   the   sum of proper divisors   (or aliquot sum)       s(n) > n.

12   is abundant, it has the proper divisors     1,2,3,4 & 6     which sum to   16   ( > 12 or n);        or alternately,   has the sigma sum of   1,2,3,4,6 & 12   which sum to   28   ( > 24 or 2n).

Abundant numbers are common, though even abundant numbers seem to be much more common than odd abundant numbers. To make things more interesting, this task is specifically about finding   odd abundant numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abundant odd numbers step by step in the X86 Assembly programming language

Source code in the x86 programming language

        .model  tiny
        .code
        .486
        org     100h
;ebp=counter, edi=Num, ebx=Div, esi=Sum
start:  xor     ebp, ebp        ;odd abundant number counter:= 0
        mov     edi, 3          ;Num:= 3
ab10:   mov     ebx, 3          ;Div:= 3
        mov     esi, 1          ;Sum:= 1
ab20:   mov     eax, edi        ;Quot:= Num/Div
        cdq                     ;edx:= 0
        div     ebx             ;eax(q):edx(r):= edx:eax/ebx
        cmp     ebx, eax        ;if Div > Quot then quit loop
        jge     ab50
         test   edx, edx        ;if remainder = 0 then
         jne    ab30
          add   esi, ebx        ;  Sum:= Sum + Div
          cmp   ebx, eax        ;  if Div # Quot then
          je    ab30
          add   esi, eax        ;    Sum:= Sum + Quot
ab30:    add    ebx, 2          ;Div:= Div+2 (only check odd Nums)
         jmp    ab20            ;loop
ab50:
        cmp     esi, edi        ;if Sum > Num then
        jle     ab80
        inc     ebp             ;  counter:= counter+1
        cmp     ebp, 25         ;  if counter<=25 or counter>=1000 then
        jle     ab60
         cmp    ebp, 1000
         jl     ab80
ab60:   mov     eax, edi        ;    print Num
        call    numout
        mov     al, ' '         ;    print spaces
        int     29h
        int     29h
        mov     eax, esi        ;    print Sum
        call    numout
        mov     al, 0Dh         ;    carriage return
        int     29h
        mov     al, 0Ah         ;    line feed
        int     29h
        cmp     ebp, 1000       ;    if counter = 1000 then
        jne     ab65
         mov    edi, 1000000001-2 ;    Num:= 1,000,000,001 - 2
ab65:   cmp     edi, 1000000000 ;      if Num > 1,000,000,000 then exit
        jg      ab90
ab80:   add     edi, 2          ;Num:= Num+2 (only check odd Nums)
        jmp     ab10            ;loop
ab90:   ret

;Print signed integer in eax with commas, e.g: 12,345,010
numout: xor     ecx, ecx        ;digit counter:= 0
no00:   cdq                     ;edx:= 0
        mov     ebx, 10         ;Num:= Num/10
        div     ebx             ;eax(q):edx(r):= edx:eax/ebx
        push    edx             ;remainder = least significant digit
        inc     ecx             ;count digit
        test    eax, eax        ;if Num # 0 then NumOut(Num)
        je      no20
         call   no00
no20:   pop     eax             ;print digit + '0'
        add     al, '0'
        int     29h
        dec     ecx             ;un-count digit
        je      no30            ;if counter # 0 and
         mov    al, cl          ;  if remainder(counter/3) = 0 then
         aam    3
         jne    no30
          mov   al, ','         ;    print ','
          int   29h
no30:   ret
        end     start


  

You may also check:How to resolve the algorithm Integer comparison step by step in the PHL programming language
You may also check:How to resolve the algorithm Variadic function step by step in the TIScript programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Fork step by step in the BASIC programming language
You may also check:How to resolve the algorithm Read entire file step by step in the Julia programming language