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

Join 117,542 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,696 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 GCC

 
Reply to this topicStart new topic

> Mixing Assembly with GCC

no2pencil
Group Icon



post 9 Jul, 2008 - 02:10 AM
Post #1


This tutorial will cover how to use a function written in assembler in your C/C++ programs on a Linux/Unix system.

I am using nasm to assemble a simple program that outputs 1 character.
That function will need to be declared global inside of the assembly code:

CODE

section .data
  msg:  db "-"    ; the output charactor
  len:  equ $-msg ; the length of msg

section .text
  global _junk    ; because this will be used with c code,
                  ; we can't redeclare start, so I've just called it Junk
  global _line    ; <-- This is our target function!
  
_junk:
  call _line      ; we never enter here through c anyhow...
  xor ebx, ebx
  mov eax, 1
  
  int 80h
_line:
   mov eax, 4
   mov ebx, 1
   mov ecx, msg   ; Load register ecx with our character
   mov edx, len   ; Load register edx with the length

   int      0x80

   ret            ; return to the c code


Now we'll use nasm to create something that gcc can use later...

nasm -f elf dash o line.o line.asm

It's important to make the output file of type elf. Otherwise gcc will complain that it doesn't know what it is.

cpp

#include <stdio.h>

extern void _line(void); // Load the _line function from assembler output file
void output(void); // This is kind of overkill, but I think it helps make the point clear

int main(void) {
printf("\n"); // Clear the slate with a fresh line for outputing some text
output(); // Display our text function
printf("\n"); // Another fresh line, just for the assembler text
_line(); // Output the character from the assembler code
output(); // Bring it all home, with another example showing our text from the c code
printf("\n"); // One more return, just for good measure

return 0; // And we're all set!
}

void output(void) {
printf("This output is from the c code:\n");
}


Now we just need to compile the c code, along with the output from the assembler code.
gcc dash o main main.c line.o

Finally, we can run the executable created by gcc, & the output should look like the following:

QUOTE

#./main

This output is from the c code:
-
This output is from the c code:


A HUGE thanks to baavgai & perfectly.insane!!!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

born2c0de
Group Icon



post 10 Jul, 2008 - 01:23 AM
Post #2
I tried doing something like this earlier but it never worked.
Now I realized that I didn't pass the the elf parameter.

Thanks!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/7/08 05:26PM

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