Presentation is loading. Please wait.

Presentation is loading. Please wait.

Translating high level language into machine code

Similar presentations


Presentation on theme: "Translating high level language into machine code"— Presentation transcript:

1 Translating high level language into machine code
XToy Programming Translating high level language into machine code

2 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 = D // saved in memory location 02 Machine code High level language 00: 01: 02: 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: halt 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 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

3 // Input: Sequence of non-zero integers, followed by 0000
program Sum // Input: Sequence of non-zero integers, followed by 0000 // Output: The sum of all the integers High level language Machine code 1) sum = 0 ; 2) while (true) 3) { 4) read a ; 5) if (a == 0) break ; 6) sum = sum + a ; 7) } 8) write sum ; 10: 7C00 RC <- 0000 11: 8AFF read RA 12: CA15 if (RA == 0) pc <- 15 13: 1CCA RC <- RC + RA 14: C pc <- 11 15: 9CFF write RC 16: 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 7) Jump back to line 2 8) When we do finish we print out answer to user

4 Now we can see the benefits of “pseudo-code”.
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: C pc <- 14 18: 9C0C mem[0C] <- RC 19: halt

5 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: R[2] <- R[0] + R[1]

6 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.

7 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

8 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.

9 Reads in a sequence of integers and prints them in reverse order.


Download ppt "Translating high level language into machine code"

Similar presentations


Ads by Google