我试图找到两个值之间的最大值
_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
Run Code Online (Sandbox Code Playgroud)
但问题是 LESS: 标签总是在执行。例如,如果参数相等,则 LESS: 标签正在执行。为什么??
A really efficient way to achieve this would be (assuming that you have at least a P6 family processor):
_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
Run Code Online (Sandbox Code Playgroud)
This code omits the stack frame (EBP) and uses an inline MOV operation to do the comparison. Nevertheless, the return value is still in EAX.
| 归档时间: |
|
| 查看次数: |
4125 次 |
| 最近记录: |