; Assembly code for pseudorandom sequence generation
; Written for MASM assembler, to be linked to MS Visual C++
; by Sergio A. Alvarez

.586
.model flat
public _rando

.data
   multiplier = 40
   increment = 3641
   modulus = 729
   seed dd ?

.code

_rando proc
; C++ prototype: rando(int inseed, int& outseed)
; inseed used as seed for linear congruence generator
; resulting residual is returned in outseed
;
; set up stack frame
   push ebp
   mov ebp,esp
; save registers
   pushad   
; get inseed parameter from stack
   mov eax,dword ptr [ebp+8]
; calculate pseudorandom residual
   mov edx,0
   mov ebx,0
   mov bx,word ptr multiplier
   mul bx
   add ax,word ptr increment
   adc dx,0
   mov bx, modulus
   div bx
; return result in outseed
   mov seed,edx
   mov edi,[ebp+12]
   mov [edi],edx
; restore registers
   popad   
; wrap up stack frame
   pop ebp
   ret
_rando endp

end
