程序集找到两个值的最大值
Assembly find max of two value
我试图找到两个值之间的最大值
_FindMax:
push ebp
mov ebp, esp
mov eax, dword [ebp+12] ; get fist argument
mov ebx, dword [ebp+8] ; get second argument
cmp eax, ebx
jl LESS ; if eax less to LESS
LESS:
mov eax, ebx ; ebx is greate and return it
mov esp, ebp
pop ebp
ret
但问题是 LESS: 标签一直在执行。例如,如果参数相等,则 LESS: 标签正在执行。为什么??
无论分支是否被采用,LESS 中的代码总是被执行。您需要跳过不想执行的代码:
_FindMax:
push ebp
mov ebp, esp
mov eax, dword [ebp+12] ; get fist argument
mov ebx, dword [ebp+8] ; get second argument
; invert the condition, and jump over the code to skip
cmp eax, ebx
jge SKIP ; jmp and return, if aex is the larger one
mov eax, ebx ; ax is NOT larger, so return ebx
SKIP:
mov esp, ebp
pop ebp
ret
一个真正有效的方法是(假设您至少有一个 P6 系列处理器):
_FindMax:
mov eax, dword [esp+8] /* get first argument */
mov ebx, dword [esp+4] /* get second argument */
cmp eax, ebx /* compare EAX to EBX */
cmovl eax, ebx /* MOV EBX to EAX if EBX > EAX */
ret
此代码省略堆栈帧 (EBP) 并使用内联 MOV
操作进行比较。尽管如此,return 值仍在 EAX
.
我试图找到两个值之间的最大值
_FindMax:
push ebp
mov ebp, esp
mov eax, dword [ebp+12] ; get fist argument
mov ebx, dword [ebp+8] ; get second argument
cmp eax, ebx
jl LESS ; if eax less to LESS
LESS:
mov eax, ebx ; ebx is greate and return it
mov esp, ebp
pop ebp
ret
但问题是 LESS: 标签一直在执行。例如,如果参数相等,则 LESS: 标签正在执行。为什么??
无论分支是否被采用,LESS 中的代码总是被执行。您需要跳过不想执行的代码:
_FindMax:
push ebp
mov ebp, esp
mov eax, dword [ebp+12] ; get fist argument
mov ebx, dword [ebp+8] ; get second argument
; invert the condition, and jump over the code to skip
cmp eax, ebx
jge SKIP ; jmp and return, if aex is the larger one
mov eax, ebx ; ax is NOT larger, so return ebx
SKIP:
mov esp, ebp
pop ebp
ret
一个真正有效的方法是(假设您至少有一个 P6 系列处理器):
_FindMax:
mov eax, dword [esp+8] /* get first argument */
mov ebx, dword [esp+4] /* get second argument */
cmp eax, ebx /* compare EAX to EBX */
cmovl eax, ebx /* MOV EBX to EAX if EBX > EAX */
ret
此代码省略堆栈帧 (EBP) 并使用内联 MOV
操作进行比较。尽管如此,return 值仍在 EAX
.