LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2010, 07:00 AM   #31
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723

Quote:
Originally Posted by cola View Post
How do you add asm code in a .c file?
I don't.

I write separate asm and C files.

Assemble the asm files.

Compile the C files.

Then link all of the corresponding object files generated.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-13-2010, 07:13 AM   #32
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,036

Rep: Reputation: 65
Quote:
Originally Posted by MTK358 View Post
I don't.

I write separate asm and C files.

Assemble the asm files.

Compile the C files.

Then link all of the corresponding object files generated.
Can you post the command to link all objects with gcc/g++ ?
 
Old 04-13-2010, 07:25 AM   #33
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
$ nasm -f elf *.asm  # Assemble
$ gcc -c *.c         # Compile
$ gcc *.o -o exename # Link
 
Old 04-13-2010, 08:43 AM   #34
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
A quick google gave me the following links:

http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
http://www.ibiblio.org/gferg/ldp/GCC...bly-HOWTO.html

for your viewing pleasure.
 
Old 04-13-2010, 09:13 AM   #35
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
But separate ASM files are more convenient for what I do, and also I use NASM (which uses Intel syntax), while GCC uses GAS (which uses AT&T syntax).
 
Old 04-13-2010, 09:23 AM   #36
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
Quote:
Originally Posted by Super TWiT View Post
I am sure many of you have heard about Linus Torvald's Rant on C++. At first, I thought he was crazy, but a lot of people agree with him that I've seen after looking at various comments on websites that journal tech news. I am looking to learn a language and am currently swayed towards C++, but I DO respect torvalds and it leads me to wonder... Many small programs are written in c. I have heard that c++ really isn't different than c as far as efficiency (I HATE bloated applications). I don't want to start a flamewar, but am looking for a balanced disscussion of c vs c++. What are the main differences?
I was at one point faced with the same question as you.. "What is the difference between C and C++?"

Well, C++ is C with some extra features (C++ is C = C + 1). You can write programs that will compile under g++ that are written exclusively with C libraries.

The difference between C and C++ in this way is that C is -

#include <stdio.h>
#include <string.h>

whereas C++ is

#include <cstdio>
#include <cstring>
#include <c - then - other header file minus .h extension>


Also you have "namespace"s, which allow you to access member functions of a class without the requirement of a scope resolution operator.

So, without "using namespace std;" in your source file, to print something with C++'s cout function, you would have to type -

std::cout << "stuff" << endl;

whereas with "using namespace std;"

you only have to type cout << "stuff" << endl;



On top of that you have inline functions, a cleaner way of passing values by reference, and a system of reading/writing input that is somewhat different than C. By which I mean the "string" class, which I have yet to prefer over the standard null terminated string. (my expertise is very much still a work in progress.)


inline functions are similar to macro definitions in that when you use a macro name as a function call, the code represented by the macro name is written in place of the function call. Inline functions do the same thing assuming that they can perform successfully, otherwise they behave just like a regular function. (the purpose is to avoid the function calls during run time that would be better served as just being code in a program, without the overhead of the function call.)


The "most" defining feature of C++ is the reversal, renaming, and expansion of the structure. By reversal I mean, in

struct demonstration_structure{

int everything_is_public_in_a_structure_except;


private:

int things_that_come_after_private;

};

whereas in a CLASS

class demonstration_class{

int everything_is_private_in_a_class;

public:

int except_everything_that_comes_after_public:
};


I would suggest learning C first, because just about all the "Beginning C++" books I've seen mostly cover C, but without the attention to the detail that a C book would give (for example, the book 'The C Programming Language' contains 90% of the features in "Beginning C++", which is my college book, with better examples and better explanations).

On top of that, C++ is C with some new stuff thrown in. In this respect, C is C++'s "base class", and C++ is the "derivative class".

The base class contains all of C. The derivative class contains everything the base class is + some more.


NOTES on the Linus letter - I imagine he isn't entirely incorrect, though I can consider the C vs C++ debate to be nothing unlike the glock vs 1911 debate in that both platforms work as intended, but some people can't handle one, the other, or both for some reason. From that point, people build emotions around their decision, and all meaningful conversation is lost from that point forward.

Last edited by Dogs; 04-13-2010 at 09:41 AM.
 
