Download XToy Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
XToy Programming
Translating high level language
into machine code
program Add
// Input: Stored in memory location 00 and 01
// Output: Sum of two integers 5 + 8 = D
// saved in memory location 02
High level language
1) int a = 8;
2) int b = 5;
3) int c = a + b;
0)
8 has to be in memory
0)
5 has to be in memory
Machine code
•00: 0008
8
•01: 0005
5
•02: 0000
0
•10: 8A00
R[A] <- mem[00]
•11: 8B01
R[B] <- mem[01]
•12: 1CAB
R[C] <- R[A] + R[B]
•13: 9C02
mem[02] <- R[C]
•14: 0000
halt
1) put contents of memory location 00 into register A
2) put contents of memory location 01 into register B
3) put results of addition into register C
0) save contents of register C into memory location 2
program Sum
// Input: Sequence of non-zero integers, followed by 0000
// Output: The sum of all the integers
High level language
1) sum = 0 ;
2) while (true)
3) {
4)
5)
6)
read a ;
if (a == 0) break ;
sum = sum + a ;
7) }
Machine code
•10: 7C00
•11: 8AFF
•12: CA15
•13: 1CCA
•14: C011
•15: 9CFF
•16: 0000
RC <- 0000
read RA
if (RA == 0) pc <- 15
RC <- RC + RA
pc <- 11
write RC
halt
8) write sum ;
1) We could store 0 in a register (e.g. C)
2) Its an infinite loop so we must come back here when line 7 is executed
4) Read in a value from stdin and save in register A
5) If the value we read in is zero .. then finish .. ie jump to line 8
6) Add contents of register A to the current value in register C
7) Jump back to line 2
8) When we do finish we print out answer to user
Now we can see the benefits of “pseudo-code”.
•0A: 0003
•0B: 0009
•0C: 0000
3
9
0
•0D: 0000
•0E: 0001
0
1
•10: 8A0A
•11: 8B0B
•12: 8C0D
•13: 810E
RA <- mem[0A]
RB <- mem[0B]
RC <- mem[0D]
R1 <- mem[0E]
•14: CA18
•15: 1CCB
•16: 2AA1
•17: C014
•18: 9C0C
•19: 0000
if (RA == 0) pc goto 18
RC <- RC + RB
RA <- RA - R1
pc <- 14
mem[0C] <- RC
halt
TOY idioms
• Register-to-register transfer.
– Suppose you want to make register 2 have
the same value as register 1.
– There is no built-in instruction to do this.
– Relying on the fact that register 0 always
contains 0000, we can use the addition
instruction to sum up R0 and R1 and put the
result in R2.
10: 1201
R[2] <- R[0] + R[1]
TOY idioms
• No-op
– In a structured programming language like Java inserting extra code is
easy.
– But in an unstructured language like TOY (where there are line numbers
and goto statements), you must be careful about inserting code.
– A branch statement hardwires in the memory address to jump to;
• if you insert code, the line numbers of your program may change.
• To avoid some of this awkwardness, machine language programmers often
find it convenient to fill in the program with "useless" statements to act as
placeholders.
– Such statements are called no-ops because they perform no operation.
– The instruction 1000 is ideal for this purpose since register 0 is always 0
anyway. (The instruction 10xy would also be a no-op for any values of x
and y because register 0 always contains 0, regardless of how you might
try to change it.
TOY idioms
• Goto
– There is no instruction that directly changes the
program counter to the particular address.
• Like a GOTO statement
– However, it is easy to use the “branch if zero”
instruction with register 0 to achieve the same effect.
– For example, the instruction C0F0 changes the
program counter to F0 since register 0 is always 0.
• So its like saying GOTO FO
Arrays
• Arrays are not directly built into the TOY
language, but it is possible to achieve the same
functionality using the
– load address, load indirect, and store indirect
instructions.
• We consider a program that reads in a sequence
of integers and prints them in reverse order.
Reads in a sequence of integers and prints them in reverse order.