; callf1_64.asm a basic structure for a subroutine to be called from "C" ; ; Parameters: double *L ; Result: L[0]=L[0]+3.0 L[1]=L[1]+4.0 global callf1_64 ; linker must know name of subroutine extern printf ; the C function, to be called for demo SECTION .data ; Data section, initialized variables fmt1: db "rdi=%ld, L[0]=%e", 10, 0 ; The printf format, "\n",'0' fmt2: db "rdi=%ld, L[1]=%e", 10, 0 ; The printf format, "\n",'0' a3: dq 3.0 ; 64-bit variable a initialized to 3.0 a4: dq 4.0 ; 64-bit variable b initializes to 4.0 SECTION .bss a: resq 1 ; temp for saving address SECTION .text ; Code section. callf1_64: ; name must appear as a nasm label push rbp ; save rbp ; mov rbp, rsp ; rbp is callers stack push rdx ; save registers, for debug print push rdi push rsi mov rax,rdi ; first, only, in parameter mov [a],rdi ; save for later use ; mov rdi,fmt1 ; format for printf debug, demo ; mov rsi,rax ; first parameter for printf ; movq xmm0, qword [rax] ; second parameter for printf ; mov rax,1 ; one xmm registers ; call printf ; Call C function ; mov rax,[a] ; first, only, in parameter, demo ; mov rdi,fmt2 ; format for printf ; mov rsi,rax ; first parameter for printf ; movq xmm0, qword [rax+8] ; second parameter for printf ; mov rax,1 ; one xmm registers ; call printf ; Call C function mov rax,[a] ; add 3.0 to L[0] fld qword [rax] ; load L[0] (pushed on flt pt stack, st0) fadd qword [a3] ; floating add 3.0 (to st0) fstp qword [rax] ; store into L[0] (pop flt pt stack) fld qword [rax+8] ; load L[1] (pushed on flt pt stack, st0) fadd qword [a4] ; floating add 4.0 (to st0) fstp qword [rax+8] ; store into L[1] (pop flt pt stack) pop rsi ; restore registers pop rdi ; in reverse order pop rdx ; mov rsp,rbp pop rbp ; restore callers stack frame ret ; return