Assembly 奇怪的汇编语言添加错误
我正在用汇编语言编程斐波那契序列的一个实现,我发现了一个奇怪的错误。它一开始工作,但当我达到8+13(也就是说,在十六进制中为8+D)时,它会给我15。我正在使用VisualStudio10/MASM 32编译/运行此文件 下面是我的代码(Irvine32是一个包含一些实用函数的库),然后我将解释运行它时得到的输出:Assembly 奇怪的汇编语言添加错误,assembly,x86,masm32,irvine32,Assembly,X86,Masm32,Irvine32,我正在用汇编语言编程斐波那契序列的一个实现,我发现了一个奇怪的错误。它一开始工作,但当我达到8+13(也就是说,在十六进制中为8+D)时,它会给我15。我正在使用VisualStudio10/MASM 32编译/运行此文件 下面是我的代码(Irvine32是一个包含一些实用函数的库),然后我将解释运行它时得到的输出: TITLE Fibonacci INCLUDE Irvine32.inc .data .code main PROC mov eax, 0 mov ebx, 1 mo
TITLE Fibonacci
INCLUDE Irvine32.inc
.data
.code
main PROC
mov eax, 0
mov ebx, 1
mov ecx,12 ; set the loop counter to 12
;the 12 registry dumps display fib(1)-fib(12) in the eax register.
;As this is iteration based instead of loop based, the math comments
;in the loop are slighty off at the begining because of the edge condition
;so my comments about fib(n-1), etc. are valid only at fib(n) where n >= 2
fib:
mov edx,eax ;store fib(n-1) in a register temporarily
;although the first time the loop runs,
add eax,ebx ;add fib(n-1) to fib(n-2) to get fib(n)
mov ebx,edx ;replace fib(n-2) with (fib n-1)
;at this point, eax holds fib(n), and ebx hold fib(n-1), which will be
;used to calculate fib (n+1) for the next loop iteration
call DumpRegs
loop fib
exit; exit the program
main ENDP
END main ; first procedure called is main
我的eax
从DumpRegs
寄存器转储的顺序如下:,
1,1,2,3,5,8,D,15,22,37,59,90
如您所见,这偏离了正确的Fib序列,即“D”。所以,我怎样才能解决这个问题,更重要的是,我想知道这里发生了什么导致了这个问题。谢谢
编辑:好吧,我明白我愚蠢的错误了。显然所有的输出都是十六进制的。嗯,我想这只是又一次提醒我不要匆忙。再次感谢你的帮助 更新备注:您的代码工作正常,您感到困惑的唯一原因是大多数输出不会产生十六进制字母,看起来像小数。15H是21D,这是正确的数字 这应该起作用:
mov ax,0
mov bx,1
mov cx,12
step:
add ax, bx
call DumpAX
push ax ; or you can use DX to swap values of ax and bx - or xor op if you wish
push bx
pop ax
pop bx
loop step
exit
你所有的输出都是十六进制的。0xD是13,0x15是21,0x22是34,依此类推。因此,计算似乎是正确的,但是您的DumpRegs过程做了一些奇怪的事情。也向我们展示DumpRegs过程