Author Topic: [SOLVED]newbie question about calling c function within assembly  (Read 10465 times)

Offline yvz

  • Jr. Member
  • *
  • Posts: 5
Before starting, I want to denote that it's my first post here so if I made any mistake that violates forum rules, apologizes.

Well, I need to handle a very simple task yet my lack of knowledge about assembly programming prevents me to achieve. But I need to solve and understand this to continue to learning so here it is.

I got a c code without main, but only a function definition.
Code: [Select]
#include <stdio.h>

extern void hello_world();

void hello_world(){
printf("Hello World!");
}

And I need to call it within assembly. There'r many examples around web including here like this. But I wont take any arguments I just need to call it and see it working. I coded this which is not working
Code: [Select]
section .data

section .text
global _start
global main
extern hello_world

_start :
call main

mov eax,1
int 80h


main :
;push ebp
;mov ebp, esp
call hello_world
;pop ebp

ret

My terminal commands are
Code: [Select]
gcc -c callee.c
nasm -f elf caller.asm
ld -s -o call callee.o caller.o -lc -I/lib/ld-linux.so.2
./call

All works without error but nothing happens. I'm trying to understand concept so I would come up with more questions :) Thanks in advance.
« Last Edit: January 06, 2014, 10:08:17 PM by yvz »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: newbie question about calling c function within assembly
« Reply #1 on: January 06, 2014, 09:45:47 PM »
Hi yvz,

Only thing I can think of:
Code: [Select]
printf("Hello World!\n");

There's a "dirty little secret" to printf - it doesn't print anything until the buffer is flushed. The linefeed will flush the buffer. Exiting the program ought to flush the buffer also, so this probably won't work. When you do get it to print, it'll look better with the linefeed anyway. :)

I can't see anything wrong with what you've done, and unfortunately I'm still not in a position to test it. Maybe I'll fix that soon... I keep saying...

Best,
Frank


Offline yvz

  • Jr. Member
  • *
  • Posts: 5
Re: newbie question about calling c function within assembly
« Reply #2 on: January 06, 2014, 10:07:43 PM »
Wow, that's it. It works now. Thanks Frank. This was ridiculously simple and easy yet took almost a week for me jumping around forums, articles and examples.