CS计算机代考程序代写 x86 Lecture 9: C to LC-3

Lecture 9: C to LC-3

@NCStateECE

Connect to slido.com
#ece209

ECE 209
Computer Systems Programming
Spring 2021

Lecture 9: LC-3 Variables and
Expressions

Announcements
Z4 due tomorrow, 3/2
Next PS: 3/10

Photo by Sora Shimazaki on Pexels

https://www.pexels.com/photo/man-in-blue-denim-jacket-holding-a-megaphone-5935755

LC-3 Processor

Data Type
16-bit signed integer

Memory
216 addresses
word-addressable (16 bits)

LC-3 Processor

Arithmetic/Logic Unit (ALU)
ADD, AND, NOT

Register File
8 registers (R0 – R7)

Control Unit
Fetch, PC, Branch, JSR

C Data Type Sizes

int

double

char

4B

8B

1B

1 word

2 words

1 word

x86 LC-3

Size is platform-specific, not standard.

sizeof Operator

sizeof(int)

sizeof(double)

sizeof(char)

4

8

1

2

4

2

x86 LC-3

Local Variables
int g1;
double g2;

int main() {
int x;
char y;
double z;
int a;

}
int foo() {

int a;
int b;

}

x

y

z

a

a

b

0

-1

-3

-4

Offset

0

-1

R5 →

R5 →

Local Variables
int g1;
double g2;

int main() {
int x;
char y;
double z;
int a;

}
int foo() {

int a;
int b;

}

x

y

z

a

R5 →

; while compiling main()…
; load from a into R0
LDR R0, R5, #-4

; store from R3 into y
STR R3, R5, #-1

Global Variables
int g1;
double g2;

int main() {
int x;
char y;
double z;
int a;

}
int foo() {

int a;
int b;

}

g2

g1

1

0

Offset

R4 →

Global Variables
int g1;
double g2;

int main() {
int x;
char y;
double z;
int a;

}
int foo() {

int a;
int b;

}

g2

g1R4 →

; while compiling main()…
; load from g1 into R1
LDR R1, R4, #0

; store from R0 into g1
STR R0, R4, #0

A variable does not have a label.

Will never use LD, LDI, ST, or STI
to access a variable.

Symbol Table
int g1;
double g2;

int main() {
int x;
char y;
double z;
int a;

}
int foo() {

int a;
int b;

}

Name Type Scope Offset
g1 int global 0

g2 double global 1

x int main 0

y char main -1

z double main -3

a int main -4

a int foo 0

b int foo -1

Code Generation Tip

Don’t clear a register before loading to it.
Just a waste of your time.

AND R0, R0, #0 ; clear R0
LDR R0, R5, #0 ; a

Expression
int g1;
double g2;

int foo() {
int a;
int b;

}

a + b

LDR R0, R5, #0 ; a
LDR R1, R5, #-1 ; b
ADD R0, R0, R1 ; a + b

1. Load variables into registers.
2. Use LC-3 instructions to perform operation.
3. Result will end up in a register.

Expression
int g1;
double g2;

int foo() {
int a;
int b;

}

a – g1

LDR R0, R5, #0 ; a
LDR R1, R4, #0 ; g1
NOT R1, R1
ADD R1, R1, #1 ; -g1
ADD R0, R0, R1 ; a – g1

1. Load variables into registers.
2. Use LC-3 instructions to perform operation.
3. Result will end up in a register.

Expression
int g1;
double g2;

int foo() {
int a;
int b;

}

a – 5

LDR R0, R5, #0 ; a
ADD R0, R0, #-5 ; a + (-5)

1. Load variables into registers.
2. Use LC-3 instructions to perform operation.
3. Result will end up in a register.

Small literal (≤ 5 bits):
Use immediate operand.

Expression
int g1;
double g2;

int foo() {
int a;
int b;

}

a – 5000

LDR R0, R5, #0 ; a
LD R1, fiveK ; -5000
ADD R0, R0, R1

fiveK .FILL #-5000

1. Load variables into registers.
2. Use LC-3 instructions to perform operation.
3. Result will end up in a register.

