我制作了一个汇编程序来计算斐波那契数。它有很多问题,但是现在最紧迫的是,当我尝试使用预定义的输入(1、2、3)时,它会在打印正确的预定义值后出现段错误。这是我第一次尝试这样的跳跃,但我不确定自己做错了什么。任何帮助将非常感激!我在Linux Mint上编写,此代码仅适用于IA32。另外,asm_io来自我正在使用的教程。
; This(fibonacci):
; it(computes the kth fibonacci number)
; Set up io
%include "asm_io.inc"
; This puts initialized data into the .data segment
segment .data
; Let's get some data:
prompt1 db "Enter kth fibonacci you want to see: ", 0
outmsg1 db "The ", 0
outmsg2 db "th fibonacci number is: ", 0
f0: dd 0x1
f1: dd 0x2
f2: dd 0x2
f3: dd 0x3
; We will put uninitialized stuff in .bss
segment .bss
; Store the input
input1 resd 1
; Code gets put into .text
segment .text
global asm_main
asm_main:
; i need to learn what these do
enter 0,0
; it turns out this puts everything into the stack
; that exists before we begin
pusha
mov eax, prompt1
call print_string
call read_int
mov [input1], eax
start:
mov ecx, [input1]
mov edx, [f0]
mov ebx, [f1]
mov eax, [f2]
push eax
mov eax, 0x1
cmp ecx, eax
je inputisone
mov eax, 0x2
cmp ecx, eax
je inputistwo
mov eax, 0x3
cmp ecx, eax
je inputisthree
pop eax
fibloop:
add ebx, edx
mov edx, eax
mov eax, ebx
dec ecx
cmp ecx, 3
jne fibloop
quit:
push eax
jmp finish
inputisone:
mov eax, [f0]
push eax
jmp finish
inputistwo:
mov eax, [f0]
push eax
jmp finish
inputisthree:
mov eax, [f2]
push eax
jmp finish
finish:
; Register dump
dump_regs 1 ; Print register values
dump_mem 2, outmsg1, 1 ; Print memory
; Now we can print the results.
mov eax, outmsg1
call print_string
mov eax, [input1]
call print_int
mov eax, outmsg2
call print_string
pop eax
call print_int
call print_nl
; this returns everything from the stack that we placed there
; for safekeeping before we ran our program
popa
mov eax, 0
leave
ret ; back to C!
谢谢您的帮助!