# The MASM Forum

## General => The Campus => Topic started by: slc234 on October 09, 2012, 06:05:26 AM

Title: fibonacci question
Post by: slc234 on October 09, 2012, 06:05:26 AM
Hi I have a program that determines the seven numbers of the fibonacci sequence and puts them in the eax register with a loop. My question is: why do I have to move 12 into ecx, couldn't I move 7 instead since it is the first seven values? Is there another way to do this without decrementing it and by using code in data too?
Code: [Select]
`INCLUDE Irvine32.inc .code main PROC mov ebp, 0 mov edx, 1 mov ebx, edx mov ecx, 12 L1: mov eax, edx mov ebp, eax mov edx, ebx add ebx, ebp ; call DumpRegs ; dec ecx loop L1 exit main ENDP END main`
Title: Re: fibonacci question
Post by: RuiLoureiro on October 09, 2012, 07:46:42 AM
Hi I have a program that determines the seven numbers of the fibonacci sequence and puts them in the eax register with a loop. My question is: why do I have to move 12 into ecx, couldn't I move 7 instead since it is the first seven values? Is there another way to do this without decrementing it and by using code in data too?
Code: [Select]
`INCLUDE Irvine32.inc .code main PROC mov ebp, 0 mov edx, 1 mov ebx, edx mov ecx, 12 L1: mov eax, edx mov ebp, eax mov edx, ebx add ebx, ebp ; call DumpRegs ; dec ecx loop L1 exit main ENDP END main`

You should move 7; you dont need dec ecx, but you need
this:
mov   ecx, 7
L1:
push ecx
...
etc.
...
pop   ecx
loop L1
Title: Re: fibonacci question
Post by: slc234 on October 09, 2012, 07:51:27 AM
Is there a way to do it without the push and pop statement?
Title: Re: fibonacci question
Post by: RuiLoureiro on October 09, 2012, 07:54:22 AM
Is there a way to do it without the push and pop statement?
No because some procs inside L1 till loop L1 destroys ECX
Yes if you dont use any proc inside that loop
Title: Re: fibonacci question
Post by: slc234 on October 09, 2012, 09:24:42 AM
It is a silly question, but how would I do it with no Proc inside the loop? I'm trying to do it the most basic way to understand it.
Title: Re: fibonacci question
Post by: raymond on October 09, 2012, 01:21:49 PM
Not only can they destroy ECX, but also EDX and EAX. However, most external procedures are expected to preserve EBX, ESI and EDI. If you intend to call the DumpRegs procedure from within the loop without any push/pop, you could do it as follows. It is based on the standard Fibonacci sequence which starts with 1 and 1 as the F1 and F2 numbers.

ESI is initialized to the F0 Fibonacci number
EDI is initialized to the F1 Fibonacci number
EBX is used as the counter and initialized to the nth Fibonacci number you want to compute. 47 would be the maximum to use in EBX; the F48 Fibonacci number exceeds  32 bits.

Code: [Select]
`   mov esi,0   mov edi,1   mov ebx,7@@:   xchg esi,edi   call DumpRegs   add esi,edi   dec ebx   jnz @B   exit`
You could replace the DumpRegs procedure by another one which would display only the content of the ESI register.
Title: Re: fibonacci question
Post by: jj2007 on October 09, 2012, 07:04:28 PM
Not only can they destroy ECX, but also EDX and EAX. However, most external procedures are expected to preserve EBX, ESI and EDI. If you intend to call the DumpRegs procedure from within the loop without any push/pop,...

DumpRegs does not trash any registers, so you can safely use it inside a loop:

include \masm32\MasmBasic\IrvineMb\Irvine32Mb.inc     ; attached

Init
mov eax, 11111111h
mov ebx, 22222222h
mov ecx, 33333333h
mov edx, 44444444h
call DumpRegs      ; displays the regs
call DumpRegs      ; if DumpRegs changes them, they should now look different
Inkey "bye"
Exit
end start

Output:
EAX=11111111  EBX=22222222  ECX=33333333  EDX=44444444
ESI=00000000  EDI=00000000  EBP=0012FF94  ESP=0012FF8C
EIP=0040103D  EFL=00000246  CF=0  SF=0  ZF=1  OF=0  AF=0  PF=1

EAX=11111111  EBX=22222222  ECX=33333333  EDX=44444444
ESI=00000000  EDI=00000000  EBP=0012FF94  ESP=0012FF8C
EIP=00401042  EFL=00000246  CF=0  SF=0  ZF=1  OF=0  AF=0  PF=1
Title: Re: fibonacci question
Post by: raymond on October 10, 2012, 10:40:40 AM
Thanks for the clarification about the DumpRegs procedure. However, using the suggested registers would not need to be modified if any other display procedure not preserving ALL registers was used.