asked 17.1k views
2 votes
The RandomRange procedure from the Irvine32 library generates a pseudorandom integer between 0 and N - 1 where N is an input parameter passed in the EAX register. Your task is to create an improved version that generates an integer between P and Q. Let the caller pass P in EBX and Q in EAX. If we call the procedure BetterRandomRange, the following code is a sample test: mov ebx, -300 ; lower bound mov eax, 100 ; upper bound call BetterRandomRange Write a short test program that calls BetterRandomRange from a loop that repeats 50 times. Display each randomly generated value. Masm g

asked
User SimonH
by
7.3k points

1 Answer

5 votes

Answer:

BetterRandomRange PROC

push ebp

mov ebp, esp

push ebx

push ecx

push edx

sub eax, ebx ; get the range

call RandomRange ; call the original RandomRange procedure

add eax, ebx ; adjust the result by adding the lower bound

pop edx

pop ecx

pop ebx

mov esp, ebp

pop ebp

ret

BetterRandomRange ENDP

Step-by-step explanation:

In this code, we first save the current stack pointer and base pointer by pushing them onto the stack. We also push the ebx, ecx, and edx registers to preserve their values.

We then subtract the lower bound (ebx) from the upper bound (eax) to get the range of possible values. We call the original RandomRange procedure, passing in the range as the input parameter.

Finally, we add the lower bound back to the result to get a random integer within the desired range. We restore the ebx, ecx, and edx registers, restore the stack pointer and base pointer, and return from the procedure.

Example

INCLUDE Irvine32.inc

.CODE

main PROC

mov ebx, -300 ; lower bound

mov eax, 100 ; upper bound

mov ecx, 50 ; loop counter

random_loop:

call BetterRandomRange

call WriteInt

call Crlf

loop random_loop

exit

main ENDP

END main

answered
User Shahzad Ahamad
by
8.5k points