Ad

added default case (a > b) to have fewer operations

Code
Diff
  • section .text
    global max ; declare max function
    max:                ; int max(int a, int b)
      mov rax, rdi      ; let the default case be a > b
      cmp rax, rsi      ; compare a ?= b
      jge _end          ; jump to end if a >= b
      mov rax, rsi      ; assigne rax to b if a < b
      _end:
      ret
    • section .text
    • global max ; declare max function
    • max: ; int max(int a, int b)
    • cmp rdi, rsi ; compare a ?= b
    • jle _lower ; jump to lower if a <= b
    • jmp _greater ; jump to greater if a > b
    • _lower:
    • mov rax, rsi ; assgine rax to b
    • jmp _end ; end program
    • _greater:
    • mov rax, rdi ; assgine rax to a
    • max: ; int max(int a, int b)
    • mov rax, rdi ; let the default case be a > b
    • cmp rax, rsi ; compare a ?= b
    • jge _end ; jump to end if a >= b
    • mov rax, rsi ; assigne rax to b if a < b
    • _end:
    • ret