Difference between revisions of "Registers"

From SkullSecurity
Jump to navigation Jump to search
(New page: This section is the first section specific to assembly. So if you're reading through the full guide, get ready for some actual learning! A register is like a variable, except that there ...)
 
Line 19: Line 19:
eax has 2 common uses: the return value and the "accumulator".  
eax has 2 common uses: the return value and the "accumulator".  


When a function returns, its return value is set to eax. Here is an example in C, and then in assembly:
When a function returns, its return value is set to eax. Here is an example in C:
  return 3;  // Return the value 3
  return 3;  // Return the value 3


Here's the same code in assembly:
  mov eax, 3 ; Set eax to 3
  mov eax, 3 ; Set eax to 3
  ret        ; Return
  ret        ; Return

Revision as of 17:33, 12 March 2007

This section is the first section specific to assembly. So if you're reading through the full guide, get ready for some actual learning!

A register is like a variable, except that there are a fixed number of registers. Each register is a special spot in the CPU where a single value is stored. A register is the only play where math can be done (addition, subtraction, etc). Registers frequently hold pointers which reference memory. Movement of values between registers and memory is very common.

Intel assembly has 8 general purpose 32-bit registers: eax, ebx, ecx, edx, esi, edi, ebp, esp. Although any data can be moved between any of these registers, compilers commonly use the same registers for the same uses, and some instructions (such as multiplication and division) can only use the registers they're designed to use.

Different compilers may have completely different conventions on how the various registers are used. For the purposes of this document, I will discuss the most common compiler, Microsoft's.

Volatility

Some registers are typically volatile across functions, and others remain unchanged. This is a feature of the compiler's standards and must be looked after in the code, registers are not preserved automatically (although in some assembly languages they are -- but not in x86). What that means is, when a function is called, there is no guarantee that volatile registers will retail their value when the function returns, and it's the functions responsibility to preserve non-volatile registers.

The conventions used by Microsoft's compiler are:

  • Volatile: ecx, edx
  • Non-Volatile: ebx, esi, edi, ebp
  • Special: eax, esp (discussed later)

Descriptions

  • eax - Accumulator

eax has 2 common uses: the return value and the "accumulator".

When a function returns, its return value is set to eax. Here is an example in C:

return 3;  // Return the value 3

Here's the same code in assembly:

mov eax, 3 ; Set eax to 3
ret        ; Return



  • ebx
  • ecx
  • edx
  • esi
  • edi
  • ebp
  • esp

16-bit Registers

64-bit Registers