Science Weblecture for Unit 66
This Unit's | Homework Page | History Lecture | Science Lecture | Lab | Parents' Notes |
In order for a computer to perform any activity, we need to have a way to talk to it. We need to know how computers actually store information (binary code), how chips talk to computers (assembly code), and how computer languages talk to assembly code ( "a = 2a;").
Computers use chips or hard-coded, built-in instruction sets that allow them to perform operations on values stored in binary code in specific locations in memory (called registers).
Machine code is binary code, represented by a string of 1s and 0s to make up numbers. In base ten, we use the rightmost value in a number to represent the number of units (1s) we have, the next value to the left represents the number of 10s we have, the next number to the left represents the number of 100s...and so on. In base two (binary), we use the rightmost value in a number to represent the number of units (1s) we have, the next value to the left represents the number of 2s we have, the next number to the left represents the number of 4s...and so on. As a shorthand, computer engineers sometimes write values in hexadecimal, a number system based on 16.
Base 10 Place Values | 103 | 102 | 101 | 100 | Value |
Base 10 Example | 4 | 6 | 7 | 2 | 4 * 103 + 6 * 102 + 7 * 101 + 2 * 100 4 thousands + 6 hundreds + 7 tens + 2 ones 4672 Highest possible value: 9999 |
Base 2 Place Values | 23 | 22 | 21 | 20 | Value |
Base 2 Example | 1 | 0 | 1 | 1 | 1 * 23 + 0 * 22 + 1 * 2 1 + 1 * 20 1 eights + 0 fours + 1 two + 1 one 11 Highest possible value: 15 |
Hexadecimal Values | 161 | 160 | Value | ||
Hexadecimal | 7 | F | 7 * 16 1 + [F=15] * 160 7 sixteens + 15 ones 112+ 15 = 127 Highest possible value (in 2 digits): 255 |
For people, binary code is incredibly cumbersome, but machines which use electrical signals or magnetic fields that are either "on" or "off" can easily use binary representation of numbers.
Computer chips, such as the 8080 chip, which was the original one used in personal computers, the Intel chip, and the RISC chip, map specific binary values to a particular instruction. In Z80 code, the Hex instruction "87" (8*16 + 7 = [base 10] 135 = [binary] 10000111 means "take the value in register A, add it to itself, and store the result in register A".
This brings us the interesting problem of "assignment", which is a basic part of programming. The expression
a = 2a
it makes no sense algebraically. It's a logical contradiction for any value for a other than 0. But on a Z80 programming, the BASIC language instruction "a = 2a" would be interpreted as bit instruction 87 -- take the value in location a, add it to itself, and stick the result back in location a.
Take a look at the instruction set for the 8080/Z80 chip at the Nemesis site. This is a collection of information on 8080 assembly language, which is still used on older computers.
Consider the following problem: You don't have a computer or calculator handy, but you need to know the square root of 734. There's way to find square roots that was used in the dark ages before calculators:
We first make a guess -- any number will do. | Let's make an educated guess of 25, since the square root has to be at least 25 (252 = 625) |
Divide our square by our guess. | 734/25 = 29.36 |
Now we take the average of the difference between our first guess (25) and this result (29.36) | (29.36 + 25)/2 = 27.18 |
We use this for our new "guess" | 734/27.18 = 27.005 |
Now we take the average of the difference between our this guess (27.18) and this result (27.005) | (27.18 + 27.005)/2 = 27.09 |
We use this for our new "guess" | 734/27.09 = 27.09 |
We just got the same answer, so at two decimal places, the square root of 734 is 27.09. If we square 27.09, we get 733.86, which is 0.14/734 or 0.00018. That means our estimate is off by less than 0.02%. It isn't mathematically exact. If we had kept better track of the values to more decimal places we could keep going and get a more accurate value for √(734).
What we do have is an algorithm or method.
Here we want the difference between our guess and our result to be accurate when the values are rounded to 2 decimal places. So our check is to subtract the new guess from the result and see if the remainder is small enough.
Now we can try to write this as a set of instructions to a program. We need two starting values, our square and our guess.
In a language like Perl (the duct tape that holds the Internet together), the code will look like this:
$square = 734;
$guess = 25;
$result = 0;
while ($guess - $result > 0.01) {
$result = $square/$guess;
$guess = ($guess + $result)/2;
}
print $result;
The $ (dollar sign) tells the Perl interpreter that the string is the name of a variable, and not a keyword in the language, like "while" in the loop. The brackets mark the beginning and end of statements in the loop. The semi-colon tells the interpreter when the statement ends; long statements or lists of values may cross multiple lines of the text file containing the code.
© 2005 - 2025 This course is offered through Scholars Online, a non-profit organization supporting classical Christian education through online courses. Permission to copy course content (lessons and labs) for personal study is granted to students currently or formerly enrolled in the course through Scholars Online. Reproduction for any other purpose, without the express written consent of the author, is prohibited.