The HD44780 does not "know" how many lines are on the display! If you move the cursor to character cell 20, that's the first character cell of the second row. Character 40: first cell of third row; character 60: first cell of fourth row. The HD44780 only has 80 character cells, no matter what size LCD is connected.
This is not quite correct. The first row starts at offset 0, that's true. But the second (third for four-line displays) row starts at offset 64! On four-line displays row two starts at offset 20, and row four starts at 84. I'm sure there's a good reason for doing it like this...
The code for positioning the cursor would be something like...
void gotoxy(int x, int y)
{
int offset[] = {0, 64, 20, 84};
commandwrite(0x80 | (offset[y] + x));
}
IIRC this is how it's done in LiquidCrystal.