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 farmov     ah, 2Chint     21h             ; DOS - GET CURRENT TIME                        ; Return: CH = hours, CL = minutes, DH = seconds                        ; DL = hundredths of secondsmov     word ptr RandSeed, cxmov     word ptr RandSeed+2, dxretf@Randomize$qv endp
; Random(Word)@Random$q4Word proc farcall    permuteSeedmov     bx, spmov     cx, dxmul     word ptr ss:[bx+4]mov     ax, cxmov     cx, dxmul     word ptr ss:[bx+4]add     ax, cxadc     dx, 0mov     ax, dxretf    2@Random$q4Word endp
; PermuteSeed(void)PermuteSeed proc nearmov     ax, word ptr RandSeed; uint16_t seedHi = RandSeed1;mov     bx, word ptr RandSeed+2; uint16_t seedLo = RandSeed2mov     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, 1shl     cx, 1shl     cx, 1; tmp <<= 3;add     ch, cl; tmp |= (tmp << 16) & 0xFF00;add     dx, cx; resultLo += tmp;add     dx, bx; resultLo += seedLo;shl     bx, 1shl     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, axmov     word ptr RandSeed+2, dxretnpermuteSeed endp

Kenny Root

Copyright © Kenny Root. All rights reserved.