| > What is a variable in C? A register? A memory location? Wouldn't it depend on the type? Something like: int p; p = &x; MOV @R1, R2 ; R1 contains the address of x, move it to pointer p in R2 int p; int value = *p; MOV @R2, R0 ; Dereference pointer p (in R2), load the value into R0 (int value) int x = 5; MOV #5, -(SP) ; Push the value 5 onto the stack (stack-allocated int) int x = 10; int y = x + 5; MOV #10, R0 ; Load the immediate value 10 into register R0 (for x) ADD #5, R0 ; Add 5 to the value in R0 (x + 5), store result in R0 or MOV #10, -(SP) ; Push 10 onto the stack for x MOV (SP), R0 ; Load x from stack into R0 ADD #5, R0 ; Add 5 to x Whether a variable gets stack-allocated or register-allocated, it's still a pretty close mapping afaict. From my understanding the original C mapped closely to PDP-7 and then PDP-11 assembly. The original implementation and how it maps to PDP-11 could be used as a reference implementation. |
Depending on optimization level, things can change. Without any optimizations, variables of “automatic storage duration” such as local variables, may get placed on the stack. But with optimizations turned on, they may end up in a register, or even not be stored anywhere, for example if they’re an integer literal that never gets modified after assignment.