Program of generate a random number, Assembly Language

This is a short program to practice assembly language loops and if/else statements. You will use various jump commands and the cmp instruction.

The program will generate a random number in the range 0-9 using the Randomize and RandomRange functions and the user will try and guess the number generated. The numbers entered are in base 10.

You should use ReadDec to read a decimal number from the keyboard (the user's guess). ReadDec stores the number read into the eax register.

This program must can be run in Visual Studio 2008 Professional edition

This program MUST ONLY use Irvine library functions

 INCLUDELIB /Irvine/Irvine32.lib

Constants (using "equ")

You should define a constant, LF, for ASCII code for a linefeed (0Ah).


Declare the following prototypes in the Prototype section of your code.

ExitProcess PROTO,

dwExitCode:DWORD;from Win32 api not Irvine to exit to dos with exit code

ReadChar PROTO;Irvine code for getting a single char from keyboard

;Character is stored in the al register.

;Can be used to pause program execution until key is hit.

ReadDec PROTO;Irvine code to read 32 bit decimal number from console

;and store it into eax

WriteString PROTO ; Irvine code to write null-terminated string to output

; EDX points to string

RandomRange PROTO ; Returns an unsigned pseudo-random 32-bit integer

; in EAX, between 0 and n-1. If n = 10 a random number

; in the range 0-9 is generated


; Input parameter: EAX = n.

Randomize PROTO ; Re-seeds the random number generator with the current time

; in seconds.

Variables (.data)

You should declare zero terminated strings for the prompts.

You should embed in the string enough CR,LF as necessary to generate the correct output.

See output example below for how the output should be formatted. You should use WriteString to print out the messages and the output.

The messages should be as follows (substitute your name for my name):

"Program 3 by Fred Kennedy"

"Guess a number in the range 0 - 9."

"Guess: "




"Do another?('y' or 'Y' to continue. Any other character to exit)"

You may declare any other variables you think you need but use variables sparingly since moving data to and from memory is not as efficient as using registers which are on the CPU.

mPrtStr Macro(See Irvine 10.2)(see macro_mPrtStr.asm on the class web site)

To print a string you should use the mPrtStr macro defined at the top of ifElse.asm

Instead of the following code to print a string named message:

pushedx ;save edx

movedx, offset message ;address of str to display should be in dx

callWriteString;display 0 terminated string

popedx;restore edx

you can use a macro:

mPrtStr message

Wherever mPrtStr appears in the code it will be replaced with the macro definition

mPrtStr MACRO arg1 ;arg1 is replaced by the name of string to be displayed

pushedx ;save edx

movedx, offset arg1 ;address of str to display should be in dx

callWriteString ;display 0 terminated string

popedx ;restore edx


Do not copy the above macro from this PDF file. It may not operate properly if you do. If you wish to copy the mPrtStr macro copy it from the top of ifElse.asm on the class web site or copy it from macro_mPrtStr.asm

During assembling wherever "mPrtStr message" appears in the code it will be replaced with:


movedx, offset message



arg1 is replaced with message if that is the name of the string passed in.

Before you can use a macro you must first define it in your code.

You can place the macro definition section of your code just above the prototype section.

Please do not use any other macros in your code unless directed otherwise or unless you ask first.

Program specification:

The list of steps below just gives the basic flow of the program. You should read the details of this entire document to get all the requirements for writing the program.

You are to write a program with an outer and inner loop. The outer loop is the equivalent of a doWhile loop.

1. display opening message

2. seed the random number generator by calling Randomize

3. enter outer loop (loop1:)

4. display "guess a number" message"

5. get the random number (RandomRange)

6. enter inner loop (loop2:)

7. display "guess" message

8. get guess from user (ReadDec)

9. You should code a series of if/else statements that compares the user's guess to the random number generated and takes appropriate action depending on whether the guess was high (print "High!" and repeat loop2), low (print "Low!" and repeat loop2) or correct(print "Correct!!" and ask the user to input 'y' to continue (ReadChar)).

You should arrange the series of if/else for step 9 in the most efficient order that requires the least amount of jumps.

You should also determine the best timing for exiting loop 2 and in which loop (loop1 or loop2) the "High!", "Low!" and "Correct!!" messages should be printed.

The series of if/else should take the following form:



else if (condition)


else (no condition needed since only option left)


10.If the user enters 'Y' or 'y', repeat loop1 (step 3) else quit (exit to dos with ExitProcess).

Warning: "Invoke ExitProcess" should remain at the bottom of the main proc even if it seems inefficient.

Since loop2 is contained in loop1, if loop1 is repeated loop2 will be repeated.

Use the appropriate jump instruction: JB, JA, JE, JBE, JAE, JNE or JMP. You should use the cmp instruction to compare 2 numbers.

Do not use any directives like .IF, .WHILE etc. You should code a loop and if/else statements using cmp, labels and jumps.

Remember that efficiency counts. Points will be taken off for inefficient code, convoluted code or code that has no effect. You should use the least amount of jumps you need to get the job done.

Organization of code

In general code should flow from top to bottom.

You should only jump back towards the top to repeat a loop. Loops should flow from top to bottom. If you have nested loops, the code for the outer loop should flow from top of outer loop to bottom of outer loop and the code for the inner loop should flow from top of inner loop to bottom of inner loop.

Outer Loop top:

-outer loop code


-inner loop code

-bottom of inner loop: code to jump back to innerLoopTop to repeat inner loop

-you may also have code to cut the loop short to repeat the loop like the C++ continue

-bottom of outer loop: code in outer loop that comes after inner loop including code to jump back to outerLoopTop to repeat outer loop. If your code does not flow from top to bottom as described above, points will be taken off.

Posted Date: 2/19/2013 1:35:00 AM | Location : United States

Related Discussions:- Program of generate a random number, Assignment Help, Ask Question on Program of generate a random number, Get Answer, Expert's Help, Program of generate a random number Discussions

Write discussion on Program of generate a random number
Your posts are moderated
Related Questions
take an integer and its base and the base in which you want to convert the number from user and perform conversion.

Compute the Fibonacci sequence - assembly program: Problem: Fibonacci   In this problem you will write a program that will compute the first 20 numbers in the Fibonacci sequ

General Data Registers Given figure indicate the register organization of 8086. The registers DX, CX, BX and AX are the general purpose 16-bit registers. AX is behaved as 16-bi

Write a program that will input a number from the keyboard, and then display the number in binary form, as well as the number of one's in the number. Hint: Shift the value left (or

need algorithm for multiplication in assembly with out mul function?

code, Assembly Language How to print strings in Right Triangle form?

NEG: Negate:- The negate instruction forms the 2's complement of the particular destination in the instruction. For obtaining 2's complement, it subtracts the contents of destinat

Overview of Intel Pro-Pentium : The 2 chief players in the PC CPU market are Motorola and Intel.  Intel has enjoyed incredible success with its processors since the early 1980

Write a nonrecursive version of the Factorial procedure (Section 8.3.2) that uses a loop. (A VideoNote for this exercise is posted on the Web site.) Write a short program that inte

write and run a programme using 8086 assembly language that interchange the lower four bits of AL registered with upper four bits.