Skip to content

Instantly share code, notes, and snippets.

@cwgreene
Created November 5, 2017 06:06
Show Gist options
  • Save cwgreene/75311bea74848fa2ee1796bcf68edad3 to your computer and use it in GitHub Desktop.
Save cwgreene/75311bea74848fa2ee1796bcf68edad3 to your computer and use it in GitHub Desktop.

Ocaml code:

type bob =
    | Add of int*int
    | Mul of int*int;;
let mulpy v = match v with
    | Add (a, b) -> a + b
    | Mul (a, b) -> a * b;;
print_string (string_of_int (mulpy (Mul (3, 2))));;

Disassembly from r2

/ (fcn) sym.camlTest__mulpy_1011 44
|   sym.camlTest__mulpy_1011 ();
|              ; CALL XREF from 0x0040234c (sym.camlTest__entry)
|           0x00402300      480fb658f8     movzx rbx, byte [rax - 8]
|           0x00402305      4885db         test rbx, rbx
|       ,=< 0x00402308      7416           je 0x402320
|       |   0x0040230a      488b5808       mov rbx, qword [rax + 8]    ; [0x8:8]=-1 ; 8
|       |   0x0040230e      48d1fb         sar rbx, 1
|       |   0x00402311      488b00         mov rax, qword [rax]
|       |   0x00402314      48ffc8         dec rax
|       |   0x00402317      480fafc3       imul rax, rbx
|       |   0x0040231b      48ffc0         inc rax
|       |   0x0040231e      c3             ret
        |   0x0040231f      90             nop
|       `-> 0x00402320      488b5808       mov rbx, qword [rax + 8]    ; [0x8:8]=-1 ; 8
|           0x00402324      488b00         mov rax, qword [rax]
|           0x00402327      488d4418ff     lea rax, [rax + rbx - 1]
\           0x0040232c      c3             ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment