1

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:00020F89                 add     eax, ebx
cseg01:00020F8B                 mov     ebx, [edx]
cseg01:00020F8D                 mov     ecx, [edx+8]
cseg01:00020F90                 add     ebx, eax
cseg01:00020F92                 imul    eax, ecx, 0DD7h
cseg01:00020F98                 add     eax, ebx

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.

2
  • 1
    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.
    – black4
    Feb 24 at 0:04

1 Answer 1

3

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.

2
  • 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.
    – WhatsUp
    Feb 20 at 16:18
  • Thank you very much!
    – black4
    Feb 24 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.