assembly x86 fibonacci

assembly - Serie Fibonacci en ensamble x86



(1)

El problema era que mi código real no coincidía con mi pseudocódigo que dio como resultado el error lógico.

Esta parte

mov ebx,first ;move first into ebx mov second,ebx ;copy ebx to second [NOW second=first]

Esto da el first valor de second , pero mi PseudoCódigo dice "primero = segundo", lo que significa dar valor de second a first .

mov ebx,second ;move second into ebx mov first,ebx ;copy ebx to second [NOW first=second]

Código de trabajo final para el procesador Intel x86:

Para cualquier otra referencia, estoy publicando un código de trabajo para Intel x86

.386 .model flat,stdcall option casemap:none .data timestell db "Loop Ran : %d Times -----",0 ;format string finalprint db "%d th Fibonacci number is %d",0 ;format string times dd 14h ;times to loop first dd 1h second dd 1h third dd 0h .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 ;set loop counter to "times" time sub ecx,2 ;loop times-2 times top: cmp ecx, 0 ; test at top of loop je bottom ; loop exit when while condition false xor ebx,ebx ;Clear ebx 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,second ;move second into ebx mov first,ebx ;copy ebx to second [NOW first=second] 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: push third push times ;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

Finalmente, después de una larga sesión de innumerables errores, espero que este sea el último.

Sin errores de compilación o tiempo de ejecución, solo un error lógico.

EDITAR: (Pseudocódigo fijo)

Mi pseudocódigo:

first = 1; second = 1; third = 0; for i from 1 to n{ third=first+second first=second second=third } return third

Esto imprimiría el resultado final de la serie.

Mi código de ensamblaje:

He añadido comentarios siempre que sea posible

.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

El código funciona bien pero la salida no me satisface:

Salida: Loop Ran : 10 Times -----Final Number is : 11 ------

En primer lugar, no estoy seguro de que el número final esté en forma decimal o hexagonal.

  • Asumiéndolo como decimal: la serie de Fibonacci no tiene 11
  • Asumiendo que es un hex: la serie Fibonacci no tiene 17 (11 hex = 17 dec)

¿Qué estoy haciendo mal?