There you go, I was able to convert your 2D example into 3D, but this implementation has a structure and two subroutines and uses recursion.

It is possible to go without all these of course, but like i said I don't like that kind of coding.
program cubes_v2;
const max_side=40;
var i,x,y,z:byte; { counter + sides of the hexahedron }
v:array[1..max_side] of word; { Structure to hold number of cut out cubes for each size }
n:word; {no. of cubes cut}
procedure sort_(var a,b,c:byte); { Sorts 3 values in increasing order }
procedure swap(var d,e:byte); { Swaps two values }
begin
d:=d xor e; { fast bitwise swap }
e:=d xor e;
d:=d xor e;
end;
begin
if a>b then swap(a,b);
if b>c then swap(b,c);
if a>b then swap(a,b);
end;
(*I realized, this algorithm doesn't work in all cases
procedure cutout_cube(a,b,c:byte);
begin
sort_(a,b,c); { Sort sides, in increasing order, a = smallest }
if a>0 then begin { If a=0 then exit, no point slicing further }
v[a]:=v[a]+((b div a)*(c div a)); { Calc. how many "a" sized squares are possible to cut out }
cutout_cube(a,a,c mod a); {* Divide the remaining 3D "L" shape into two heaxahedrons }
cutout_cube(a,b mod a,c); {* Recursive call on both hexahedrons for further cube cutting }
end;
end;
*)
procedure cutout_cube(trig:boolean;a,b,c:byte);
var x,y:word;
begin
sort_(a,b,c); { Sort sides, in increasing order, a = smallest }
if a>0 then begin { If a=0 then exit, no point slicing further }
x:=b div a;
y:=c div a;;
v[a]:=v[a]+x*y;
if trig then begin
cutout_cube(trig,a,a,c mod a);
cutout_cube(trig,a,b mod a,c);
end else begin
if ((c mod a =0) and (y>0)) then cutout_cube(trig,a,c,b mod a) else
if ((b mod a =0) and (x>0)) then cutout_cube(trig,a,b,c mod a) else
begin
cutout_cube(trig,a,a,b mod a);
cutout_cube(trig,a,b,c mod a);
end;
end;end;
end;
begin
fillchar(v,sizeof(v),0); { init v with 0's, i.e. no cubes cut out }
repeat
write('x= ');readln(x);
write('y= ');readln(y);
write('z= ');readln(z);
writeln;
until ((x in[1..max_side]) and (y in[1..max_side]) and (z in[1..max_side]));
sort_(x,y,z);
cutout_cube(y=z,x,y,z); { Start slicing }
n:=0; {init n}
for i:=max_side downto 1 do
if v[i]>0 then begin
writeln(i,'x',i,'x',i,' cubes: ',v[i]); { Display cut out cubes }
inc(n,v[i]);
end;
writeln(#13#10,'Total cubes : ',n);
writeln(#13#10,'Total volume: ',word(x*y*z)); { Display total 1x1x1 cubers ( volume ) }
readln;
end.