What this code do?

I decompile old dos game (WATCOM compiler). I do not understand code after calling localtime. What calculations performing after calling localtime?

``````cseg01:00020F60 sub_20F60       proc near               ; CODE XREF: sub_20D30+5p
cseg01:00020F60
cseg01:00020F60 var_14          = dword ptr -14h
cseg01:00020F60
cseg01:00020F60                 push    ebx
cseg01:00020F61                 push    ecx
cseg01:00020F62                 push    edx
cseg01:00020F63                 push    edi
cseg01:00020F64                 sub     esp, 4
cseg01:00020F67                 xor     eax, eax
cseg01:00020F69                 call    time_
cseg01:00020F6E                 mov     [esp+14h+var_14], eax
cseg01:00020F71                 mov     eax, esp
cseg01:00020F73                 call    localtime_
cseg01:00020F78                 mov     ebx, [eax+4]
cseg01:00020F7B                 mov     edx, eax
cseg01:00020F7D                 lea     eax, ds:0[ebx*8]
cseg01:00020F84                 sub     eax, ebx
cseg01:00020F86                 shl     eax, 3
cseg01:00020F8B                 mov     ebx, [edx]
cseg01:00020F8D                 mov     ecx, [edx+8]
cseg01:00020F92                 imul    eax, ecx, 0DD7h

``````

I tried to write pseudocode, there is my pseudocode:

``````    after calling localtime

ebx = minutes

eax = ebx * 8;

eax = eax - ebx;

eax = eax >> 3;

eax = eax + ebx;

ebx = seconds;

ecx = hours;

ebx = ebx + eax;

eax = ecx * 3543;

eax = eax + ebx;

``````

I do not understand this math stuff.

• What's the result used for? A randomness seed?
– Rup
Feb 20 at 1:53
• Yes, it's actually as turned out randomness seed, thank you. Feb 24 at 0:04

It looks roughly like

``````total_seconds = (minutes * 57) + (hours * 3543) + seconds
``````

Why that's not 60 seconds per minute or 3600 seconds per hour I don't know.

The minutes calculation is

• eax = (minutes * 8) - minutes = minutes * 7
• eax = eax << 3 (this is a left shift not right) = eax * 8 = minutes * 7 * 8 = minutes * 56
• eax = eax + minutes = minutes * 57

Again I'm not sure why this isn't just an imul. Maybe it was more efficient to do multiplications manually for multiplicands with only a few bits on some very old processors, but I'd be surprised.

• That's right. For old CPU's, multiplications could be so slow that it makes sense to replace them with shifts/adds/subs. Thus some compilers have this kind of optimizations. The aim of the whole thing could be e.g. setting the random seed. Feb 20 at 16:18
• Thank you very much! Feb 24 at 0:09