Large literal (> 5 bits):
Add value to code (.FILL)
LD into a register

Code Generation Tip

If subtracting a literal value,
use a negative literal.

ADD R0, R0, #-10 ; subtract 10

Code Generation Tip

If subtracting a literal value,
use a negative literal.

LD R1, val
NOT R1, R1
ADD R1, R1, #1
ADD R0, R0, R1

val .FILL #100

LD R1, val
ADD R0, R0, R1

val .FILL #-100

Side Effect: Assignment
int g1;
double g2;

int foo() {
int a;
int b;

}

a = 3

AND R0, R0, #0
ADD R0, R0, #3 ; R0 <- 3 STR R0, R5, #0 ; a <- R0 1. Evaluate expression on right. 2. Store to variable on left. Side Effect: Assignment int g1; double g2; int foo() { int a; int b; ... } b += a - 1 LDR R0, R5, #0 ; a ADD R0, R0, #-1 ; a-1 LDR R1, R5, #-1 ; b ADD R0, R0, R1 ; b + (a-1) STR R0, R5, #-1 ; b <- R0 1. Evaluate expression on right. 2. Store to variable on left. Side Effect: Post-Increment int g1; double g2; int foo() { int a; int b; ... } a++ LDR R0, R5, #0 ; a ADD R1, R0, #1 ; a + 1 STR R1, R5, #0 ; store a ; old value is in R0 1. Load variable 2. Use a different register to increment and store Don't forget to store! Side Effect: Pre-Increment int g1; double g2; int foo() { int a; int b; ... } ++a LDR R0, R5, #0 ; a ADD R0, R0, #1 ; a + 1 STR R0, R5, #0 ; store a ; incremented value is in R0 1. Load variable 2. Use same register to increment and store Don't forget to store! Code Generation Tip Store when evaluating ++ or --. Don't wait. You might forget. ; a = c + b++ LDR R0, R5, #-1 ; b ADD R1, R0, #1 ; b++ STR R1, R5, #-1 ; store b LDR R1, R5, #-2 ; c ADD R0, R0, R1 ; c + old b STR R0, R5, #0 ; store to a Arithmetic and Logic Operators Operator LC-3 Code + ADD - NOT, ADD +1, ADD *, /, % will not ask & AND | NOT, NOT, AND, NOT ^ (NOT, AND), (NOT, AND), OR ~ NOT << ADD register to itself (DO NOT STORE!!!) >> will not ask

Relational Operators
a < b Equivalent to: a - b < 0 1. Subtract 2. LC-3 condition code bits: less than zero (N), zero (Z), or greater than zero (P). LDR R0, R5, #0 ; a LDR R1, R5, #-1 ; b NOT R1, R1 ADD R1, R1, #1 ADD R0, R0, R1 ; a - b -- is N bit set? Relational Operators a < b Equivalent to: a - b < 0 If you need a value (rare), put 1 (true) or 0 (false) into a register. If used in a loop or if-statement, use condition code bit to perform appropriate branch (BR). No need to actually create a 1 or 0 value in that case. Code Generation Tip Remember that condition code is set for every LDR, ADD, AND, NOT. ; a != 0 LDR R0, R5, #0 ; a ; N,P bit is set if non-zero Logical Operators Typically used to combine relational expressions, in the context of an if-statement or loop. if ((a < b) && (b > 100)) …

Also, || and && have special ordering properties (short cuts).

Let’s postpone discussion until we’ve talked about branches.

Lecture 9: LC-3 Variables and Expressions
Slide Number 2
LC-3 Processor
LC-3 Processor
Slide Number 6
Slide Number 7
Local Variables
Local Variables
Global Variables
Global Variables
Slide Number 12
Symbol Table
Slide Number 17
Expression
Expression
Expression
Expression
Slide Number 23
Slide Number 24
Side Effect: Assignment
Side Effect: Assignment
Side Effect: Post-Increment
Side Effect: Pre-Increment
Slide Number 30
Arithmetic and Logic Operators
Relational Operators
Relational Operators
Slide Number 35
Logical Operators