Welcome to Dream.In.Code
Getting Help is Easy!

Join 117,610 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,006 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Mixing Assembly with my GCC

 
Reply to this topicStart new topic

Mixing Assembly with my GCC, Linux : gcc -v 4.1.2

no2pencil
post 4 Jul, 2008 - 01:34 AM
Post #1


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 5,435



Thanked 35 times

Dream Kudos: 2350

Expert In: Goofing Off

My Contributions


I'm using nasm to assemble a simple program that outputs a character, & that function is declared global inside of the assembly code:

CODE

GLOBAL _line

SECTION .data
msg:  db "-"
len:    equ $-msg

SECTION .text
_line:
   mov   edx, len
   mov   ecx, msg
   mov   ebx, 1
   mov   eax, 4
   int      0x80

   mov   ebx, 0
   mov   eax, 1
   int      0x80


It assembles without error (as it's fairly simple) & runs as designed.

So in my c code, I declare the function external that will call from the linked assembly object file :

CODE

#include <stdio.h>

extern void _line(void);

int main(void) {
  printf("The following should be the assembly code:\n");
  _line();
  return 0;
}


When I compile it, I get no errors using gcc dash o main main.c line.o

However when I run the file, the only thing that happens is the assembly code. What am I doing wrong?!

My guess is that somehow the assembly code has the main therefor once it's processing, it's done. So the main from the c code is being ignored.
User is online!Profile CardPM

Go to the top of the page


baavgai
post 4 Jul, 2008 - 03:05 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,763



Thanked 70 times

Dream Kudos: 400

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Your missing the "ret" for return. You know where to return to by looking at the stack. A C call will push the return address on the stack along with any parameters passed. In subroutines in ASM, you usually do a lot of stack play at the beginning and end, preserving and restoring values related to parameters.

Found this page that seems to cover it pretty well: http://www.digitalmars.com/ctg/ctgAsm.html
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 4 Jul, 2008 - 10:21 AM
Post #3


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 550



Thanked 43 times

Dream Kudos: 25
My Contributions


It could be to do with the linkage. If you link the program using gcc/g++, then it should be difficult to do this incorrectly. If you were invoking ld directly, it might be a different story. However, ld should give you a warning if it cannot find the symbol "start". In that case, it will automatically choose a start symbol, which happens to be the first code symbol it finds in the program.

Also, function calling is built into x86. But you do at least need a ret instruction (pops return address off the stack and jumps to that address). If you don't actually pass any parameters, then there is little need to manipulate the stack. Call automatically puts the return address on the stack.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 11:41PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month