Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I was looking at Project Euler Problem 48:

The series, 11 + 22 + 33 + ... + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.

In Python I can do this with a single line :

sum((x**x for x in xrange(1,1001))

But the assembly equivalent of this would be 100 lines and a real challenge.

Should I be doing some of these puzzles in assembly to gain some insight into low-level programming and understand how the computer actually works?

share|improve this question

closed as primarily opinion-based by durron597, MichaelT, enderland, Snowman, gnat Jul 13 at 22:30

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

If you don't know how memory works how can you be expected to deal with topics like pointers? If you don't understand how a computer and operating system works how can you program an application? –  Ramhound Apr 11 '11 at 15:37
I can make a lot of abstractions Ramhound thats why I asked you this . I might not get the full hang of pointers still able to do somethings in pointers like swapping which makes logical sense once you assume pointers store memory location of some other datatype .Again Ramhound since you suggested that I should understand these things can you suggest what broad topics I should read ? Assembly Language and a bit of COA would be good ? –  Nishant Apr 11 '11 at 16:43
If you don't get the full hang of pointers, you will find that to be a severe disadvantage in some places. –  David Thornley Apr 11 '11 at 18:29
Either your Python line computes x^2 for x in 1..1000 (instead of x^x for x in 1..1000) or python is even more magic than I thought. :) –  Ingo Apr 12 '11 at 15:21
Is the "raise to the power of" function * in Python? –  user1249 Aug 28 '11 at 20:05

6 Answers 6

up vote 1 down vote accepted

Beyond learning assembly, I believe that learning how a low-level language like C is compiled is highly valuable. So my answer is yes, but then again I'm probably biased because I enjoy low-level programming.

For example, just understanding how simple statements are compiled. The following function,

int func(int val)
  int num = val * 5;
  return num;

...becomes (the interesting bit at least):

movl    %edi, -20(%rbp)
movl    -20(%rbp), %edx
movl    %edx, %eax
sall    $2, %eax
addl    %edx, %eax

This code takes the argument from the stack (val, the parameter to func), shifts it the left 2 places (multiply by 2^2 or 4) and then adds the original value to the result. The end result is a multiplication by 5. An example like this illustrates a number of things to be aware of, such as compiler optimizations. Instead of calling an instruction to directly multiply by 5, it shifts two places to multiply by 4 and then adds the original value. I found examples like this to greatly improve my understanding of things at a lower level.

Generate assembler output from gcc with the -S option. However, be aware that the results will vary by compiler and optimization level.

Anyway, I don't think being an assembly language programmer is the same as understanding assembly. Again, I feel that programming in a language like C and knowing how it gets put into machine code is a valuable practice.

share|improve this answer

Probably not necessary to actually be able to write assembler (most of which sis details of the initialisiation and calling convention for your system).

It is worth understanding some, in case you are trying to debug something without source.

BUT it is definitely worth understanding the machine at least at the level of 'C' and pointers (essentially a high level assembler) so that you know why concating a string a million times in a loop is bad.

share|improve this answer
Actually, some relatively clever implementations can make for i in range(1000000): s = s + '.' no(t much) worse than the "optimized" versions my re-using s. Similarily, several other developments invalidate what's a reasonable assumption with C knowledge and assuming a naive implementation. But in general it's more useful than harmful. Just keep in mind that language implementations are sometimes smarter than you ;) –  delnan Apr 11 '11 at 16:36
@delnan -I just needed a short example of where something that looks innocuous in Java/C# might be very expensive on silicon. I have particularly come across a lot of people who think a large memory allocation and copy is instant. –  Martin Beckett Apr 11 '11 at 16:39
I hate to believe that delnan :-) –  Nishant Apr 11 '11 at 17:04

Good question. Learning Assembly is definitely good and worth the effort.

  1. Such low level understanding will be of good use in embedded software development
  2. Refactoring at a low level
  3. It will help you in effectively utilizing the debugger
  4. Give you an understanding of disassembled code to help in stuff like code caves
share|improve this answer
I had to look up codecaves. I use these a lot, but we call them "Detours" now. research.microsoft.com/en-us/projects/detours –  ProdigySim Apr 11 '11 at 18:25

Yes and no.

While it is true that it will grant you a better understanding of what your code is doing and why some things are just a bad idea you have to think about the effort.

Learning assembler is not a weekend project, it will take you a lot of time and you need to think whether or not this time could be better spent.

If your not into optimised code then you probably wont ever see benefits equal to the effort you put in.

share|improve this answer

I did a lot of assembler when I was younger and I don't think it helped understand anything outside of assembler. If you take a look at modern assembler it's all macro language stuff anyway. I usually hate analogies, but here goes anyway: Does knowing how the engine of a car works make you a better driver?

share|improve this answer
Does knowing how the engine of a car works make you a better driver? No, but it makes you a better car owner, because if the engine breaks down you can fix it. –  Cameron Martin Apr 16 '14 at 21:15
Agreed, but it doesn't make you a better driver, which is the analogy I was using. By fixing the engine yourself you're not supporting the wider economy by employing mechanics,... Analogies come with limits. Generally I'd never discourage someone from looking to learn something if they are interested, but I think you need to go pretty deep down the rabbit hole to be able to apply it usefully to higher level languages. For a start you need to understand the compile optimisations used by the specific language if it's compiled, or the interpreter, or the jitter. It's a huge area. –  Ian Apr 17 '14 at 3:14

The way I've been working on understanding assembler is by writing programs in higher-level languages, then replacing parts with (at least hopefully) functionally equivalent tracts of assembled code. This means I get to use HLLs for what they're good for-organising and solving high-level problems-and I use asm for banging the metal.

(When I talk about the host program being written in a HLL, I mean C or ObjC when I'm trying to learn x86_64 asm, and BASIC when I'm working on Z80, 6502 and 6809 asm).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.