Author Topic: Calling C math library functions from NASM  (Read 2119 times)

Offline markallyn

  • Jr. Member
  • *
  • Posts: 10
Calling C math library functions from NASM
« on: March 03, 2017, 02:39:05 PM »
Hello everyone,

I would like to call the C Math Library functions from NASM, for example, calling the log function from NASM.  I declare log as an extern function, but how do I pass parameters to the log function prior to the call?  And in what register does the result get returned?  The C function signature is log(double) and returns a C double. 

Of course I could resort to the FPU to do the work, but that requires 5 or 6 assembly instructions to compute a natural log whereas going to the C math library may be fewer lines (if I could figure out how to do it). 

I've looked all over the internet for answers to this, but to no avail.

Thanks,
Mark Allyn

Offline jordi

  • Jr. Member
  • *
  • Posts: 6
Re: Calling C math library functions from NASM
« Reply #1 on: March 04, 2017, 02:04:02 PM »
Hi Mark,

At least in Linux using 64 bit NASM you can call the log function from C
math lib passing the argument in RDI and getting the result in RAX:

; double asm64_C_log(double)
;
; calculate the (natural) logarithm of op
;
; in:
;
; rdi: op
;
; returns:
;
; rax: log(op)

        global asm64_C_log
        extern log

asm64_C_log:

        call    log
        ret

If you call this function from C you'll get the same result as calling log
in C.

Jordi

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: Calling C math library functions from NASM
« Reply #2 on: March 04, 2017, 02:50:40 PM »
OP should be more specific on things like 1) Platform (win or linux) 2) architecture (32 or 64-bit) in order to get instant help from members.

Assuming it for Win64, calling LOG from C should be something like this;

Code: [Select]
global main
extern printf
extern log

section .data
s db '%f',0
x dq 2.7

section .code
main:
        sub     rsp,0x28    ;align stack and shadow space
        movq    xmm0,[x]    ;argument in XMM0
        call    log         ;return to XMM0
        movq    rdx,xmm0 
        mov     rcx,s
        mov     rax,0
        call    printf
        add     rsp,0x28
        ret

Also applies to calling any other routines from C math.

Offline markallyn

  • Jr. Member
  • *
  • Posts: 10
Re: Calling C math library functions from NASM
« Reply #3 on: March 04, 2017, 05:26:13 PM »
Hello Stressful,

You're right about my not being sufficiently specific.  As a matter of fact, I'm running 32 bit linux and compiling therefore an elf32 file, not win64.  Apologies are in order.

As a matter of fact, I later worked out how to do what I wanted to achieve.  Here is the solution for the call to the libm log function:

Code: [Select]
;math.s.  A NASM program that calls several functions in the C math library <math.h>

SEGMENT .data
input dq 25.0
SEGMENT .bss
double  resq 1
SEGMENT .text
global _start
extern log
extern sqrt
_start:

nop
finit
fld qword [input]
fstp qword [double]
lea esi, [double]
sub esp, 8
lea edi, [esp]
mov ecx, 8
rep movsb
call log
fstp qword [esp]
add esp, 4
mov eax, 1
mov ebx, 0
int 0x80

There are probably a half dozen better solutions, but this has the virtue of my being able to understand what's happening. 

Regards,
Mark Allyn