Exploring simplified parsing code and memory card output

Can someone explain to me what's going on at the bottom? Questions:

  • Why doesn't the variable stay in the register?
  • why does the rbp register contain the address of the memory cache line and not the address i (i.e. 4 bytes)?
  • What does it mean? movl $0x5,-0x4(%rbp)

    ; What do %

    and mean negative 0x4

    ?

int main(int argc, char* argv[])
{
    int i = 5;  // line 25
    i ++;       // line 26
}
-----------------------------------------------------

Disassembly:
25                  int i = 5;
00000000004014f4:   movl $0x5,-0x4(%rbp)
26                  i ++;
00000000004014fb:   addl $0x1,-0x4(%rbp)
-----------------------------------------------------

Register Values:
rbp: 0x23fe60
-----------------------------------------------------

Memory map at line 25:
Address  |   0-3  |   4-7  |   8-B  |   C-F  |
0x23fe60 |00000000|00000000|00000000|05000000|

Memory map at line 26:
Address  |   0-3  |   4-7  |   8-B  |   C-F  |
0x23fe60 |00000000|00000000|00000000|06000000|

      

Note: the above was generated by Eclipse, I am compiling using mingw on a 64 bit machine.

+3


source to share


4 answers


  • Why doesn't the variable stay in the register?

Because your compiler didn't like it. He decided to push the variable i

onto the stack.

  • why does the rbp register contain the address of the memory cache line and not the address i (i.e. 4 bytes)?

rbp

does not contain a memory cache address. rbp

is the Base pointer that points to the bottom of the stack. -0x4(%rbp)

is the location of the variable i

in memory (on the stack). This means rbp MINUS value 4. Why 4? Because it i

takes 4 bytes. So this is the address i

.

See http://en.wikibooks.org/wiki/X86_Disassembly/The_Stack



  • What does it mean? movl $0x5,-0x4(%rbp)

    ; What does %

    and mean negative 0x4

    ?

There are 2 general assembly syntaxes

  • AT&T which is an unreadable mess
  • Intel "normal" syntax

Your code is in AT&T unfortunately. %

- as variables are referenced in AT & T syntax -0x4

- hexadecimal representation of a number -4

(see answer above).

See http://en.wikipedia.org/wiki/X86_assembly_language#Syntax

+3


source


  • There is no such thing as a "cache address" - the cache is abstracted from the program, only the processor and OS know about the cache and how to translate memory addresses into "cache addresses"

  • The variable i is allocated on the stack. rbp is a base frame pointer that points to the current frame of the function, which is executing on the stack. therefore -0x4(%rbp)

    means that at offset 4 (backward - return to this point later) in the rbp register is the variable i. Learn more about how the stack works and what its frames are: http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/stack.html

  • movl $0x5,-0x4(%rbp)

    valid and you are correct seems strange coming from the Intel syntax world. But this is actually an example of AT&T syntax. Read here for further explanations: http://en.wikipedia.org/wiki/X86_assembly_language#Syntax



I really think you might find it helpful to read this: http://lwn.net/Articles/250967 , it helped me a lot when thinking about the "big picture" of memory.

+2


source


  • Do you mean "not in the registry?" Since your program is not compiled with high optimization settings (but if you compiled with high optimization values, your whole procedure would be empty because it does nothing (has no side effects). The address containing your variable will certainly be promoted to the memory cache when the CPU executes these instructions

    • register rbp contains the base of the function's stack frame where local automatic variables are allocated, this is determined by the calling convention of your language.

    • in this case, the mov instruction is to move the immediate value to a memory location. % Has a register name. Since stacks grow down traditionally, the offset of the local automatic variable is the negative offset from the frame / stack base.

+2


source


  • Why doesn't the variable i stay in the register?

This is a compiler decision. In the past, people used the keyword register

to tell the compiler that a variable is being used frequently, so that the compiler could possibly use a register for that field instead of writing on the stack. This is not recommended right now as compilers are very smart these days and will often optimize much better than we can.

  • why does the rbp register contain the address of the memory cache line and not the address i (i.e. 4 bytes)?

When the function is entered, local variables will be pushed onto the stack. This will set rbp to rsp, the stack pointer. Since rsp points to the next free space, so does rbp, so you need -4 to get the address of the last variable pushed onto the stack, in this case i

.

  • What does it mean? movl $ 0x5, -0x4 (% rbp); What does% mean and negative 0x4 mean?

Place the value 0x5 in the rbp-4 address.

0


source







All Articles