LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 11-27-2004, 09:45 PM   #1
XsuX
Member
 
Registered: Oct 2004
Location: US
Distribution: Fedora Core 1
Posts: 43

Rep: Reputation: 15
x86 assembly programming in Linux


Anybody have a website or book that they can reccomend to help learn about assembly programming on an x86 in Linux? I've searched the web for hours, Googled it re-Googled it, and re-re-Googled it, but found very little except this Which is cool for learning how to write a single program and learning a little conceptual stuff, but not much help for anything bigger. Any help would be greatly appreciated.
 
Old 11-27-2004, 10:18 PM   #2
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 31
I'm currently working through Dr. Paul Carter's book: PC Assembly Language. It's usable on both Linux and Windows and uses Intel syntax rather than AT&T syntax.

PDF Available here: http://drpaulcarter.com/pcasm/

Good luck!
 
Old 11-27-2004, 10:54 PM   #3
XsuX
Member
 
Registered: Oct 2004
Location: US
Distribution: Fedora Core 1
Posts: 43

Original Poster
Rep: Reputation: 15
Thanks so much!
 
Old 11-28-2004, 09:30 AM   #4
rblampain
Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 7
Posts: 835

Rep: Reputation: 35
Learning assembly language under linux is not easy at all. There is no documentation to be found on the net to start you up. I've been experiencing the problem for years. The best I can suggest is to read an online book that is very complete:
if you Google "art of assembly" you'll get it listed first, it's by Randall Hyde and you'll find the same material referenced many times. The trouble is to learn something you have to go back to his DOS version which is for 8 and 16 bits registers but quite helpful and everything you'll learn will apply to Linux except the system calls and a few other minor details.

There used to be a few sites you could visit for examples of code but they're not there anymore.

This DOS version is quite old be still valid so don't be deterred. Next is how you want to use assembly, you've got 2 options:
1) writing each line of code and making your own system calls (which is what I do)
2) using assembly as a high level language, you learn about libraries and special commands which you type and this high level assembly will insert the necessary code for you and call routines from the libraries.

Personnally I hate option 2, if you want to make a career of it I suppose it's OK otherwise I reckon you can forget about it.
Option 2 is the way Randall Hyde has gone with what he calls if my recollection is correct HLA (high level assembly)

There is a few assemblers to choose from but I use NASM (network assembler) which is initially a Linux assembler but is now also available for windows. It also has an Intel syntax and is for 32 bits registers and newer processors.
So after you learn the basics from any online tutorial you find, NASM will put you up to date. (NASM is part of most distros so you probably have it already but not installed by default).

Most of the short info available on the net tend to exist to replace short snippets of C code to assembly or using assembly and C libraries but there is occasionally one that will teach you something.

I learned assembly under dos with A86/A386 Macro assembler D86/D386 Debugger by Eric Isaacson who was part
of the team that developed the first Intel assembler. The manual coming with the package is terse but the debugger is a beauty giving you among other things the option to proceed with your program one line at the time and seeing the result in registers and memory as you go plus other things. The cost used to be about US$80.

For the system calls go to
http://www.lxhp.in-berlin.de/lhpsysc0.html
the doc is in English

At one stage, I thought I had to stop using assembly and I bought myself a book about C, however, after I typed in
my first program in C and found out the size of the resulting program I put it back on the shelf and won't probably use it at all. I found it easier to learn assembly than to learn C.

Keep this post alive if you have more questions (or discoveries), I'll help if I can.
 
Old 11-28-2004, 04:05 PM   #5
angustia
Member
 
Registered: Apr 2003
Location: chile
Posts: 58

Rep: Reputation: 15
http://www.csee.umbc.edu/help/nasm/sample.shtml

to write in assembly and call printf from inside
 
Old 11-29-2004, 06:23 AM   #6
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 274

Rep: Reputation: 30
Quote:
Originally posted by rblampain

At one stage, I thought I had to stop using assembly and I bought myself a book about C, however, after I typed in
my first program in C and found out the size of the resulting program I put it back on the shelf and won't probably use it at all. I found it easier to learn assembly than to learn C.

