1;; Copyright 2019 The Android Open Source Project 2;; 3;; This software is licensed under the terms of the GNU General Public 4;; License version 2, as published by the Free Software Foundation, and 5;; may be copied, distributed, and modified under those terms. 6;; 7;; This program is distributed in the hope that it will be useful, 8;; but WITHOUT ANY WARRANTY; without even the implied warranty of 9;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10;; GNU General Public License for more details. 11 12;; Simple asm function that calls back a c function. 13;; Note, that the different calling conventions on x86_64 for Win vs. Darwin/Lin 14;; See https://en.wikipedia.org/wiki/X86_calling_conventions for details 15%ifidn __OUTPUT_FORMAT__, win64 16 ; Windows default calling convention uses rcx, rdx for first 2 vars. 17 %define MOV_REG_PARM1 mov rcx 18 %define MOV_REG_PARM2 mov rdx 19%else 20 ; darwin/linux use rdi & rsi 21 %define MOV_REG_PARM1 mov rdi 22 %define MOV_REG_PARM2 mov rsi 23%endif 24 25; Platforms mangle names slightly differently 26%ifidn __OUTPUT_FORMAT__, macho64 27 ; Darwin mangles with a _ 28 %define HELLO_FUNC _hello 29 %define SAY_HELLO_FUNC _say_hello 30%else 31 ; windows & linux do not mangle. 32 %define HELLO_FUNC hello 33 %define SAY_HELLO_FUNC say_hello 34%endif 35 36; Declare needed C functions, the linker will resolve these. 37 extern HELLO_FUNC ; the C hello function we are calling. 38 39 section .text ; Code section. 40 global SAY_HELLO_FUNC ; Export out function, linker can now link against it. 41 42SAY_HELLO_FUNC: ; Function entry 43 push rbp ; Push stack 44 45 MOV_REG_PARM1, 127 ; Load our 2 parameters in registers. 46 MOV_REG_PARM2, qword msg 47 call HELLO_FUNC ; Call mangled C function 48 49 pop rbp ; restore stack 50 mov rax, 255 ; return value 51 ret ; return 52 53 54 section .data ; Data section, initialized variables 55msg: db "Hello world", 0 ; C string needs 0 56