Inside the Einstein: The Disc, a TatungBytes course: XtalDOS listing the first practice disc image, on a rainbow sunburst
← Back to Modules
module
4

Numbers, And Names For Them

Introduction

Everything on a disc is a number. The files, the names of the files, the list of where the files are - all of it, in the end, is a long row of small numbers, and this course is about knowing which number means what. Before you can do that, you need to be able to work with numbers in Python: give them names, do sums with them, and write them the way people who work with discs write them.

Names

In S2 you printed 40 straight into a sentence. A program that is going to work things out needs to remember numbers, and for that you give them names:

tracks = 40

This means let tracks be 40. From then on, wherever you write tracks, Python uses 40. A name like this is called a variable. It is not an equation - = here means "make this name stand for this value" - and you can change it later by giving it a new value.

Names can be any word you like, made of letters, digits and _ (not starting with a digit, and no spaces). Choose names that say what the number is: sector_size is far easier to read in a month's time than s or x.

Whole-Number Arithmetic

Python writes the four sums as +, -, * (times) and / (divide) - but this course hardly ever uses /, because it gives a number with a decimal point, and a disc never has half a sector. Instead it uses two operators that keep to whole numbers:

  • // divides and throws away the remainder: 1000 // 512 is 1.
  • % gives only the remainder: 1000 % 512 is 488.

Together they answer the question you will ask over and over: "how many whole ones of these, and how much is left over?" A thousand bytes is one whole 512-byte sector with 488 bytes over.

What A Byte Is

A byte is the smallest number a disc stores: a whole number from 0 to 255. That is all it is. 256 different values, because a byte is made of eight bits, each of which is 0 or 1, and eight bits can be arranged in 256 ways.

Whether a byte means a letter, a part of a larger number, or an instruction for the Einstein's processor depends entirely on where it is and what reads it. The byte itself is just a number from 0 to 255.

Hexadecimal

People who work with bytes usually write them in hexadecimal - hex - which counts in sixteens instead of tens. It needs sixteen digits, so after 0-9 come the letters A-F:

decimal:  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
hex:      0 1 2 3 4 5 6 7 8 9  A  B  C  D  E  F 10 11 ...

Why bother? Because every byte is exactly two hex digits, from 00 to FF. 255 is FF; 229 is E5; 16 is 10. Rows of bytes written as pairs of hex digits line up neatly and are quick to read once you are used to them - and you will be, by the end of Part one.

In Python you write a hex number with 0x in front: 0xE5 is the same number as 229, typed differently. To go the other way, hex(229) gives the text 0xe5. Python does not mind whether you write letters in hex as capitals or small letters.

You will meet E5 a great deal: it is the value every byte of a blank Einstein disc is filled with.

The code

Save this as numbers.py in your edisc folder.

tracks = 40
sectors = 10
sector_size = 512

total = tracks * sectors * sector_size
print("Bytes on a disc:", total)
print("In kilobytes:", total // 1024)

print("1000 bytes is", 1000 // 512, "whole sector and", 1000 % 512, "bytes over")

print(229, hex(229), 0xE5)
print(0xFF, 0x100)

Run it

python3 numbers.py

What you should see

Bytes on a disc: 204800
In kilobytes: 200
1000 bytes is 1 whole sector and 488 bytes over
229 0xe5 229
255 256

The first three lines give the disc's shape names. total is worked out from them, once, and printed. A kilobyte - the "k" the Einstein uses when it tells you about space - is 1,024 bytes, so total // 1024 is the disc's size in K: 200.

The last two lines are hex. hex(229) makes 0xe5; 0xE5 is just 229. 0xFF is 255, the largest number one byte can hold, and 0x100 - one more - is 256, which is too big for a byte.

Change one thing

  • Change sectors = 10 to sectors = 20. Which lines of output change, and why do the others not?
  • Change 1000 (both of them) to 1024. How many whole sectors now, and how much over? Then try 512.
  • Add print(hex(204800)). What is the disc's size in hex?
  • Add print(0x15 * 256). You will meet the answer again in S8.

When it goes wrong

SymptomCause
NameError: name 'sector_size' is not definedA name used before it was given a value, or spelt differently in two places - Python treats sector_size and Sector_size as different names
A number with .0 on the end, like 200.0/ was used instead of //

Summary

You can now give numbers names, do whole-number arithmetic with // and %, and read and write hex. A byte is a number from 0 to 255 - two hex digits, 00 to FF - and nothing more until something decides what it means.

Next

S5, Making A Byte - putting bytes into Python, and into a file, and changing a byte of a real disc image.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.