2016-02-23 11 views
7

अंतहीन अनगिनत त्रुटियों के लंबे सत्र के बाद, उम्मीद है कि यह अंतिम है।विधानसभा में फिबोनाची श्रृंखला x86

कोई संकलन या रनटाइम त्रुटियां, बस एक तार्किक त्रुटि।

संपादित करें: (फिक्स्ड स्यूडोकोड)

मेरे स्यूडोकोड:

first = 1; 
second = 1; 
third = 0; 

for i from 1 to n{ 

    third=first+second 
    first=second 
    second=third 

} 
return third 

इस श्रृंखला का अंतिम परिणाम मुद्रित होगा।

मेरे विधानसभा कोड:

आउटपुट:: Loop Ran : 10 Times -----Final Number is : 11 ------

मैं टिप्पणी नहीं जहां कभी संभव

.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 

कोड अच्छी तरह से चलता है, लेकिन उत्पादन मुझे संतुष्ट नहीं है जोड़ लिया है

सबसे पहले मैं rea नहीं हूँ कृपया सुनिश्चित करें कि अंतिम संख्या दशमलव या हेक्स रूप में है।

  • दशमलव के रूप में यह मान लिया जाये कि: फाइबोनैचि सीरीज 11
  • हेक्स के रूप में यह मान लिया जाये कि नहीं है: फाइबोनैचि सीरीज नहीं है 17 (11 हेक्स = 17 dec)

मैं क्या कर रहा हूँ गलत?

+0

अनिश्चित अगर मुद्रित संख्या में है होना करने के लिए कोई ज़रूरत नहीं के लिए एक काम कोड पोस्टिंग कर रहा हूँ दशमलव। 'printf' प्रारूप के रूप में 'फाइनलप्रिंट' स्ट्रिंग का उपयोग करता है, और यदि यह नियमित 'printf' जैसा कुछ भी है, तो यह दशमलव के रूप में आउटपुट के लिए'% d' का उपयोग करेगा। – usr2564301

+2

बस अपनी टिप्पणियों की तुलना करें जो आप वास्तव में करना चाहते थे;) 'अब दूसरा = पहला' हाँ, लेकिन आप 'पहला = दूसरा' चाहते थे ... ओह। आपको टिप्पणी करने के लिए +1 मिलता है, इस प्रकार हम आपकी त्रुटि को खोज सकते हैं। – Jester

+0

नोट: छद्म कोड सही फाइबोनैकी संख्या देता है, हालांकि एन = 10 के लिए यह '144' लौटाता है, तकनीकी रूप से * 12 वीं * फिब संख्या (या' 89', 'एन' कैसे शुरू होता है, इस पर निर्भर करता है, लेकिन यह अभी भी एक बहुत दूर है)। – usr2564301

उत्तर

4

समस्या यह थी कि मेरा वास्तविक कोड मेरे छद्म कोड के साथ मेल नहीं खा रहा था जिसके परिणामस्वरूप तार्किक त्रुटि हुई।

इस भाग

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

यह second की first मूल्य देता है, लेकिन मेरे स्यूडोकोड कहते हैं, "पहले = दूसरा", first करने के लिए second का मूल्य देना है जिसका अर्थ है।

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

अंतिम कार्य संहिता 86 इंटेल प्रोसेसर के लिए:

किसी भी आगे सन्दर्भदाता के लिए, मैं 86 इंटेल

.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 
+0

एक सामान्य टिप्पणी के रूप में, रजिस्ट्रारों में मूल्यों को संग्रहीत करने वाला कोड हमेशा समान कोड से छोटा, छोटा और तेज़ होता है जो स्मृति में मूल्य डालता है, हालांकि पंजीकरण करने के लिए थोड़ा और काम करने की आवश्यकता है जिसमें कौन सा चर है। आप आसानी से यहां कर सकते थे। एक और सबक आपने यह सीखा है कि असेंबली कोड लिखने के प्रयास से पहले उच्च स्तर की भाषा में एल्गोरिदम को वास्तव में कोड (स्यूडोकोड नहीं) के लिए उपयुक्त है। एचएलएल में एक त्रुटि देखना असेंबली में एक जैसा दिखने से कहीं ज्यादा आसान है। – Gene

+0

@ जीन क्षमा करें, मैं असेंबली के लिए एक पूर्ण नौसिखिया हूं क्योंकि यह अलग है। मैंने समझ में नहीं आया कि आपने क्या कहा। क्या आप यह कहना चाहते थे कि मुझे यह भी टिप्पणी करनी चाहिए कि किस चर में पंजीकरण है? जैसे 'ebx में दूसरे स्थानांतरित करें' की तरह मुझे 'ebx = second' लिखा होगा? मैंने पहले जावास्क्रिप्ट में छद्म कोड का परीक्षण किया और मुझे लगता है कि मेरा कोड सही संख्या देता है लेकिन अनुरोधित एनएच नंबर से दो कदम आगे, मैंने सादगी पर ध्यान केंद्रित किया जिसने मुझे इस छोटी सी त्रुटि को याद किया। – Idkwhoami

+0

आप अच्छी तरह से कर रहे हैं। नहीं, मैं कह रहा हूं कि आपको स्मृति स्थानों की बिल्कुल आवश्यकता नहीं है। जैसे पहले, दूसरे और तीसरे के लिए esi, edi, और edx का उपयोग करें। यह अभी भी लूप गिनती, अस्थायी मूल्य इत्यादि के लिए 3 रजिस्टरों को छोड़ देता है। – Gene