calling assembly functions from c

user173973 :

I'm trying to use a function in assembly, invoked from a C project. This function is supposed to call a libc function let's say printf(), but I keep getting a segmentation fault.

In the .c file I have the declaration of the function let's say

int do_shit_in_asm()

In the .asm file I have

.extern printf
.section .data
         printtext:
              .ascii "test"
.section .text
.global do_shit_in_asm
.type do_shit_in_asm, @function

do_shit_in_asm:
    pushl %ebp
    movl %esp, %ebp
    push printtext
    call printf
    movl %ebp, %esp
    pop %ebp
ret

Any pointers comments would be appreciated.

as func.asm -o func.o

gcc prog.c func.o -o prog
R.. GitHub STOP HELPING ICE :

Change push printtext to push $printtext.

As it is, you're loading a value from the address printtext and pushing that, rather than pushing the address. Thus, you're passing 'test' as a 32-bit number, rather than a pointer, and printf is trying to interpret that as an address and crashing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling C printf from assembly

From Dev

Assembly x86 - Calling C functions

From Dev

Segfault calling c function from assembly

From Dev

Error when calling method in custom assembly from Azure functions

From Dev

Calling a C function from Assembly -- switching calling convention

From Java

Calling C++ functions from Java

From Dev

Calling Cython C functions from Python

From Dev

Calling R functions from C++

From Dev

Calling C++ functions from Python

From Dev

Calling Python functions from C++ with namespace

From Dev

C++ Calling Functions with Parameters from Map

From Dev

Calling NASM float in x86 assembly from C

From Dev

Return a value from assembly function to calling c++ function

From Dev

Calling C# from native within one assembly

From Dev

Segmentation fault error when calling assembly function from C

From Dev

segmentation fault while calling functions in nasm assembly

From Dev

How Calling Functions with lots of args in Assembly Works

From Dev

Issue with calling multiple labels/functions in assembly

From Dev

Calling printf from assembly - not working

From Dev

How to call Assembly Functions from C on x86 architecture?

From Dev

Calling functions from other functions

From Dev

C function calling problems in assembly

From Dev

Calling functions from a Gist

From Dev

Trouble with calling C++ functions from C code

From Dev

Calling c++ functions

From Dev

Calling __global__ CUDA functions from regular C++ code

From Dev

Calling C# functions from PowerShell with params keyword

From Dev

Efficient Solution for Calling C Math Functions from Python

From Dev

Properly calling C++ functions in DLL from Delphi "Access Violation"

Related Related

HotTag

Archive