CS计算机代考程序代写 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 10: LC-3 Statements

Announcements
Z5: 3/9
Next PS: 3/10

Photo by Sora Shimazaki on Pexels

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

Expression Statement

expr ; Semicolon required. Marks the end of the statement.

Treat each statement as independent.
Don’t rely on actions of other statements.
Don’t try to keep variable in register between statements.
Load each variable as needed, store each variable as directed.

Side effect = store

Running example…

int main() {
int a, b, c, d, e;

}

a

b

c

d

0

-1

-3

-4

Offset

R5 →

e

-2

Expression Statement

a = b + c;

LDR R0, R5, #-1 ; LOAD b
LDR R1, R5, #-2 ; LOAD c
ADD R0, R0, R1 ; COMPUTE b + c
STR R0, R5, #0 ; STORE to a

Expression Statement

a = b + c & 1 – d;

LDR R0, R5, #-3 ; d
NOT R0, R0
ADD R0, R0, #1 ; -d
ADD R0, R0, #1 ; (1 – d)
LDR R1, R5, #-2 ;c
LDR R2, R5, #-1 ;b
ADD R1, R1, R2 ; (b + c)

Must understand and follow precedence and associativity rules.
Order of evaluation doesn’t matter, as long as it follows rules.

AND R0, R0, R1 ;(b+c)&(1-d)
STR R0, R5, #0 ; -> a

Expression Statement

e = 3 << a; AND R0, R0, #0 ADD R0, R0, #3 ; R0 <- 3 LDR R1, R5, #0 ; a is number of times to shift BRnz L2 ; skip loop L1 ADD R0, R0, R0 ; << 1 ADD R1, R1, #-1 ; BRp L1 ; until R1 == 0 L2 STR R0, R5, #-4 ; STORE to e Add register to itself = single-bit left shift. (DOES NOT change variable.) Your turn... c = a + --b & 0xc; a b c d 0 -1 -3 -4 Offset R5 → e -2 If statement T F ; evaluate expression ; sets condition codes BRxxx NEXT ; branch if FALSE ; code for statement NEXT ... If statement if (a < 10) b += 2; LDR R0, R5, #0 ; a ADD R0, R0, #-10 ; (a-10) BRzp NEXT ; skip if a >= 10
LDR R0, R5, #-1 ; b
ADD R0, R0, #2
STR R0, R5, #-1 ; -> b

NEXT …

If-else statement
if (a < 10) b += 2; else b += 3; LDR R0, R5, #0 ; a ADD R0, R0, #-10 ; (a-10) BRzp ELSE ; skip if a >= 10
LDR R0, R5, #-1 ; b
ADD R0, R0, #2
STR R0, R5, #-1 ; -> b
BRnzp NEXT ; skip else part

ELSE LDR R0, R5, #-1
ADD R0, R0, #3
STR R0, R5, #-1

NEXT …

Your turn…

if (a > b)
c = a;

else
c = b; a

b

c

d

0

-1

-3

-4

Offset

R5 →

e

-2

While statement

T

F

LOOP ; evaluate expression
; sets condition codes

BRxxx NEXT
; branch if FALSE

; code for statement

NEXT …

BRnzp LOOP

While statement

while (a < 10) { b += 2; a--; } LOOP LDR R0, R5, #0 ; a ADD R0, R0, #-10 BRzp NEXT ; exit? LDR R0, R5, #-1 ADD R0, R0, #2 STR R0, R5, #-1 ; b += 2 LDR R0, R5, #0 ADD R1, R0, #-1 STR R1, R5, #0 ; a-- BRnzp LOOP NEXT ... For loop LOOP ; test expression ; sets condition codes BRxxx NEXT ; if exit ; code for statement NEXT ... BRnzp LOOP T F; init expression ; re-init expression For loop for (d=0; d<5; d++) { b = a & c; c <<= 1; } LDR R0, R5, #0 ; a LDR R1, R5, #-2 ; c AND R0, R0, R1 STR R0, R5, #-1 ; -> b
LDR R0, R5, #-2 ; c
ADD R0, R0, R0 ; << 1 STR R0, R5, #-2 ; -> c
LDR R0, R5, #-3 ; d
ADD R1, R0, #1 ; d++
STR R1, R5, #-3
BRnzp LOOP

NEXT …

AND R0, R0, #0
STR R0, R5, #-3 ; 0 -> d

LOOP LDR R0, R5, #-3
ADD R0, R0, #-5
BRzp NEXT ; exit?

Your turn…

for (a=e; a 1)

Logical:
must do left side first
if false, don’t evaluate right side
(shortcut!)

if ((b <= 10) && (d > 1)) a += 3;

LDR R0, R5, #-1 ; b
ADD R0, R0, #-10
BRp NEXT ; if > 10, skip statement
LDR R0, R5, #-3 ; d
ADD R0, R0, #-1
BRnz NEXT ; if <= 1, skip LDR R0, R5, #0 ADD R0, R0, #3 STR R0, R5, #0 NEXT if ((b <= 10) || (d > 1)) a += 3;

LDR R0, R5, #-1 ; b
ADD R0, R0, #-10
BRnz L1 ; if <= 10, do statement LDR R0, R5, #-3 ; d ADD R0, R0, #-1 BRnz NEXT ; if <= 1, skip L1 LDR R0, R5, #0 ADD R0, R0, #3 STR R0, R5, #0 NEXT How is this useful? if (a && (b/a > 6)) d += 10;

if ((a == 0) || (b/a > 6)) d += 10;

if (!a || (b/a > 6)) d += 10;

How is this useful?

if ((a > 100) && (b < sqrt(a)) d += 10; if ((a == b) || (b == c) || (c == d) || (d == e)) d += 10; Your turn... done = 0; for (a=1; !done && a