Hi, I'm new here.
I have to write a short program that take the user input from stack and finds its factors.
Then the factors are placed into an array (also taken from stack) and the returned to main.
Any help?

Thanks
Posted on 2010-03-17 10:20:23 by ZosoLzrd
Sure, find out how to factor a given number, write it down in pseudo code then translate that pseudo code into assembly.

Ask again when you have a little more of code together... we're glad to help :)
Posted on 2010-03-17 10:34:26 by JimmyClif
I have the basic idea..

push array
push input

subprogram get factors:
push registers
push ebp
mov ebp, esp

mov ecx, input
mov eax, input
mov ebx, array

loop
    divide eax by ecx
Posted on 2010-03-17 10:36:53 by ZosoLzrd
sorry posted without finishing..

after dividing..
cmp  edx (where remainder is), with zero
if edx = o

then put eax (divisor) into array

if not, then start loop again until loop decrements ecx to 0.

I'm having trouble understanding how to pass array into stack, change it,
then pass it back to main and print out
Posted on 2010-03-17 10:39:42 by ZosoLzrd
ZosoLzrd"]I'm having trouble understanding how to pass array into stack, change it, then pass it back to main and print out


In most cases array is passed as a pointer to its first (zeroth? ;-)) element.

Should this program return non-prime factors too?

What is that o in if edx = o?
Posted on 2010-03-17 11:40:54 by baldr
We had this question on the Nasm forum a few days ago...

http://forum.nasm.us/index.php?topic=769.0

I don't think my reply to that will help you much, but you might want to take a look.

I would do the "push registers" (that need to be preserved) after the "push ebp"/"mov ebp, esp". That will put your arguments in the "usual" place - , , etc. You might want a local variable or two...

If you start with eax=ecx=input, the "div" will give 1 and input as factors - not really what you want. You may want to start with small ecx and work up - lest you find "factors with factors", if you see what I mean. (don't forget about edx, when you "div"!) The "div" will alter eax. In case you don't have a factor (edx != 0), you'll want to restore the old value of eax before the next trial value of ecx. If you do find a factor, save it ("mov , ecx"/"add ebx, 4", or so). It occurs to me that you'll probably want to count factors you find, and return the count - to be able to display them correctly. Then you'll want to continue with the same eax, but starting over with ecx values. I think that's how I'd do it...

Best,
Frank



Posted on 2010-03-17 11:47:44 by fbkotler