Today’s topics Machine Architecture Upcoming Reading

advertisement
Today’s topics
Machine Architecture
More Low-level Programming
Upcoming
Language Translation (G.I. Chapter 9)
Reading
Great Ideas, Chapters 8
CPS 001
24.1
Programming Loops


Now use new instructions to do the equivalent of while
We noted that syntax for if and while were same




Assembler code surprisingly similar for these two
Major addition is the update
Also need jump back to beginning of loop
Demonstrate with code equivalent to:
{
limit = 0;
sum = 0;
x = a.getInt();
while (limit < x)
{
sum = (sum + x);
x = a.getInt();
}
b.setInt(sum);
}
CPS 001
24.2
summer.as
0
copy
1
copy
2
copy
3
copy
4
in
5
copy
6 #L0 copy
7
cmp
8
jnb
9
copy
10
add
11
copy
12
in
13
copy
14
jmp
15 #L1 copy
16
out
CPS 001
ax, #C0
limit, ax
ax, #C0
sum, ax
ax
x, ax
ax, limit
ax, x
#L1
ax, sum
ax, x
sum, ax
ax
x, ax
#L0
ax, sum
ax
40
41
42
43
limit
#C0
sum
x
0
0
0
0
Notes:
#L0=6
#L1=15
24.3
Another looping example


Calculate N! (N factorial) but do it with a loop this
time
Code is equivalent to the following Java:
{
n = a.getInt();
i = 1;
fact = 1;
while (i < n+1)
{
fact = (fact * I);
i = (i + 1);
}
b.setInt(fact);
}
CPS 001
24.4
fact.as
1
in
2
copy
3
copy
4
copy
5
copy
6 #L0 copy
7
add
8
copy
9
copy
10
cmp
11
jnb
12
copy
13
mult
14
copy
15
copy
16
add
CPS 001
ax
n, ax
ax, #C1
i, ax
fact, ax
ax, n
ax, #C1
E0, ax
ax, i
ax, E0
#L1
ax, fact
ax, i
fact, ax
ax, i
ax, #c1
17
18
19 #L1
20
21 halt
copy
jmp
copy
out
40
41
42
43
44
0
0
1
0
0
n
i
#C1
fact
E0
i, ax
#L0
ax, fact
ax
Notes:
#L0=6
#L1=19
24.5
Assembler Programming Notes

Note that previous program added the mul instruction



The best way to follow such a program is by tracing


Most hardware has standard arithmetic support
Historically not the case
See trace for fact.as program on web page
Writing assembler programs from scratch




CPS 001
Not that hard
Can get quite used to working at this level
Was done for efficiency reasons
o Could do better than automatic translation (e.g., compiler)
However, remember 15 lines of code a day
o This figure is language independent!
o Compilers have gotten better than the average programmer
24.6
Handling List or Arrays

Need extra hardware to do this well



Can be done with our limited hardware




Have registers that point to the list/array
Increment these registers to step through list/array
Involves having the program modify itself
Not hard to write
Errors in such self-modifying code very hard to find!
Additional Features Desired (minimal upgrade)


CPS 001
Need for more registers
Handling function/method calls
o Need to “remember” where you came from
o Jump to statement after that when done
24.7
Modern Hardware

Memory Size



Lots of Registers



PC’s often have gigabyte of memory now
What does this do to the size of the instruction?
It is not unusual to have 32 accumulators
What does this do to the size of the instruction?
Memory Hierarchy
1.
2.
3.
4.
5.
CPS 001
Registers
Cache Memory
Main Memory
Disk (virtual memory)
Offline storage (tapes, CDROMs, DVDs, etc.)
24.8
Download