Finalmente dopo una lunga sessione di innumerevoli errori, spero che questo sia l'ultimo.Fibonacci Series in Assembly x86
Nessun errore di compilazione o di runtime, solo un errore logico.
EDIT: (Pseudocodice fisso)
mio Pseudocodice:
first = 1;
second = 1;
third = 0;
for i from 1 to n{
third=first+second
first=second
second=third
}
return third
Questo sarebbe stampare il risultato finale della serie.
My Code Montaggio:
Ho aggiunto commenti dove mai possibile
.386
.model flat,stdcall
option casemap:none
.data
timestell db "Loop Ran : %d Times -----",0 ;format string
fmtd db "%d",0
finalprint db "Final Number is : %d ------",0 ;format string
times dd 0Ah ;times to loop
first dd 1h
second dd 1h
third dd 0h
.data?
retvalue1 dd ? ;we will initialize it later
.code
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
includelib MSVCRT
extrn printf:near
extrn exit:near
public main
main proc
mov ecx, times ;loop "times" times
mov eax,0 ;just to store number of times loop ran
top: ;body of loop
cmp ecx, 0 ;test at top of loop
je bottom ;loop exit when while condition false
add eax,1 ;Just to test number of times loop ran
mov ebx,first ;move first into ebx
add ebx,second ;add ebx, [ first+second ]
mov third,ebx ;Copy result i.e ebx [first+second] to third
xor ebx,ebx ;clear for further use
mov ebx,first ;move first into ebx
mov second,ebx ;copy ebx to second [NOW second=first]
xor ebx,ebx ;clear for later use
mov ebx,third ;move thirs into ebx
mov second,ebx ;copy ebx to third [NOW second=third]
xor ebx,ebx ;clear it
dec ecx ;decrement loop
jmp top ;Loop again
bottom:
mov retvalue1,eax ;store eax into a variable
push retvalue1 ;pass this variable to printf
push offset timestell ;pass Format string to printf
call printf ;Print no. of times loop ran
push third ;push value of third to printf
push offset finalprint ;push the format string
call printf ;Print the final number
push 0 ;exit gracefully
call exit ;exit system
main endp
end main
Il codice funziona bene, ma l'uscita non mi soddisfa:
uscita: Loop Ran : 10 Times -----Final Number is : 11 ------
Prima di tutto non sono rea Sono sicuro che il numero finale è in forma decimale o esadecimale.
- Assumendo come decimali: Fibonacci Series non ha 11
- Assumendo come esadecimale: Fibonacci Series non ha 17 (11 hex = 17 dicembre)
Che cosa sto facendo sbagliato?
Non c'è bisogno di essere sicuri se il numero stampato è in decimale. 'printf' usa la stringa' finalprint' come formato, e se assomiglia a un normale 'printf', userà'% d' come output decimale. – usr2564301
Basta confrontare i tuoi commenti con quello che volevi davvero fare;) 'ORA secondo = prima' sì ma volevi' primo = secondo' ... oops. Ottieni un +1 per i commenti, è così che possiamo individuare il tuo errore. – Jester
Nota: lo pseudocodice restituisce il numero corretto di Fibonacci, anche se per n = 10 restituisce '144', tecnicamente il * 12th * numero di fib (o' 89', a seconda di come 'n' viene inizializzato, ma è ancora troppo lontano). – usr2564301