Keep this post alive if you have more questions (or discoveries), I'll help if I can.
If the mentioned program was "Hello world", the binary was probably
around 70kb long, compared to 2-3kb in pure machine code. But
what is 70kb? Besides, it's not that any C program is around 20 times
bigger than the same functionality written in machine code, it's just
an overhead (provided you don't link further libs).
I personally have been coding pure asm for years, back in the good
old 68k days, and now I'd always prefer C/as (inline assembler) over
pure asm. You'll reach your goal many times faster, your code is
much more readable, and I personally can't think of any task where
the (minimal) speed advantage over C/bottlenecks in asm is worth the extra work (and headache) of pure asm

P/S drawback of as is that it is not using Intel notation, but AT/T.
(but that can be learned)
 
Old 11-29-2004, 11:05 PM   #7
rblampain
Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 7
Posts: 835

Rep: Reputation: 35
To marius 2: a few corrections
1) When you have a 2 kb assembly program you've got a substantial program and my experience is of getting a 20kb
program in C for a few hundreds (not thousands) bytes of code in assembly.
2) "Hello world" in assembly shouldn't take more than a hundred bytes of assembly language, so we may not be talking about the same thing when you refer to "pure" assembly.
3) I am not talking about the same AS you're talking about and this is why I specified I was talking about the one from Eric Isaacson which IS INTEL syntax. I should know, I've been using it for over 15 years.

As for the rest, it's philosophical. The point here is XsuX wants to learn assembly.
 
Old 11-30-2004, 05:37 AM   #8
DavidW567
LQ Newbie
 
Registered: Nov 2004
Posts: 17

Rep: Reputation: 0
Following on from the assember thread.
I am converting some Windows C code to Linux. Some of the code is in assembler. With Windows Visual C it is within
__asm {
mov ..

etc.
}
and is assembled automatically within the C code.
Can this be done with Linux or do I have to assemble the code separately.

David
 
Old 11-30-2004, 06:29 AM   #9
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 274

Rep: Reputation: 30
@David: You can use inline assembly with gcc (as). Unfortunately as uses AT&T syntax
instead of Intel (it's said that newer versions also support Intel syntax, but I never got
this to work), which is somehow different (operands are source->destination, not
destination<-source, register naming is different, which is sometimes confusing, but can be
learned). Looks like this:


void C_3DMath_Primitives::Add(Vector3Df *D,Vector3Df *A,Vector3Df *B){
#ifndef MATH_USE_ASM
D->xf=A->xf+B->xf;
D->yf=A->yf+B->yf;
D->zf=A->zf+B->zf;
#endif
#ifdef MATH_USE_ASM
#ifdef _WIN32

_asm{ mov ESI,A
fld DWORD PTR 8[ESI]
fld DWORD PTR 4[ESI]
fld DWORD PTR [ESI]
mov ESI,B
mov EDI,D
fld DWORD PTR 8[ESI]
fld DWORD PTR 4[ESI]
fld DWORD PTR [ESI]
faddp st(3),st(0)
faddp st(3),st(0)
faddp st(3),st(0)
fstp DWORD PTR [EDI]
fstp DWORD PTR 4[EDI]
fstp DWORD PTR 8[EDI]
}

#endif
#ifdef __linux__
__asm( "movl %0,%%esi;"
"fld 8(%%esi);"
"fld 4(%%esi);"
"fld (%%esi);"
"movl %1,%%esi;"
"movl %2,%%edi;"

"fld 8(%%esi);"
"fld 4(%%esi);"
"fld (%%esi);"

"faddp %%st(0),%%st(3);"
"faddp %%st(0),%%st(3);"
"faddp %%st(0),%%st(3);"

"fstp (%%edi);"
"fstp 4(%%edi);"
"fstp 8(%%edi);"

:"=m"(A),"=m"(B),"=m"(D)
:"0"(A),"1"(B),"2"(D)
:"%edi","%esi"
);

#endif
#endif
}
 
Old 12-01-2004, 09:45 AM   #10
DavidW567
LQ Newbie
 
Registered: Nov 2004
Posts: 17

Rep: Reputation: 0
Thanks,
have you any info on how to complile intel code

David
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
assembly programming Ephracis Programming 5 05-08-2005 02:04 AM
How to start programming assembly in Linux? lowpro2k3 Programming 3 04-05-2005 12:10 AM
x86 Assembly - segmentation fault? jrtayloriv Programming 1 07-05-2004 12:52 AM
assembly programming in linux chriscoelphoto Linux - Newbie 2 05-02-2004 05:21 PM
x86 assembly: error message mandrake_linux Programming 1 06-12-2001 09:00 AM


All times are GMT -5. The time now is 04:56 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration