views:

904

answers:

3

Hey guys...

I'm really wanting to learn assembly. I'm pretty good at c/c++, but want a better understanding of what's going on at a lower level.

I realize that asembly related questions have been asked before, but I'm just looking for some direction that's particular to my situation:

I'm running windows 7, and am confused about how I should start working with assembly. Do I have to start with x64 because I'm running windows 7? Some people have said 'start with 32 bit first' - how do I go about doing this? What does my operating system have to do with my ability to write assembly for '32' or '64' bit. In fact, what does 'n bit' assembly mean, where n is a number??

Thanks in advance for the answers!


Edit:

Here are some links that have helped me get started with assembly; others who are just getting started may find them helpful. I'll keep updating this list as I continue on my assembly journey :)

Note: As I've been learning, I've decided to focus on programming with masm32. Therefore most of the below resources focus on that.

+1  A: 

Get IDA pro. It's the bees knees for working with assembly.

I personally don't see much of a difference between 32-bit and 64-bit. It is not about the bits but the instruction set. When you talk about assembly you talk about instruction sets. Perhaps they are implying that a 32-bit instruction set is better to learn from. However if that is your goal I suggest Donald Knuths books on algorithms -- they teach algorithms in terms of a 7-bit instruction set assembly :D

For portability issues, I suggest that instead of inline assembly you learn how to use compiler intrinsics -- it will be the best optimization for non-embedded optimizations. :D

Hassan Syed
From what I'm reading, it's a disassembler... so, actually writing/coding some assembly language code isn't possible, right? If so, this is only a half-answer.
jae
Working with assembly these days (even in embedded land) is about making adjustments to code generated by a C/C++ compiler. IDA makes this job as painless as possible.
Hassan Syed
That may be but it still didn't respond to my original question that well :)
Cam
@incrediman There is a lot more to my answer than IDA pro.....
Hassan Syed
@Hassan, what if I need to write code that runs before a C/C++ runtime exists?
Carl Norum
@Hassan Yeah, true. Maybe I was a bit harsh... sorry!
Cam
@carl you know the answer to that nonsensical question as well as I, you write some assembler/machine code.
Hassan Syed
@Hassan, you just said four comments higher up that working with assembly means making adjustments to compiler-generated code; I was just trying to present the counterexample.
Carl Norum
+14  A: 

When people refer to 32-bit and 64-bit assembly, they're talking about which instruction set you'll use - they're also sometimes called Ia32 and x64 in the Intel case, which I presume you're asking about. There is a lot more going on in the 64-bit case, so starting with 32-bit is probably good; you just need to make sure you're assembling your program with a 32-bit assembler into a 32-bit binary. Windows will still know how to run it.

What I really recommend for getting started with assembly would be something with a simpler instruction set to get a handle on. Go learn MIPS assembly - the spim simulator is great and easy to use. If you really want to dive straight into the Intel assembly world, write yourself a little C program that calls your assembly routines for you; doing all the setup and teardown for a 'real program' is a big mess, and you won't even be able to get started there. So just write a C wrapper with main() in it, and compile and link that with the object files you get from writing your assembly code.

Please don't get in the habit of writing inline assembly in your C code - it's a code portability nightmare, and there's no reason for it.

You can download all of the Intel 64 and IA-32 Architectures Software Developer's Manuals to get started.

Carl Norum
This is helpful, thanks.The instruction set difference makes sense... is that the only difference?Like, is there any difference in the way a program written in 32-bit will run as opposed to a 64-bit program? If not, why are they called 32-bit/64-bit, as opposed to 'instruction set A' and 'instruction set B', for example?
Cam
@incrediman, the instruction set is a pretty huge difference. The instruction sets do have different names, but people just use 32-bit/64-bit as shorthand. In addition, there are different calling conventions (ABI) between the two instruction sets, and even two competing 64-bit ABIs.
Carl Norum
"For the time being, we'll just be doing 32 bit coding, so the internal registers in the CPU, for us, will be 32 bits. Each bit is capable of storing a 1 or a 0." (http://www.friedspace.com/assembly/cpuregs1.php) ... Is this an additional difference between 32 and 64 bit asm? If not what is that sentence trying to say?Thanks!
Cam
@incrediman, yes. The 64-bit architecture has 64-bit registers (and memory addressing), and the 32-bit architecture has 32-bit registers. That's one of the reasons people use the 64/32 names to differentiate the two.
Carl Norum
Thanks for your patience/answers!Alright - so at this point I have downloaded masm32, and have successfully built a 'hello world' console app using some simple example code an an include file. I downloaded masm32 here: http://www.masm32.com/So I have it working. is this a good place to start though?
Cam
@incrediman, that sounds as good as any, yeah. Good luck!
Carl Norum
@Carl, `x64` should be `x86-64`, to prevent confusion with incompatible 64-bit Intel processor series, the `IA64` (Itanium).
mctylr
That then would be IA64, and not x64...
Mustapha Isyaku-Rabiu
+1 for MIPS recommendation.
Mustapha Isyaku-Rabiu
Definitely consider getting some microcontroller emulator and learning assembly for it. Assembly is pretty much dead for PC (most compilers can produce better ASM code from higher-language than programmers could by hand), but it's still very strong on microcontrollers, and various cool projects you could achieve with them are really worth the effort. Plus you get to write without the OS, which in assembly gets in your way more often than helps.
SF.
Thanks SF that's a good idea. Actually though I have some microcontrollers lying around, so I'll probably learn assembly for them, which would be pretty cool. Also, it's not like I'm trying to make useful software with assembly for my Windows 7 PC :) ... I'm mainly learning it so I can start learning about what happens behind my higher-level code.
Cam
I think I'd differ with these guys and go for 64 bit -- 64 bit assembly has twice as many registers which gives more room for learning.
Joel
A: 

but want a better understanding of what's going on at a lower level

If you really want to know everything that's going on at a lower level on x86/x64 processors/systems, I would really recommend starting with the basics, that is, 286/386 real mode code. For example, in 16-bit code you are forced to use memory segmentation which is an important concept to understand. Today's 32-bit and 64-bit operating systems are still started in real mode, then switch to/between the relevant modes.

But if you're interested in application/algorithm development, you might not want to learn all the low-level OS stuff. Instead you can start right off with x86/x64 code, depending on your platform. Note that 32-bit code will also run on 64-bit Windows, but not the other way round.

AndiDog
Boot time isn't the only lower-level way to interact with a system; I think writing native assembly for OS programs is a good way to start. Writing and debugging boot systems is not for the faint of heart.
Carl Norum
At this point, I am "thunk" 'ing that 16-bit segments are dead! ;-)
Tall Jeff
Understanding 16 bit segments is about as useful as learning how Roman numerals work. And as far as starting in real mode to bootstrap your own OS, that would take a couple of years of study unless it's just going to be printing out "The BIOS handed me these register values on screen xxxx xxxx". Low level stuff like reading/writing hardware ports in device drivers would be a good use for assembly code even if you're not an asm genius.
Arthur Kalliokoski