In generic terms the compression ratio is the total cylinder volume with piston at bottom dead center divided by the volume above the piston at TDC.
Using a 440 as an example:
The block height from crankshaft centerline to the top of the deck surface is "speified" to be 10.725". It is unlikely the true deck height is "exactly" that measurement, but it gives a starting point of how much length the crank stroke, connecting rod and piston and take up.
Again as an example, stock 440 crank with 3.75" stroke, when at TDC is 1/2 the stroke length, or 1.875" (at center of pin). To have the top of the piston at zero deck height the rod length + piston pin to top of piston combined needs to be 10.725" - 1.875" = 8.850" or less or the piston top will be above the top of the engine block at TDC.
The specified 440 rod length (center of crank pin to center of piston pin) is 6.768", but again this may vary slightly and is used to get an estimate for our calculations. Subtracting the rod length, 8.850" - 6.768" = 2.082". This would be from the center of the piston pin to the top of the engine deck. A piston with 2.082" would theoretically pit the piston at zero deck height at TDC. I'm just mentioning these measurements because they come in handy when working out stroker engine combinations.
Looking at some common piston compression heights (center of pin to top of piston, but not including dome if equipped), a stock low compression piston has a height of 1.912". This would result in the piston being 0.170" below the engine deck at TDC. This additional volume would be added to the volume above the piston that is used to divide the total volume from, resulting in a lower compression ratio.
Many performance 440 pistons have a compression height greater than 2.06x", an example with a higher height would be the Keith black pistons with a 2.067", which if the block height and rod length were "spec", sit just 0.015" below the engine deck.
Note that the engine block deck may not be at spec, and it also may not be square to the Crank center line. This can cause different values front to back and side to side.
I usually have the machine shop "square" deck the block so all these distances are equal.
The piston to deck and gasket thickness also are used to calculate "quench" distance between the piston top and cylinder head. With a closed chamber head and flat top pistons, it is pretty easy to calculate, but with open chamber heads and dome or reverse dome "quench" pistons you have to account for the chamber to head deck and the dome clearance.
This is just showing how piston height can change compression ratio, but also starts to show the total stack up of what will actually fit in the block.
You could use different rod lengths to change where the piston sits at TDC relative to the block deck, This would be the same as changing piston height at far as calculating compression, the the change in rod length will also change the rod to crank ratio. Not going to get into that as were talking really small changes, but it does change things besides the compression ratio.
There is also the case of using s shorter piston or rod, and using a longer stroke crank. This makes a big difference in compression ratio (and some changes to rod ratio), because now the piston swept volume has changed (The engines displacement), not just where the piston sits relative to block deck height.
So... Getting to the calculating of compression.
Compression is Total cylinder volume / Volume above the piston at TDC.
Total cylinder volume is piston swept volume + Volume above the piston at TDC
The formula for the volume of a cylinder is V = pi * radius^2 * height.
The swept volume is just the volume that the piston displaces from TDC to BDC, so in the equation, V = 3.14 * (Bore/2)^2 * Stroke, This is often simplified as pi/4 * Bore^2 * Stroke, or V=0.7854 * Bore * Bore * Stroke. For a stock 440 bore of 4.320" and Stroke of 3.750" the swept volume of one cylinder is 54.97 ci.
Not compression related, but Multiply by 8 cylinders to get total engine displacement = 439.72"
Because the cylinder volume is usually measured in cubic centimeters (cc), we need the conversion factor from cc to cubic inches (ci), or convert the other way of ci to cc so we are working with the same units of measurement.
cc to ci, ci = cc * 0.0610237
ci to cc, cc = ci * 16.3871
We have the piston swept volume of 54.97 ci = 900.72 cc
The volume above the piston at TDC will be the volume that the piston sits below the deck (or subtracted if above deck) calculated the same as above, just where stroke was before, just use the piston the deck distance. With the KB 2.067" piston that distance is 0.015". so that volume is 0.2199 ci or 3.603 cc
Then the head gasket. Sometimes you can get the volume from the manufacturer, but a close estimate is to again use the cylinder volume equation.
I usually use 4.410" for the "bore" diameter of the head gasket and then the gasket thickness. This equates to 5.007 cc for a 0.020" head gasket and 10.014 cc for a 0.040" head gasket.
Factory cylinder head volumes are usually much larger than the specs. They really need to be measured to know for sure. For this example I'll use 89cc
The pistons usually have valve reliefs which add to the volume or domes which subtract from the volume. A KB237 flat top has 5cc valve reliefs (from KB, not measured.)
Some calculator also account for the small volume around the piston to wall clearance down to the first cylinder ring. That is usually going to be pretty small, and I don't bother calculating it.
So Total Volume = Swept + piston to deck + head gasket + cylinder head volume. 900.72 + 3.603 + 5.007 + 89 .0 + 5 = 1003.33
Volume above piston at TDC = 1003.33 - 900.72 = 102.61
Compression ratio = 1003.33 / 102.61 = 9.778:1