13. Trees 3

13. Trees 3 - Maggie Johnson Handout #13 CS103B Trees:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Maggie Johnson Handout #13 CS103B Trees: Applications Key Topics * A Computer's Language * Generating Assembly Code from Binary Trees * File System Design * FATs and Inodes * Decision Trees _________________________________________________________________________________ A Computer's Language At the lowest level, the only language that a computer understands is 0's and 1's. Any program written in a language like C or Java, must be translated (using a compiler) to 0's and 1's in order to run. Machine language instructions (which is another name for the 0's and 1's) control the computer at the hardware level. These instructions tell the computer when to transfer data in and out of memory cells, and what basic operations to perform on them. Thus, a simple statement from a C program like "for (i=0; i<n; i++)" would translate into several machine language instructions. In the early days of computers, programmers found it very cumbersome to program using machine language. So, they created a language that was a little more English-like. This language is called assembly language. It consists of instructions like "LOAD" or "ADD". It is easier to work with than machine language, but its basic structure is the same. Each machine language instruction translates into one assembly language instruction. For example, consider a machine for which we are writing assembly language. It has several registers and the following instructions. A register is special quick-access memory where the processor stores the results of current arithmetic and compare operations. LOAD X, RY Place the value of variable X in register Y ADD RX, RY Add the value of register X to register Y and store result in register Y SUB RX, RY Subtract the value of register X from register Y; store result in register Y MUL RX, RY Multiply the value of register X by register Y; store result in register Y DIV RX, RY Divide the value of register X by register Y and store result in register Y For example, LOAD 2, R0 LOAD 3, R1 ADD R0, R1 will place 5 in register R1 Generating Assembly Code Directly from Binary Trees
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
We are going to write some functions that will generate assembly language code for evaluating an arithmetic expression stored in a binary tree. We will assume that the data is already in the tree, so we won't discuss at this point how to translate an expression into a binary tree. The nodes of the expression tree will need to store two elements: a label indicating the variable stored in a node, and the height of the tree at that node. The height field is important because we will be using it to control which register we are using. To compute the height of each node of a binary tree, the following function can be called before calling the procedure to generate the assembly language. The height of each node in the tree will then be in place when needed. This function works recursively making the height of a leaf equal to 0, and the height of each node one more than the larger of the heights of its children. Notice that this definition
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

13. Trees 3 - Maggie Johnson Handout #13 CS103B Trees:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online