There are 3 kinds of statements in any high level programming language:
|
You can prove that programming languages that contain these 3 statement types are Turing complete.
We have learned enough assembler programming to show you how a compiler translates assignment statements that only using simple (= unstructured) variables
I can now show you how the (C) compiler translates this C program:
int a = 4; short b = 5; char c // 1 byte; int main( ) { c = a + 5*b; } |
Translating the variable definitions:
int a = 4;
short b = 5;
char c // 1 byte;
int main( )
{
c = a + 5*b;
}
|
We have discussed variable definitions before:
.data
a: .4byte 4 // a initialized with the value 4
b: .2byte 5 // b initialized with the value 5
c: .skip 1 // c is not initialized
|
DEMO: /home/cs255001/demo/asm/2-mov/add.s
Translating the assignment statement:
int a = 4;
short b = 5;
char c // 1 byte;
int main( )
{
c = a + 5*b;
}
|
How does the compiler translate the above statement into ARM assembler code:
.text // Always translate the RHS first (Teaching note: explain register allocation and de-allocation) |
Translating the assignment statement:
int a = 4;
short b = 5;
char c // 1 byte;
int main( )
{
c = a + 5*b;
}
|
Load a into a register:
.text // r1 = (copy of) a movw r0, #:lower16:a // Moves the address of memory movt r0, #:upper16:a // variable a into register r0 ldr r1,[r0] // Load value in int var a to r1 |
Translating the assignment statement:
int a = 4;
short b = 5;
char c // 1 byte;
int main( )
{
c = a + 5*b;
}
|
Then load b into another register:
.text // r1 = (copy of) a movw r0, #:lower16:a movt r0, #:upper16:a ldr r1,[r0] // r2 = (copy of) b movw r0, #:lower16:b // Moves the address of memory movt r0, #:upper16:b // variable b into register r0 ldrsh r2,[r0] // Load value in short var b to r2 |
Translating the assignment statement:
int a = 4;
short b = 5;
char c // 1 byte;
int main( )
{
c = a + 5*b;
}
|
Multiply b with 5 to obtain 5*b:
.text // r1 = (copy of) a movw r0, #:lower16:a mov r3, #5 movt r0, #:upper16:a mul r2, r3, r2 // r2=5*b ldr r1,[r0] // r2 = (copy of) b movw r0, #:lower16:b movt r0, #:upper16:b ldrsh r2,[r0] |
Translating the assignment statement:
int a = 4;
short b = 5;
char c // 1 byte;
int main( )
{
c = a + 5*b;
}
|
Add a with 5*b --- the result is in a register...
.text // r1 = (copy of) a movw r0, #:lower16:a mov r3, #5 movt r0, #:upper16:a mul r2, r3, r2 // r2=5*b ldr r1,[r0] add r1, r1, r2 // r1=RHS // r2 = (copy of) b movw r0, #:lower16:b movt r0, #:upper16:b ldrsh r2,[r0] |
Translating the assignment statement:
int a = 4;
short b = 5;
char c // 1 byte;
int main( )
{
c = a + 5*b;
}
|
Store the result into variable c in memory:
.text // r1 = (copy of) a movw r0, #:lower16:a mov r3, #5 movt r0, #:upper16:a mul r2, r3, r2 // r2=5*b ldr r1,[r0] add r1, r1, r2 // r1=RHS // r2 = (copy of) b movw r0, #:lower16:c movw r0, #:lower16:b movt r0, #:upper16:c movt r0, #:upper16:b strb r1, [r0] ldrsh r2,[r0] |
The complete ARM assembler program:
// Move a into r1 movw r0, #:lower16:a // Moves the address of memory movt r0, #:upper16:a // variable a into register r0 ldr r1,[r0] // Move value in int var into r1 // Move b into r2 movw r0, #:lower16:b // Moves the address of memory movt r0, #:upper16:b // variable b into register r0 ldrsh r2,[r0] // Move value in short var into r2 // Compute 5*b mov r3, #5 // r3 = 5 mul r2, r3, r2 // r2 = r3 * r2 = 5*b // Compute a + 5*b add r1, r1, r2 // r1 = a + b // Store sum (in r1) to var c movw r0, #:lower16:c // Moves the address of memory movt r0, #:upper16:c // variable c into register r0 strb r1,[r0] |
DEMO: /home/cs255001/demo/asm/2-mov/add.s