X86 Lottery Simulator

Assignment 

PLEASE CODE IN ASSEMBLY LANGUAGE PROGRAM.PLEASE USE VISUAL STUDIO AND ASSEMBLY CODE(.asm) 

The format should be like:

<< TITLE MASM Template (main.asm)

INCLUDE Irvine32.inc

.data

.code

main PROC

exit main

ENDP

END main >>

Create a lottery simulator. The program must allow the user to enter the starting value, the ending value, and the number of times to generate a random number. The program should store the inputs in variables and use them in the program. Requirements: Display input messages to get the starting value, ending value, and number of times to generate a number. Use a LOOP instruction to generate a random number between the start and end values and display the randomly generated number. The program should display the lottery number generated for each loop iteration. For example, the user wants to run the lottery 100 times with the starting value of 1 and the ending value of 999 where each time the lottery generates a number it gets displayed on the screen. Change the color of the background and text for your program using the Irvine Library function. 

Solution 

TITLE MASM Template (main.asm)

INCLUDE Irvine32.inc

.data

BlueTextOnGray = blue + (lightGray * 16)

DefaultColor = (lightGray * 16)

startmsg    DB    “Please enter the starting value: “,0

endmsg            DB    “Please enter the ending value: “,0

timesmsg    DB    “Please enter the number of times to generate random numbers: “,0

starting    DD    0

ending            DD    0

number            DD    0

.code

main PROC

call Randomize                      ; init the random generator

moveax,BlueTextOnGray

callSetTextColor              ; set the color as blue on gray

callClrScr                          ; clear the screen on the selected color

movedx, OFFSET startmsg ; display input message to enter starting value

callWriteString               ; display the string

callReadInt                   ; get the value from the user into eax

movstarting,eax               ; save read value in the variable

movedx, OFFSET endmsg         ; display input message to enter ending value

callWriteString               ; display the string

callReadInt                   ; get the value from the user into eax

movending,eax                       ; save read value in the variable

movedx, OFFSET timesmsg ; display input message to enter number of times value

callWriteString               ; display the string

callReadInt                   ; get the value from the user into eax

movnumber,eax                       ; save read value in the variable

movecx,number                       ; load number of times in ecx for using it in a loop instruction

generate:

moveax,ending                       ; load the ending value

sub   eax,starting                  ; subtract the starting to get the range

callRandomRange               ; generate a random number within the range 0 to starting-ending

add   eax,starting                  ; add starting value to get a value between starting and ending

movedx,eax                          ; save the generated number in edx

moveax,DefaultColor           ; get default color of gray

movebx,ecx                          ; get current number count in ebx

and ebx,15                          ; cut the number between 0 and 15, which are the valid colors

cmpebx,lightGray              ; see if the color we get is also gray as the background

jne ok                                    ; avoid printing gray on gray

movebx,blue                   ; change it to blue

ok: add eax,ebx                           ; add count to default color to get a different color

callSetTextColor              ; change the text color

moveax,edx                          ; get the generated number we saved in edx and put it in eax

callWriteDec                        ; display the generated number on the screen

callCrLf                            ; print a line feed to separate the lines

loop generate                       ; decrement ecx and repeat number generation while ecx is not zero

exit                                ; exit the program

main ENDP

END main