这个版本的斐波那契代码在汇编中的概念
concept beind this version of fibonacci code in assembly
这是我的教授在 class 中使用的汇编中斐波那契代码的替代版本,它使用了一个新公式。
f(n-2)/2 + f(n-1)*2
so the sequence is 1, 2, 4, 9, 20, 44, 98, 218
我只是有几件事让我感到困惑。
jb和je是什么意思?
是一和二两个局部变量吗?他们在输入中采用了等式的哪些不同部分?
[ebp-4] 和 [ebp-8],是那些注册表?他们为什么被选中。
请帮助我,我真的迷失了这段代码背后的概念!
这是代码
TITLE Mobonacci
.586
.MODEL FLAT, C
.code
Mobonacci PROC
; will move n into EAX;
push ebp
mov ebp, esp
mov eax, [ebp+8]
; see if EAX is less than or equal to 1;
cmp eax, 2
jb one
je two
ja L1
; call the Mobonacci;
L1:
sub esp, 8
; Mobo(n-1) * 2;
sub eax, 1
push eax
call Mobonacci;
mov ebx, eax
pop eax
; times ebx by 2;
shl ebx, 1
mov [ebp-4], ebx
; Mobo(n-2)/2;
sub eax, 1
push eax
call Mobonacci
mov ebx, eax
pop eax
; Divide ebx by 2;
shr ebx, 1
mov [ebp-8], ebx
; sum of two numbers;
mov eax, 0
add eax, [ebp-4]
add eax, [ebp-8]
; Clean stack;
mov esp, ebp
jmp ending
; Return 1;
one:
mov eax, 1
jmp ending
; Return 2;
two:
mov eax, 2
jmp ending
; Return the value in eax;
ending:
pop ebp
ret
Mobonacci ENDP
END
1) What do jb
and je
mean?
它们是 x86 指令集中的跳转。 jb
是向下跳转,je
是相等跳转。它们根据影响 x86 标志寄存器的最近操作确定 "below" 或 "equal" 是否为真。或者换句话说,如果例如你做了 cmp eax,ebx
(比较 eax
和 ebx
寄存器),那么紧接着 jb foo
将跳转到标签 foo
如果 eax
值 低于 ebx
值。如果值相等,je foo
将跳转到标签 foo
。 Google "x86 jump instructions" 了解更多详情。
2) Are one
and two
two local variables? What different part of
equation do they take in the inputs in?
不是变量。它们是指示可以从代码中的其他地方跳转到的地方的标签。如果将此知识与问题 #1 的答案结合起来,代码会将 eax
与 2
(cmp eax,2
) 进行比较,如果 jb one
跳转到标签 one
eax
小于 2
(它的值可能是 1),如果 eax
的值等于 2
,则 je two
跳转到标签 two
].最后,如果 eax
的值 高于 值 2,ja L1
跳转到标签 L1
。请注意,由于下一条指令的标签为 L1
,这没有任何功能性影响。
3) [ebp-4]
and [ebp-8]
, are those registry? Why are they chosen?
ebp
是x86架构中的Base Pointer register。它通常包含一个地址。引用 [ebp-4]
和 [ebp-8]
指的是内存中位置 ebp-4
和 ebp-8
的值(该地址的值减 4 和减 8)。 Google "x86 stack frame" 了解为什么这样做的详细信息。
ebp 的使用是可选的。该代码可以使用
Mobonacci PROC
sub esp,8 ;allocate space for 2 integers
mov eax,[esp+12] ;get parameter
; ...
mov [esp+4],ebx ;instead of [ebp-4]
; ...
mov [esp+0],ebx ;instead of [ebp-8]
; ...
ending:
add esp,8 ;restore esp
ret
Mobonacci ENDP
这是我的教授在 class 中使用的汇编中斐波那契代码的替代版本,它使用了一个新公式。
f(n-2)/2 + f(n-1)*2
so the sequence is 1, 2, 4, 9, 20, 44, 98, 218
我只是有几件事让我感到困惑。
jb和je是什么意思?
是一和二两个局部变量吗?他们在输入中采用了等式的哪些不同部分?
[ebp-4] 和 [ebp-8],是那些注册表?他们为什么被选中。
请帮助我,我真的迷失了这段代码背后的概念!
这是代码
TITLE Mobonacci
.586
.MODEL FLAT, C
.code
Mobonacci PROC
; will move n into EAX;
push ebp
mov ebp, esp
mov eax, [ebp+8]
; see if EAX is less than or equal to 1;
cmp eax, 2
jb one
je two
ja L1
; call the Mobonacci;
L1:
sub esp, 8
; Mobo(n-1) * 2;
sub eax, 1
push eax
call Mobonacci;
mov ebx, eax
pop eax
; times ebx by 2;
shl ebx, 1
mov [ebp-4], ebx
; Mobo(n-2)/2;
sub eax, 1
push eax
call Mobonacci
mov ebx, eax
pop eax
; Divide ebx by 2;
shr ebx, 1
mov [ebp-8], ebx
; sum of two numbers;
mov eax, 0
add eax, [ebp-4]
add eax, [ebp-8]
; Clean stack;
mov esp, ebp
jmp ending
; Return 1;
one:
mov eax, 1
jmp ending
; Return 2;
two:
mov eax, 2
jmp ending
; Return the value in eax;
ending:
pop ebp
ret
Mobonacci ENDP
END
1) What do
jb
andje
mean?
它们是 x86 指令集中的跳转。 jb
是向下跳转,je
是相等跳转。它们根据影响 x86 标志寄存器的最近操作确定 "below" 或 "equal" 是否为真。或者换句话说,如果例如你做了 cmp eax,ebx
(比较 eax
和 ebx
寄存器),那么紧接着 jb foo
将跳转到标签 foo
如果 eax
值 低于 ebx
值。如果值相等,je foo
将跳转到标签 foo
。 Google "x86 jump instructions" 了解更多详情。
2) Are
one
andtwo
two local variables? What different part of equation do they take in the inputs in?
不是变量。它们是指示可以从代码中的其他地方跳转到的地方的标签。如果将此知识与问题 #1 的答案结合起来,代码会将 eax
与 2
(cmp eax,2
) 进行比较,如果 jb one
跳转到标签 one
eax
小于 2
(它的值可能是 1),如果 eax
的值等于 2
,则 je two
跳转到标签 two
].最后,如果 eax
的值 高于 值 2,ja L1
跳转到标签 L1
。请注意,由于下一条指令的标签为 L1
,这没有任何功能性影响。
3)
[ebp-4]
and[ebp-8]
, are those registry? Why are they chosen?
ebp
是x86架构中的Base Pointer register。它通常包含一个地址。引用 [ebp-4]
和 [ebp-8]
指的是内存中位置 ebp-4
和 ebp-8
的值(该地址的值减 4 和减 8)。 Google "x86 stack frame" 了解为什么这样做的详细信息。
ebp 的使用是可选的。该代码可以使用
Mobonacci PROC
sub esp,8 ;allocate space for 2 integers
mov eax,[esp+12] ;get parameter
; ...
mov [esp+4],ebx ;instead of [ebp-4]
; ...
mov [esp+0],ebx ;instead of [ebp-8]
; ...
ending:
add esp,8 ;restore esp
ret
Mobonacci ENDP