Factors of an integer: Difference between revisions

m
factors get saved at reserved RAM space instead of stack now
No edit summary
m (factors get saved at reserved RAM space instead of stack now)
Line 4,691:
{{works with|nasm}}
<lang asm>
section .bss
factorArr resd 250 ;big buffer against seg fault
section .text
global _main
_main:
mov ebp, esp;program putsfor allcorrect factors on the local stack surrounded by 0xffffffff (-1)debugging
mov eax, 123450x7ffffffe ;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
push -1; boundary
looping:
mov eax, ebx ;restore eax
xor edx, edx ;clear edx
div ecx ;divide eax by ecx (our number by our current factor)
cmp edx, 0 ;edx stores remainder,test if itour isnumber 0% wen have== a factor0
jne next
pushmov ecxedx, [factorArr] ;if yes, we storeincrement the foundsize factor onof the stackarray 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 ;compareis then factorbigger wethen test for with the actualour number, if the factor is greater we jump to end?
jg end ;if yes we end
inc ecx ;add 1 to the factor
jmp looping ;do it again
end:
pushmov -1ecx, factorArr ;another -1 to markpass thearr boundaryaddress onby theecx other side
xor eax, eax ;clear eax
mov eax, ebp ;eax now contains highest address of the stack -> maybe call output with that
mov esp, ebp ;garbage collecting
ret
</lang>
13

edits