Try simplifying, this makes it a lot easier to get a fix if there is an issue.
What is important, do you use trunc compiler or official compiler, which optimization level do you use?
For the following I am now using trunk compiler from this week and O2 because it makes (in this case) the generated code a little easier to read.
Most simple version of a test would be:
program test;
var
size : byte;
begin
size := 16;
size := size div 2;
end.
then compile this and make sure you have assembler output enabled:
/usr/local/bin/ppcarm -O2 -Mobjfpc -Tembedded -al test.pas -WpLPC1114FBD48_302 -CpARMV6M
and then have a look at the generated code:
# Var size located in register r1
# [5] size := 16;
mov r1,#16
# [6] size := size div 2;
mov r0,r1
lsr r0,r0,#31
add r0,r1,r0
asr r0,r0,#1
uxtb r0,r0
# Var size located in register r0
You can also run this code in gdb or ozone to see if r0 has correct value in the end. (for me it has)
Compiled with trunk compiler the code seems to do what I'd expect. Not the most optimized code for the situation but technically OK
Look as the assembler code that was generated by your code now, if the code for div looks very differnet then modify the most simple example to match the exact problem.
Then you will be able to properly judge if you found a compiler error or a programming error.