Turbo Pascal

Turbo Pascal was written by Borland way back when I first started programming. I played a game back then called Trade Wars 2002 and I recently rediscovered it. I read some forums about how certain days seem to be "better" for trading than others and it smacked of a crappy random number generator.

I disassembled a program written in Turbo Pascal and here is the assembly for the random functions:

; Randomize(void)
@Randomize$qv proc far
mov     ah, 2Ch
int     21h             ; DOS - GET CURRENT TIME
                        ; Return: CH = hours, CL = minutes, DH = seconds
                        ; DL = hundredths of seconds
mov     word ptr RandSeed, cx
mov     word ptr RandSeed+2, dx
retf
@Randomize$qv endp

; Random(Word)
@Random$q4Word proc far
call    permuteSeed
mov     bx, sp
mov     cx, dx
mul     word ptr ss:[bx+4]
mov     ax, cx
mov     cx, dx
mul     word ptr ss:[bx+4]
add     ax, cx
adc     dx, 0
mov     ax, dx
retf    2
@Random$q4Word endp

; PermuteSeed(void)
PermuteSeed proc near
mov     ax, word ptr RandSeed
; uint16_t seedHi = RandSeed1;
mov     bx, word ptr RandSeed+2
; uint16_t seedLo = RandSeed2
mov     cx, ax
; uint16_t tmp = seedHi;
mul     cs:word_1DA26 ; dw 8405h
; uint32_t result = seedHi * 0x8405;
; uint16_t seedHi = (result >> 16) & 0xFF;
; uint16_t resultLo = result & 0xFF;
shl     cx, 1
shl     cx, 1
shl     cx, 1
; tmp <<= 3;
add     ch, cl
; tmp |= (tmp << 16) & 0xFF00;
add     dx, cx
; resultLo += tmp;
add     dx, bx
; resultLo += seedLo;
shl     bx, 1
shl     bx, 1
; seedLo <<= 2;
add     dx, bx
; resultLo += seedLo;
add     dh, bl
; resultLo += (seedLo & 0xFF) << 16;
mov     cl, 5
; tmp = (tmp & 0xFF00) | 0x05;
shl     bx, cl
; seedLo <<= 5;
add     dh, bl
; resultLo += (seedLo & 0xFF) << 16;
add     ax, 1
; seedHi++;
adc     dx, 0
;
mov     word ptr RandSeed, ax
mov     word ptr RandSeed+2, dx
retn
permuteSeed endp

Copyright © Kenny Root. All rights reserved.