|
|
|
|
|
by josefx
633 days ago
|
|
> but doesn't unoptimized C map closely to 1:1 What is a variable in C? A register? A memory location? The language doesn't have basic concepts needed to map anything 1:1 to assembly and the ones it has usually come with half a dozen standards worth of required error handling, because having single instruction features like sqrt return -1 on error wasn't enough. |
|
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.