Old 04-13-2010, 09:35 AM   #37
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by cola View Post
How do you add asm code in a .c file?
If you had to ask (meaning you didn't already know the basics and where to find the advanced info) you shouldn't try to use the info graemef linked.

When you read through that, you'll see it looks pretty hard. A lot of gnu documentation looks hard for something that is actually easy, so people try the small subset they think they understand and generally get away with it. Embedded asm is at least as hard as that documentation makes it look. Most people who try it, try the small subset they think they understand and get unreliable and confusing results.

I don't agree with MTK358's choice regarding NASM vs. gas, nor 32 bit vs. 64 bit. But I think his choice of separate asm files rather than embedded is the only sane way to learn asm.

Someday, if you are an expert asm programmer, you might want to also learn the embedded asm syntax.

Last edited by johnsfine; 04-13-2010 at 09:36 AM.
 
Old 04-13-2010, 09:52 AM   #38
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
http://uncyclopedia.wikia.com/wiki/C%2B%2B

Quote:
Originally Posted by johnsfine View Post
I don't agree with MTK358's choice regarding NASM vs. gas, nor 32 bit vs. 64 bit. But I think his choice of separate asm files rather than embedded is the only sane way to learn asm.
What do you mean?
 
Old 04-13-2010, 01:26 PM   #39
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I agree with Johnsfine:

1. For many reasons, I'd recommend GAS over NASM. It's a "value judgement", there is no one "right or wrong answer"; but I agree that, "in general", GAS is probably the "better choice".

2. Mtk358 - I don't understand the reference to "32 bit vs 64 bit" either.
But just to be sure we're all on the same page...
My experience is that you can do either 32-bit or 64-bit development with equal ease on any 64-bit Linux. You just can't mix'n'match 32- and 64-bit objects in the same executable

3. I agree that putting *all* of your assembly code in a *separate* file is a Good Idea. The same is true for mixing Assembly with C/C++, or mixing C and C++, or mixing C and FORTRAN: separate source files are just cleaner. Even when you *can* mix'n'match different languages in the same source file ... usually, you probably *shouldn't*

'Hope that helps .. PSM

PS:
I stand by the snarky comment I made about C++ above I agree that in skilled hands, C++ can be an excellent tool. I *don't* agree that C++ is necessarily more efficient than a lot of other languages ... even C++ programs written by good developers. And I think we *all* agree that C++ is complex, and that's it's too easy for a naive developer to make serious coding errors ... and not even realize it.

IMHO...

Last edited by paulsm4; 04-13-2010 at 01:30 PM.
 
Old 04-13-2010, 01:40 PM   #40
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by paulsm4 View Post
For many reasons, I'd recommend GAS over NASM. It's a "value judgement", there is no one "right or wrong answer"; but I agree that, "in general", GAS is probably the "better choice".
I don't know, I am also not quite sure which is better. I just know that GAS looks very cryptic compared to NASM.

It would be nice to have a good comparison somewhere.

At least the instructions are still all the same

Quote:
Originally Posted by paulsm4 View Post
Mtk358 - I don't understand the reference to "32 bit vs 64 bit" either.
But just to be sure we're all on the same page...
My experience is that you can do either 32-bit or 64-bit development with equal ease on any 64-bit Linux. You just can't mix'n'match 32- and 64-bit objects in the same executable
No matter what I tried I could not get 32-bit ASM code to work on my 64-bit desktop.
 
Old 04-13-2010, 01:43 PM   #41
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
...
No matter what I tried I could not get 32-bit ASM code to work on my 64-bit desktop.

You haven't yet tried to understand how/where from the linker pick libraries while linking.
 
Old 04-13-2010, 02:02 PM   #42
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by paulsm4 View Post
I don't understand the reference to "32 bit vs 64 bit" either.
That was regarding the choice to learn X86 assembler instead of X86_64 assembler, because tutorials are available for X86. I think that is like looking for your keys under the lamppost because the light is better there instead of looking for your keys where you dropped them.

Quote:
My experience is that you can do either 32-bit or 64-bit development with equal ease on any 64-bit Linux.
Does that include Arch?

My experience is that you can do either 32-bit or 64-bit development with equal ease on any 64-bit Linux that I have tried so far. That does not include Arch.

Quote:
Originally Posted by Sergei Steshenko View Post
You haven't yet tried to understand how/where from the linker pick libraries while linking.
Look at
http://www.linuxquestions.org/questi...-linux-801349/
We were at:
A) Don't know how to install a 32 bit libgcc.a on 64 bit Arch
B) Don't know how to use 32 bit libgcc_s.so instead of libgcc.a (though I had some idea on that which I don't think MTK358 followed up. Bottom of post 14 in that other thread.)

Last edited by johnsfine; 04-13-2010 at 02:11 PM.
 
Old 04-13-2010, 03:27 PM   #43
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by johnsfine View Post
That was regarding the choice to learn X86 assembler instead of X86_64 assembler, because tutorials are available for X86. I think that is like looking for your keys under the lamppost because the light is better there instead of looking for your keys where you dropped them.
But it says nowhere how to do x86_64 assembler.

It's more like "looking for the keys under the lamppost instead of where you dropped them because it's pitch black away from the lamppost!".

Quote:
Originally Posted by johnsfine View Post
though I had some idea on that which I don't think MTK358 followed up. Bottom of post 14 in that other thread.
You didn't tell me how to obtain the necessary files, preferably in Arch package format. If using a different distro would be much more practical, then maybe I'll be willing to do an Arch/(Some other more ASM programming freindly distro) dual boot.

Last edited by MTK358; 04-13-2010 at 03:30 PM.
 
Old 04-13-2010, 03:36 PM   #44
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
asm (
     ".intel_syntax\n"
     "mov eax,8\n"
     ".att_syntax\n"
    );
 
Old 04-13-2010, 03:37 PM   #45
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So GAS does intel syntax, too?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration