显示斐波那契数列的前 24 个值

发布于 2024-10-25 13:45:58 字数 79 浏览 4 评论 0原文

我该如何编写一个程序,用汇编语言显示斐波那契数列的前 24 个值?

如果有人可以帮助我,我将不胜感激,我对汇编中的代码感到困惑。

How would I write a program that will display the first 24 values in the Fibonacci series in assembly language?

If anyone could help me I would greatly appreciate it, I'm confused with the code in assembly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

童话里做英雄 2024-11-01 13:45:58

嗯,你的做法与大多数其他语言的方式非常相似,如下所示:

for loop counter = 1 to 24 do
    next_number = fibonacci(previous, previous2)
    print(next_number)
    previous2 = previous
    previous = next_number

与其他语言的明显区别包括:

  1. 在这种情况下,你的“变量”可能都在寄存器中。
  2. 您可能需要编写自己的代码来转换和打印数字。

Well, you do it pretty similarly to the way you would in most other languages, something like this:

for loop counter = 1 to 24 do
    next_number = fibonacci(previous, previous2)
    print(next_number)
    previous2 = previous
    previous = next_number

The obvious differences from other languages include:

  1. In this case, your "variables" will all probably be in registers.
  2. You'll probably have to write your own code to convert and print a number.
小兔几 2024-11-01 13:45:58

我留下了两个空格,因为代码取决于它将运行的系统(您没有指定编译器和操作系统)。

我还没有测试过代码,但我认为它会起作用。

    mov eax, 0         ; first number
    mov ebx, 1         ; second number
                       ; edx will contain the third number (eax + ebx )

    mov ecx, 24 - 2    ; print 24 numbers (don't count the first
                         and second because they are printed in the begining)

    mov edx, eax
    call print_number  ; print the first number

    mov edx, ebx
    call print_number  ; print the second number
fibo:
    mov edx, eax
    add edx, ebx       ; edx = eax + ebx

    call print_number

    ; now we have the third number in edx
    ; eax = 1st, ebx = 2nd, edx = 3rd
    ; to prepare eax and abx for the next iteration, shift the values to the right
    ; eax = 2nd, ebx = 3rd, edx = ?
    mov eax, ebx
    mov ebx, edx

    loop fibo

    ; TO DO: exit program

print_number:
    ; TO DO: edx contains the number, print it
    return

希望有帮助。

I left two blank spaces because the code depends on the system where it's going to run (you didn't specify the compiler and operating system).

I haven't tested the code but I think it will work.

    mov eax, 0         ; first number
    mov ebx, 1         ; second number
                       ; edx will contain the third number (eax + ebx )

    mov ecx, 24 - 2    ; print 24 numbers (don't count the first
                         and second because they are printed in the begining)

    mov edx, eax
    call print_number  ; print the first number

    mov edx, ebx
    call print_number  ; print the second number
fibo:
    mov edx, eax
    add edx, ebx       ; edx = eax + ebx

    call print_number

    ; now we have the third number in edx
    ; eax = 1st, ebx = 2nd, edx = 3rd
    ; to prepare eax and abx for the next iteration, shift the values to the right
    ; eax = 2nd, ebx = 3rd, edx = ?
    mov eax, ebx
    mov ebx, edx

    loop fibo

    ; TO DO: exit program

print_number:
    ; TO DO: edx contains the number, print it
    return

Hope it helps.

北座城市 2024-11-01 13:45:58

斐波那契数列中的数字是其前面的两个数字之和。为了简单起见,您可以将这些数字存储在一个数组中,前两个元素设置为 1。 esi 和 edi 可以指向 n-1 和 n-2,所以 fibonacci(n) = [esi] + [edi]] 对?在伪代码中,它看起来像:

fibonacci    DWORD    24 dup (?)

esi = fibonacci(0)       // those are pointers to elements!
edi = fibonacci(1)

for x = 2 to 23
    fibonacci(x) = [esi] + [edi]
    esi += 4             // if you don't like DWORDs change this
    edi += 4
end loop

您可以将 x 保存在 ecx 寄存器中,将 fibonacci(x) 保存在 eax 寄存器中。

A number in Fibonacci series is the sum by the two numbers preceding it. To keep it simple you can store these numbers in an array with the first two element set to 1. esi and edi could point to n-1 and n-2, so fibonacci(n) = [esi] + [edi]] right? In pseudocode it looks like:

fibonacci    DWORD    24 dup (?)

esi = fibonacci(0)       // those are pointers to elements!
edi = fibonacci(1)

for x = 2 to 23
    fibonacci(x) = [esi] + [edi]
    esi += 4             // if you don't like DWORDs change this
    edi += 4
end loop

you can keep x in the ecx register and fibonacci(x) in the eax register.

动听の歌 2024-11-01 13:45:58

尝试这个代码,它会根据您的要求工作这个答案使用循环运行24次,循环标签是下一个,它首先从ax和bx获取数据,然后将其相加,所有函数都重复24次,直到循环完成。

   data segment
    org 0000h
    arr dw 17h dup(0000h)
    data ends
    code segment
    assume cs:code, ds:data
    start:  mov ax,data
        mov ds,ax
        mov cx,0018h
        lea si,arr
        inc si
        mov [si],01h
    next:   mov ax,[si-1]
        mov bx,[si]
        add ax,bx
        inc si
        mov [si],ax
        loop next
        mov ah,4ch
        int 21h
     code ends
    end start
    end

try this code it would work as per your requirements this answer makes use of a loop running for 24 times and the label looped is next which first takes the data from ax and bx and then adds it up these all functions are repeated for 24 times till the loop is completed.

   data segment
    org 0000h
    arr dw 17h dup(0000h)
    data ends
    code segment
    assume cs:code, ds:data
    start:  mov ax,data
        mov ds,ax
        mov cx,0018h
        lea si,arr
        inc si
        mov [si],01h
    next:   mov ax,[si-1]
        mov bx,[si]
        add ax,bx
        inc si
        mov [si],ax
        loop next
        mov ah,4ch
        int 21h
     code ends
    end start
    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文