tags:

views:

344

answers:

3

I routinely compile my C code to assembly to read the output and learn about how the compiler interprets my code. While doing this, I often think, "maybe I should just write my software in assembly!"

But in all honesty I don't know how to begin. I've written 16 bit assembly compiled with tasm before and it was sorta fun. But I want to start writing assembly and interfacing it with C programs, etc--doing everything I can do with C, in assembly instead.

But, how?

Do I need to learn about executable headers (e.g. ELF)? (If not, I still want to--that's another thing--how can I write the header myself and just compile pure binary, without the compiler trying to add the header again?)

What is the (arguably) most common compiler? I wish to use the gcc since I use it already, but the AT&T assembly syntax causes me unbearable pain and suffering.

Can I still use the C standard library functions? I mean, I'm used to just calling an interrupt, which you can't do in protected mode--so is there an assembly version of printf? malloc? etc? I've seen them used in the assembly output of gcc, but how do I include those to begin with in a straight assembly project?

All this being said I can read assembly, and sort of know how to write it--I understand calling conventions and the like, I just want to actually write programs in it rather than having some theoretical knowledge of it.

Anyways, does anyone know any good tutorials? Recommended compilers/syntax? Common mistakes? Any tips that will make my life easier (inb4 don't use assembly)?

edit: one more thing:

I remember in my 16-bit assembling days stumbling over myself when the entry point of a "procedure" got too far away from the point where it was called because it was called with a short jump or something (a small offset from the point where it is called) instead of just a memory address, and I couldn't figure out how to do it otherwise.

Is this an issue in 32 bit? How can I be safe about the jumps in my code?

A: 

My best suggestion, having taken a compilers class where I wrote a subset of the C language to compile to MIPS, buy a book on the topic for the architecture you want to write for. There is just too must to know to convey in an SO question.

I suspect you're looking for Intel-compatible, x86 assembly. Many architectures are 32-bits, for example, MIPS is also 32-bits. I don't know of any specific books on x86 assembly, though I'm sure if you asked SO for suggestions, they'd have great ones.

As for common gotchas: Be careful of instructions which have a "delay slot", not sure if x86 has these though.

Ben S
Isn't MIPS 16 bits?
Imagist
No: http://www.mips.com/products/architectures/
Ben S
A: 

If I understand correctly, there are no near/far jumps in 32-bit. You're only going to have one segment for data, one for stack, and one for code (they might even be one and the same).

What architecture are you wanting to start in? It sounds like Intel/PC. What OS? Linux?

You can write assembly using the standard C libraries (fopen, fclose, malloc, etc). I believe there's a Linux-only API as well. Same goes with Windows--there's the standard C and there's the Win32 API.

I believe file headers are necessary in Linux and Windows--you can't write pure code like you could with DOS.

Interrupts are replaced by the C, Linux, Win32, etc APIs. They're all C-based which means you can readily use them with assembly.

Jake
well, I meant, I could include the headers in my code, and then compile to pure binary (and not have the headers included a second time, as I wrote them myself). I know I don't need to, but I thought it would be interesting to learn anyways.
Carson Myers
+1  A: 

Do I need to learn about executable headers (e.g. ELF)?

No, your linker will do that for you.

(If not, I still want to--that's another thing--how can I write the header myself and just compile pure binary, without the compiler trying to add the header again?)

Use a hex editor. :-)

Sorry, I'm just kidding: I don't know the answer.

What is the (arguably) most common compiler?

gcc

I wish to use the gcc since I use it already, but the AT&T assembly syntax causes me unbearable pain and suffering.

How about msvc then.

Can I still use the C standard library functions?

Yes.

I mean, I'm used to just calling an interrupt, which you can't do in protected mode--so is there an assembly version of printf? malloc? etc?

No, you just use the ordinary/object/library version.

I've seen them used in the assembly output of gcc, but how do I include those to begin with in a straight assembly project?

  1. Declare them as being extern to your assembly code
  2. Call them from your assembly, using the right calling conventions (i.e. the right parameter-passing conventions and the right name-mangling conventions)
  3. Run the linker, passing your assembly and the C standard library as input.

Is this an issue in 32 bit? How can I be safe about the jumps in my code?

I don't think it's an issue.

ChrisW