T
T
trynottofil2021-05-16 20:03:21
C++ / C#
trynottofil, 2021-05-16 20:03:21

How to call assembler functions from a C++ program?

I tried to figure it out myself, but time is very short. You need to write a C++ program that calls 3 functions written in GCC assembler:
a) calculates the terms of the series a_i = (1+i)/(1+i^3) from i=0 to i=N-1 and writes them into an array A of N elements;
b) calculates the partial sum S of this row up to N-1 (as close as possible to the sum on ℝ) by A;
c) calculates ∏(a_i-S/N)^2 for the elements of array A.

Here is my code:

#include <iostream>
extern "C" void number1();
__asm(
    "number1():"
        "push    %rbp"
        "mov     %rbp, %rsp"
        "sub     %rsp, 232"
        "mov     DWORD PTR [%rbp-4], 0"
        "mov     DWORD PTR [%rbp-4], 0"
        "jmp     .L2"
".L3:"
        "mov     %ecx, DWORD PTR [%rbp-4]"
        "mov     %edx, DWORD PTR [%rbp-4]"
        "inc     %edx"
        "mov     %eax, DWORD PTR [%rbp-4]"
        "imul    %eax, DWORD PTR [%rbp-4]"
        "imul    %eax, DWORD PTR [%rbp-4]"
        "inc     %eax"
        "mov     DWORD PTR [%rbp-340], %eax"
        "mov     %eax, %edx"
        "cdq"
        "idiv    DWORD PTR [%rbp-340]"
        "mov     DWORD PTR [%rbp-340], %eax"
        "mov     %eax, DWORD PTR [%rbp-340]"
        "cvtsi2sd        %xmm0, %eax"
        "movsx   %rax, %ecx"
        "movsd   QWORD PTR [%rbp-336+%rax*8], %xmm0"
        "inc     DWORD PTR [%rbp-4]"
".L2:"
        "cmp     DWORD PTR [%rbp-4], 38"
        "jle     .L3"
        "leave"
        "ret"
);
int main()
{
number1();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-05-17
@trynottofil

How to call assembler functions from a C++ program?

extern "C" void number1();
…
int main()
{
number1();
}

Exactly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question