gpt4 book ai didi

assembly - assembly x86 中的斐波那契数列

转载 作者:行者123 更新时间:2023-12-04 12:29:49 28 4
gpt4 key购买 nike

终于在无数次错误的漫长 session 之后,希望这是最后一个。

没有编译或运行时错误,只是一个逻辑错误。

编辑:(固定伪代码)

我的伪代码:

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

for i from 1 to n{

third=first+second
first=second
second=third

}
return third

这将打印该系列的最终结果。

我的汇编代码:

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

代码运行良好,但输出不满足我:

输出: Loop Ran : 10 Times -----Final Number is : 11 ------
首先,我不确定最终数字是十进制还是十六进制形式。
  • 假设它是十进制:斐波那契数列没有 11
  • 假设它是十六进制:斐波那契数列没有 17 (11 hex = 17 dec)

  • 我究竟做错了什么?

    最佳答案

    问题是我的实际代码与我的伪代码不匹配,导致了逻辑错误。

    这部分

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

    这给 first second 的值,但我的伪代码说“first=second”,这意味着给出 second 的值至 first .
         mov ebx,second      ;move second into ebx
    mov first,ebx ;copy ebx to second [NOW first=second]

    x86 Intel 处理器的最终工作代码:

    对于任何进一步的推荐人,我正在发布 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

    关于assembly - assembly x86 中的斐波那契数列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35579861/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com