View
223
Download
4
Category
Preview:
Citation preview
XToy Programming
Translating high level language
into machine code
1) int a = 8;
2) int b = 5;
3) int c = a + b;
program Add// Input: Stored in memory location 00 and 01 // Output: Sum of two integers 5 + 8 = D // saved in memory location 02
•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
•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
0) 8 has to be in memory
0) 5 has to be in memory
1) put contents of memory location 00 into register A
High level language Machine code
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
1) sum = 0 ;2) while (true) 3) {
4) read a ; 5) if (a == 0) break ; 6) sum = sum + a ;
7) }
8) write sum ;
program Sum// Input: Sequence of non-zero integers, followed by 0000// Output: The sum of all the integers
High level language
•10: 7C00 RC <- 0000 •11: 8AFF read RA•12: CA15 if (RA == 0) pc <- 15 •13: 1CCA RC <- RC + RA •14: C011 pc <- 11 •15: 9CFF write RC •16: 0000 halt
•10: 7C00 RC <- 0000 •11: 8AFF read RA•12: CA15 if (RA == 0) pc <- 15 •13: 1CCA RC <- RC + RA •14: C011 pc <- 11 •15: 9CFF write RC •16: 0000 halt
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
8) When we do finish we print out answer to user7) Jump back to line 2
Machine code
•0A: 0003 3•0B: 0009 9•0C: 0000 0
•0D: 0000 0•0E: 0001 1
•10: 8A0A RA <- mem[0A] •11: 8B0B RB <- mem[0B] •12: 8C0D RC <- mem[0D] •13: 810E R1 <- mem[0E]
•14: CA18 if (RA == 0) pc goto 18•15: 1CCB RC <- RC + RB •16: 2AA1 RA <- RA - R1 •17: C014 pc <- 14 •18: 9C0C mem[0C] <- RC•19: 0000 halt
•0A: 0003 3•0B: 0009 9•0C: 0000 0
•0D: 0000 0•0E: 0001 1
•10: 8A0A RA <- mem[0A] •11: 8B0B RB <- mem[0B] •12: 8C0D RC <- mem[0D] •13: 810E R1 <- mem[0E]
•14: CA18 if (RA == 0) pc goto 18•15: 1CCB RC <- RC + RB •16: 2AA1 RA <- RA - R1 •17: C014 pc <- 14 •18: 9C0C mem[0C] <- RC•19: 0000 halt
Now we can see the benefits of “pseudo-code”.
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.
Recommended