3 deleted 17 characters in body
source | link

When a computer stores a variable, when a program needs to get the variable's value, how does the computer know where to look in memory for that variable's value?

The program tells it. (Also, computersComputers do not natively have a concept of "variables";"variables" - that's entirely a high-level language thing!)

Here's a C program:

int main(void)
{
    int a = 1;
    return a + 3;
}

and here's the assembly code it compiles to: (comments starting with ;;)

main:
    ; {
    pushq   %rbp
    movq    %rsp, %rbp

    ; int a = 1
    movl    $1, -4(%rbp)

    ; return a + 3
    movl    -4(%rbp), %eax
    addl    $3, %eax

    ; }
    popq    %rbp
    ret

For "int a = 1;" the CPU sees the instruction "store the value 1 at the address (whatever is invalue of register rbp, minus 4)". It knows where to store the value 1 because the program tells it.

Likewise, the next instruction says "load the value at address (whatever is invalue of register rbp, minus 4) into register eax". The computer doesn't need to know about things like variables.

When a computer stores a variable, when a program needs to get the variable's value, how does the computer know where to look in memory for that variable's value?

The program tells it. (Also, computers do not natively have a concept of "variables"; that's entirely a high-level language thing!)

Here's a C program:

int main(void)
{
    int a = 1;
    return a + 3;
}

and here's the assembly code it compiles to: (comments starting with ;)

main:
    ; {
    pushq   %rbp
    movq    %rsp, %rbp

    ; int a = 1
    movl    $1, -4(%rbp)

    ; return a + 3
    movl    -4(%rbp), %eax
    addl    $3, %eax

    ; }
    popq    %rbp
    ret

For "int a = 1;" the CPU sees the instruction "store the value 1 at the address (whatever is in register rbp, minus 4)". It knows where to store the value 1 because the program tells it.

Likewise, the next instruction says "load the value at address (whatever is in register rbp, minus 4) into register eax". The computer doesn't need to know about things like variables.

When a computer stores a variable, when a program needs to get the variable's value, how does the computer know where to look in memory for that variable's value?

The program tells it. Computers do not natively have a concept of "variables" - that's entirely a high-level language thing!

Here's a C program:

int main(void)
{
    int a = 1;
    return a + 3;
}

and here's the assembly code it compiles to: (comments starting with ;)

main:
    ; {
    pushq   %rbp
    movq    %rsp, %rbp

    ; int a = 1
    movl    $1, -4(%rbp)

    ; return a + 3
    movl    -4(%rbp), %eax
    addl    $3, %eax

    ; }
    popq    %rbp
    ret

For "int a = 1;" the CPU sees the instruction "store the value 1 at the address (value of register rbp, minus 4)". It knows where to store the value 1 because the program tells it.

Likewise, the next instruction says "load the value at address (value of register rbp, minus 4) into register eax". The computer doesn't need to know about things like variables.

2 edited body
source | link

When a computer stores a variable, when a program needs to get the variable's value, how does the computer know where to look in memory for that variable's value?

The program tells it. (Also, computers do not natively have a concept of "variables"; that's entirely a high-level language thing!)

Here's a C program:

int main(void)
{
    int a = 1;
    return a + 3;
}

and here's the assembly code it compiles to: (comments starting with ;)

main:
    ; {
    pushq   %rbp
    movq    %rsp, %rbp

    ; int a = 1
    movl    $1, -4(%rbp)

    ; return a + 3
    movl    -4(%rbp), %eax
    addl    $3, %eax

    ; }
    popq    %rbp
    ret

For "int a = 1;" the CPU sees the instruction "store the value 1 at the address (whatever is in register ebp +rbp, minus 4)". It knows where to store the value 1 because the program tells it.

Likewise, the next instruction says "load the value at address (whatever is in register ebp +rbp, minus 4) into register eax". The computer doesn't need to know about things like variables.

When a computer stores a variable, when a program needs to get the variable's value, how does the computer know where to look in memory for that variable's value?

The program tells it. (Also, computers do not natively have a concept of "variables"; that's entirely a high-level language thing!)

Here's a C program:

int main(void)
{
    int a = 1;
    return a + 3;
}

and here's the assembly code it compiles to: (comments starting with ;)

main:
    ; {
    pushq   %rbp
    movq    %rsp, %rbp

    ; int a = 1
    movl    $1, -4(%rbp)

    ; return a + 3
    movl    -4(%rbp), %eax
    addl    $3, %eax

    ; }
    popq    %rbp
    ret

For "int a = 1;" the CPU sees the instruction "store the value 1 at the address (whatever is in register ebp + 4)". It knows where to store the value 1 because the program tells it.

Likewise, the next instruction says "load the value at address (whatever is in register ebp + 4) into register eax". The computer doesn't need to know about things like variables.

When a computer stores a variable, when a program needs to get the variable's value, how does the computer know where to look in memory for that variable's value?

The program tells it. (Also, computers do not natively have a concept of "variables"; that's entirely a high-level language thing!)

Here's a C program:

int main(void)
{
    int a = 1;
    return a + 3;
}

and here's the assembly code it compiles to: (comments starting with ;)

main:
    ; {
    pushq   %rbp
    movq    %rsp, %rbp

    ; int a = 1
    movl    $1, -4(%rbp)

    ; return a + 3
    movl    -4(%rbp), %eax
    addl    $3, %eax

    ; }
    popq    %rbp
    ret

For "int a = 1;" the CPU sees the instruction "store the value 1 at the address (whatever is in register rbp, minus 4)". It knows where to store the value 1 because the program tells it.

Likewise, the next instruction says "load the value at address (whatever is in register rbp, minus 4) into register eax". The computer doesn't need to know about things like variables.

1
source | link

When a computer stores a variable, when a program needs to get the variable's value, how does the computer know where to look in memory for that variable's value?

The program tells it. (Also, computers do not natively have a concept of "variables"; that's entirely a high-level language thing!)

Here's a C program:

int main(void)
{
    int a = 1;
    return a + 3;
}

and here's the assembly code it compiles to: (comments starting with ;)

main:
    ; {
    pushq   %rbp
    movq    %rsp, %rbp

    ; int a = 1
    movl    $1, -4(%rbp)

    ; return a + 3
    movl    -4(%rbp), %eax
    addl    $3, %eax

    ; }
    popq    %rbp
    ret

For "int a = 1;" the CPU sees the instruction "store the value 1 at the address (whatever is in register ebp + 4)". It knows where to store the value 1 because the program tells it.

Likewise, the next instruction says "load the value at address (whatever is in register ebp + 4) into register eax". The computer doesn't need to know about things like variables.