News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Code Trouble

Started by etow, January 24, 2008, 08:21:56 PM

Previous topic - Next topic

GregL

Hey etow,

Please use Code Tags when posting code. You can use the 'Insert Code' button or just put in the tags manually.


etow

Yes, thanks

All my problems are solved.

GregL

Keep at it etow.  :U 


etow

Here is my final code in zip file.

[attachment deleted by admin]

RuiLoureiro

Etow,
           I want to say that i am not a programmer and i am not young.
           Are you a mathematics student ? You have some troubles with Math.

; This code should be:           

.While Factor <= Esi      ; while Factor less than or equal to (Number = Esi)
Mov Eax, Number     ; set Eax = Number
Mov Ebx, Factor       ; set Ebx = Factor
Cdq                        ; convert to double quad word or 64-bit long word
Div   Ebx                 ; Eax = Eax / Ebx
Mov Quotient, Eax    ; set Quotient = Eax
                              ; Quotient = Number / Factor

;                  Mov Eax, Number     ; set Eax = Number
;                  Cdq                 ; convert to double quad word or 64-bit long word
;                  Mov Ebx, Factor     ; set Ebx = Factor
;                  Div Ebx             ; Eax = Eax / Ebx, Edx contains the remainder

            Do you believe you need to divide EDX:EAX 2 times, one to get
            the quotient and the other to get the remainder ? Why ? And
            then, why do you believe we need to do it one more time ? Why ?
            This is the case here:

; This code should be:           

Inc primeCounter        ; primeCounter = primeCounter + 1
                               ; generate PrimeFactor string by
                               ; concatenating prime factors and
                               ; adding them to PrimeFactor string
Mov Esi, Quotient       ; set Esi = Quotient
Mov Number, Esi         ; Esi = Number = Quotient
;                Mov Edi, Quotient       ; set Edi = Quotient
;                Mov Number, Edi         ; Edi = Number = Quotient             
;                Mov Eax, Number         ; set Eax = Number
;                Mov Ebx, Factor     ; set Ebx = Factor
;                Cdq                 ; convert to double quad word or 64-bit long word
;                Div Ebx             ; Eax = Eax / Ebx
;                Mov Quotient, Eax   ; set Quotient = Eax
                           ; Quotient = Number / Factor
                           ;    Mov Remainder, Edx  ; set Remainder = Edx


            I try to explain: you want to get Factor *** While Factor <= Esi ***
                              where ESI is the Number to be factored.
                       
            Look at this example:  Number=70, Factor=2 (at the begining). ESI=70.
           
                           Number               Factor
            Now, begin:     70      divided by    2     quotient=35     remainder=0

            It means:   70 = 2 * 35 and means also that to get the next factor of 70
                                    we need to get the factors of the Number=35
                                    and we begin with the same Factor=2;
            ;.......................................................................
            Now we want to get the factors of 35, so Number=35, ESI=35, Factor=2

                           Number               Factor
            Next step:       35      divided by    2     quotient=???    remainder=not 0
                             35                    3             =???             =not 0
                             35                    4             =???             =not 0
                             35                    5     quotient=7      remainder= 0

            It means:   35 = 5 * 7 and means also that to get the next factor
                                    we need to use the Number=7 and the same Factor=5;
                                   
            Then:   70 = 2 * 35 = 2 * 5 * 7 because 35 = 5 * 7
            ;.........................................................................
            Now we want to get the factors of 7, so Number=7, ESI=7, Factor=5

                           Number               Factor
            Next step:       7      divided by    5     quotient=???    remainder=not 0
                             7                    6             =???             =not 0
                             7                    7     quotient= 1    remaider= 0
           
            It means:   7 = 7 * 1 and means also that to get the next factor
                                  we need to use the Number=1 and the same Factor=7;
                                  ESI should be =1, too.

            Then:   70 = 2 * 5 * 7.
                                     
            Now stop because Factor is not less than or equal ESI (ESI=1 and Factor=7).

            Isnt it?
RuiLoureiro           

etow

Hi RuiLoureiro,

Sorry I have not cleaned the code properly.
No, I do not have problems with math.  I do well with math.
I think I have difficulty visualizing using assembly language instructions to get the math to work properly, that is all.


etow

Here is the algorithm to get the prime factorization:


PrimeFactor = " "
Factor = 2

Do while (Factor <= Number)
   Quotient = Number / Factor
   Remainder = Number mod Factor

   If (Remainder = 0) then
          .If (primeCounter = 1) ; if primeCounter equals to 1
              PrimeFactor = concat(PrimeFactor, Factor)
              ; insert the first prime factor to the PrimeFactor string
           .ElseIf (primeCounter > 1) ; if primeCounter greater than 1
                PrimeFactor = concat(PrimeFactor, " x ", Factor)
                ; concatenate additional prime factors to the PrimeFactor string
            .EndIf
           Number = Quotient
           Quotient = Number / Factor
           Remainder = Number mod Factor
   Else
      Factor = Factor + 1
   EndIf
Loop



etow

Here is my edited final code.



[attachment deleted by admin]

etow

Sorry I forgot to mention how to increment primeCounter.

Here is the revised algorithm to do prime factorization:



primeCounter = 1
PrimeFactor = " "
Factor = 2

Do while (Factor <= Number)
   Quotient = Number / Factor
   Remainder = Number mod Factor

   If (Remainder = 0) then

          .If (primeCounter = 1) then ; if primeCounter equals to 1
              PrimeFactor = concat(PrimeFactor, Factor)
              ; insert the first prime factor to the PrimeFactor string
           .ElseIf (primeCounter > 1) then ; if primeCounter greater than 1
                PrimeFactor = concat(PrimeFactor, " x ", Factor)
                ; concatenate additional prime factors to the PrimeFactor string
            .EndIf

           Number = Quotient
           Quotient = Number / Factor
           Remainder = Number mod Factor

           primeCounter = primeCounter + 1           
   ElseIf
      Factor = Factor + 1
   EndIf
Loop


etow

Reminder:

Number is a positive integer greater or equal to 2.


etow

Another version of prime factorization but with a procedure call :



[attachment deleted by admin]