斐波纳契数列在大会的x86 [英] Fibonacci Series in Assembly x86
问题描述
最后无数错误长会议结束后,希望这是最后一个。
没有编译或运行时错误,只是一个逻辑上的错误。
编辑:(固定伪code)
我的伪code:
第一= 1;
第二= 1;
第三= 0; 对于i从1到n { 第三=第一+第二
第一=第二
第二=第三}
第三回
这将打印该系列的最终结果。
我的大会code:
我添加了评论,其中可以没有
块引用>.386
.MODEL平,STDCALL
选项casemap:无。数据
timestell DB循环冉:%d次-----,0;格式字符串
FMTD DB%d个0
finalprint DB最终数目为:%d个------,0;格式字符串
次DD 0AH;次循环
第一DD 1H
第二DD 1H
第三DD 0H
。数据?retvalue1 DD? ;我们将在稍后初始化。code
包括windows.inc
包括user32.inc
includelib USER32.LIB
包括kernel32.inc
includelib KERNEL32.LIB
includelib MSVCRT
EXTRN的printf:临近
EXTRN退出:临近公共主
主要PROC
MOV ECX,倍;循环时代时代
MOV EAX,0;只是次循环的门店数量跑
顶部:;循环体
CMP ECX,0;试验在循环的顶部
JE底部;循环退出的时候,而条件为假
添加EAX,1;只是为了测试时间循环次数跑
MOV EBX,第一;第一个进入EBX
添加EBX第二;新增EBX,[第一+秒]
MOV第三,EBX;复制导致即EBX [第一+秒]至第三
XOR EBX,EBX;明确进一步使用
MOV EBX,第一;第一个进入EBX
MOV第二,EBX; EBX复制到第二个[NOW秒=首页]
XOR EBX,EBX;明确以备后用
MOV EBX,第三位;移动thirs到EBX
MOV第二,EBX;复制到EBX第三[NOW秒=三]
XOR EBX,EBX;清除它
十二月ecx;减少循环
JMP顶部;再次循环 底部:
MOV retvalue1,EAX; EAX店到一个变量
推retvalue1;通过该变量的printf
推偏移timestell;传递格式字符串的printf
调用printf;打印没有。次跑环
推第三;第三推值的printf
推偏移finalprint;推格式字符串
调用printf;打印的最终数目
推0;优雅地退出
调用exit;退出系统主要ENDP最终主在code运行良好,但产量不会满足我:
输出:
循环冉:10次-----最终数目为:11 ------
首先我真的不知道这最后的数字是十进制或十六进制形式。
- 假定它为十进制:斐波纳契系列没有11
- 假定它为十六进制:斐波纳契系列没有17(11进制= 12月17日)
我究竟做错了什么?
解决方案但问题是,我的实际code不是用我的伪code这导致出现逻辑错误匹配。
这部分
MOV EBX,第一;第一个进入EBX
MOV第二,EBX; EBX复制到第二个[NOW秒=首页]这给了
,但我的伪code说:第一=第二这意味着给
第一
值第二个的值第二个
到第一
。MOV EBX,第二位;第二次转移到EBX
MOV第一,EBX,EBX复制到第二个[第一NOW =秒]最后的工作code用于x86 Intel处理器:
有关任何进一步的引荐,我张贴工作code用于x86英特尔
.386
.MODEL平,STDCALL
选项casemap:无。数据
timestell DB循环冉:%d次-----,0;格式字符串
finalprint DB%d个次Fibonacci数为%d,0;格式字符串
次DD 14H;次循环
第一DD 1H
第二DD 1H
第三DD 0H。code
包括windows.inc
包括user32.inc
includelib USER32.LIB
包括kernel32.inc
includelib KERNEL32.LIB
includelib MSVCRT
EXTRN的printf:临近
EXTRN退出:临近公共主
主要PROC
MOV ECX,时间;设置循环计数器为时间时间
子ECX,2,循环次数2次 最佳:
CMP ECX,0;试验在循环的顶部
JE底部;循环退出的时候,而条件为假
XOR EBX,EBX;清除EBX
MOV EBX,第一;第一个进入EBX
添加EBX第二;新增EBX,[第一+秒]
MOV第三,EBX;复制导致即EBX [第一+秒]至第三
XOR EBX,EBX;明确进一步使用
MOV EBX,第二位;第二次转移到EBX
MOV第一,EBX,EBX复制到第二个[第一NOW =秒]
XOR EBX,EBX;明确以备后用
MOV EBX,第三位;移动thirs到EBX
MOV第二,EBX;复制到EBX第三[NOW秒=三]
XOR EBX,EBX;清除它
十二月ecx;减少循环
JMP顶部;再次循环 底部:
第三推
推倍;第三推值的printf
推偏移finalprint;推格式字符串
调用printf;打印的最终数目
推0;优雅地退出
调用exit;退出系统 主要ENDP最终主Finally after a long session of countless errors , hope this is the final one.
No Compile or runtime errors, Just a logical error.
EDIT: (Fixed Pseudocode)
My Pseudocode:
first = 1; second = 1; third = 0; for i from 1 to n{ third=first+second first=second second=third } return third
This would print the final result of the series.
My Assembly Code:
I have added Comments where ever possible
.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
The code runs well but the output doesn't satisfies me:
Output:
Loop Ran : 10 Times -----Final Number is : 11 ------
First of all i am not really sure that Final number is in decimal or hex form.
- Assuming it as decimal : Fibonacci Series doesn't have 11
- Assuming it as hex : Fibonacci Series doesn't have 17 (11 hex = 17 dec)
What am i doing wrong?
解决方案The problem was that my Actual code was not matching with my Pseudocode which resulted in the Logical error.
This Part
mov ebx,first ;move first into ebx mov second,ebx ;copy ebx to second [NOW second=first]
This gives
first
value ofsecond
, but my PseudoCode says "first=second", which means give value ofsecond
tofirst
.mov ebx,second ;move second into ebx mov first,ebx ;copy ebx to second [NOW first=second]
Final Working Code for x86 Intel Processor:
For any further referrers , i am posting a working code for x86 intel
.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
这篇关于斐波纳契数列在大会的x86的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!