Question

I am attempting to learn assembly language, but i need help with learning what each command's purpose is. Following is a program where they are used.

 push   %ebp
 mov    %esp,%ebp
 sub    $0x10,%esp
 mov    0x8(%ebp),%eax
 add    $0x1,%eax
 mov    %eax,-0x4(%ebp)
 mov    -0x4(%ebp),%eax
 leave  
 ret
Was it helpful?

Solution

A very basic Assembly Language Primer:

First, you should know that assembly languages are relative to a particular Processor (CPU) or family of Processors. For example the snippet shown in the question appears to be for the Intel x86 architecture. This is important because very many assembly language instructions act upon, or on the basis of, the content of the CPU's registers. Therefore before even delving into the details of individual instructions you should get familiar with the CPU's architecture, i.e. the names and characteristics of the available registers as well as their roles (some of these registers are generic in nature but others have very specific roles). Also part of the architecture is the way outside memory locations are addressed.

Second, there are but very few kinds of assembly language instructions:

  • flow control instructions (tests, jumps, return [from subroutine], etc.)
  • data copy instructions (move memory content to/from registry and/or memory)
  • stack management instructions (these are a special kind of data copy instruction which also act on the Stack Pointer oft' named SP, a special register).
  • arithmetic instructions (add, subtract, multiply, etc.)
  • boolean operations (shift [register contents], bitwise and, bitwise or, etc.)

Third, what can sometimes make things more complicated (but also what make assembly logic quite powerful) are addressing modes. Essentially addressing modes are used to determine which memory location(s) will be used for a given instruction. Example of addressing modes are direct and indirect, whereby the former provides the address where to read/write directly as an operand of the instruction and the latter provides a memory address operand as well, but the effective address used for the read/write is obtained by reading first the content of the address provided and used that content as the address where the operation should take place.

With the above basic understanding, you can probably start getting into the Instructions Manual for the processor and make more sense of it all.

Maybe start with the following Wikipedia entries:

and then maybe a document like the this 8 pages introductory course notes (pdf)

A last word of caution: looking directly at code and trying to figure what it does is a very effective way of getting to understand assembly language. However I noted that your original posting (on pastebin) made reference to a disassembler; beware that sometimes disassemblers can get confused (for example when they handle areas of data just as they contained code) and produce very non sensible instruction lists. Also by reading real source code (rather source produce by disassembly), you benefit from the author's comment and good label names which are good help in understanding the logic.

OTHER TIPS

this is a pretty good book for your purpose:

Computer Systems: A Programmer's Perspective

Just a quick search yielded the following reference for x86 assembly language

Why you wish to learn assembly language is another matter!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top