装配找到两个值的最大值

Ala*_*ste 1 assembly fasm

我试图找到两个值之间的最大值

_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: 标签正在执行。为什么??

zx4*_*485 6

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.