Ad
Fundamentals
Mathematics
Code
Diff
  • section .text
    global is_square      ; C-style declaration: ``_Bool is_square();``
    
    is_square:
      CVTSI2SS xmm0, rdi  ; sqrt takes a ``double`` as its argument, so converting rdi to ``double`` first
      SQRTSS xmm0, xmm0   ; now, XMM0 = sqrt(XMM0
      CVTSS2SI rax, xmm0  ; RAX = (int) XMM0
      MUL rax             ; RAX *= RAX
      XOR rax, rdi        ; if this XOR evaluates to 0, then RAX == RDI
      SETZ al             ; in which case you set the lower 8 bits of AX to 00000001, i.e. "true"
      RET                 ; return to the caller
    • section .text
    • global is_square ; C-style declaration: ``_Bool is_square();``
    • extern sqrt ; C-style declaration: ``double sqrt(double);``
    • is_square:
    • cvtsi2sd xmm0, rdi ; sqrt takes a ``double`` as its argument, so converting rdi to ``double`` first
    • call sqrt ; now, XMM0 = sqrt(XMM0)
    • cvtsd2si rax, xmm0 ; RAX = (int) XMM0
    • mul rax ; RAX *= RAX
    • xor rax, rdi ; if this XOR evaluates to 0, then RAX == RDI
    • setz al ; in which case you set the lower 8 bits of AX to 00000001, i.e. "true"
    • ret ; return to the caller
    • CVTSI2SS xmm0, rdi ; sqrt takes a ``double`` as its argument, so converting rdi to ``double`` first
    • SQRTSS xmm0, xmm0 ; now, XMM0 = sqrt(XMM0
    • CVTSS2SI rax, xmm0 ; RAX = (int) XMM0
    • MUL rax ; RAX *= RAX
    • XOR rax, rdi ; if this XOR evaluates to 0, then RAX == RDI
    • SETZ al ; in which case you set the lower 8 bits of AX to 00000001, i.e. "true"
    • RET ; return to the caller