Question
I'm supposed to write a program which when run, will randomly select an uppercase letter and then ask the user to guess the letter. The program must provide hints if the user guesses incorrectly (ex. "You guessed 'A' which is too low. Guess higher." or "You guessed 'Z' which is too high. Guess Lower."). When the user finally guesses the correct letter, the program should say "Congratulations, you guessed my letter in only X tries." (where X is the actual number of guesses). The program will also not count any guesses made by the user that are invalid (ex. any number, special character, a space, comma, etc.). The program must ask the user if he/she wants to play again. NO 32 bit or 64 bit code allowed!
Sample of what output should look like when program has run:
-------------------------------------------------------
Welcome to the Letter guessing Game!
I am thinking of a letter in the alphabet.
The object of this game is for you to guess my letter.
If your guess is incorrect, I will give you a hint.
I will count the number of guesses you make.
-------------------------------------------------------
I have my letter.
Enter your guess: m
You guessed "M" which is too high. Guess lower.
Enter your guess: k
You guessed "K" which is too low. Guess higher.
Enter your guess: l
You guessed "L" and my letter is "L"
Congratulations, you guessed my letter in only 3 tries!
Do you want to play again? (y/n): y
I have my letter.
Enter your guess: 9
That is not a letter! Please try again
Enter your guess: f
You guessed "F" which is too high. Guess lower.
Enter your guess: b
You guessed "B" and my letter is "B"
Congratulations, you guessed my letter in only 2 tries!
Do you want to play again? (y/n): y
I have my letter.
Enter your guess: n
You guessed "N" and my letter is "N"
You must be psychic. You guessed my letter in only 1 try!
Do you want to play again? (y/n): n
Explanation / Answer
Hi here im giving you program for random number.in same maner u can try for the character. This assembly language program is designed to create a very simple game where user is allowed to guess a hard coded number between 1 and 255. This hard coded number can be replaced by a randomly generated number using a “random number generator” for EMU8086, which is bit complex because EMU8086 do not contain an instruction to do this implicitly. Program will output if guess is higher or lower than the input number. The code is well commented so that it can be used by anyone who is interested in learning assembly language. System will print a message saying “Please enter a valid number:” Enter a number and *** Press ENTER *** key to continue. *** means ‘Enter key’ is pressed **** Please enter a valid number: 0 Value is More Please enter a valid number: 128 Value is More Please enter a valid number: 170 Value if Less Please enter a valid number: 255 Value if Less Please enter a valid number: 256 Error – Number out of range! Please enter a valid number: -1 Error – Number out of range! Please enter a valid number: 3333 Error – Number out of range! Please enter a valid number: 169 You have made fine Guess! Retry [y/n] ? Source Code view source print? 001 ; E091040066_A1.asm: 002 003 ; Author: Ayoma Gayan Wijethunga 004 005 ; Date : 03/05/2011 006 007 ; ****** This code is written for EMU8086 ***** 008 009 .model small 010 .stack 100h 011 .data 012 013 number db 169d ;variable 'number' stores the random value 014 015 ;declarations used to add LineBreak to strings 016 CR equ 13d 017 LF equ 10d 018 019 ;String messages used through the application 020 prompt db CR, LF,'Please enter a valid number : $' 021 lessMsg db CR, LF,'Value if Less ','$' 022 moreMsg db CR, LF,'Value is More ', '$' 023 equalMsg db CR, LF,'You have made fine Guess!', '$' 024 overflowMsg db CR, LF,'Error - Number out of range!', '$' 025 retry db CR, LF,'Retry [y/n] ? ' ,'$' 026 027 guess db 0d ;variable user to store value user entered 028 errorChk db 0d ;variable user to check if entered value is in range 029 030 param label Byte 031 032 .code 033 034 start: 035 036 ; --- BEGIN resting all registers and variables to 0h 037 MOV ax, 0h 038 MOV bx, 0h 039 MOV cx, 0h 040 MOV dx, 0h 041 042 MOV BX, OFFSET guess ; get address of 'guess' variable in BX. 043 MOV BYTE PTR [BX], 0d ; set 'guess' to 0 (decimal) 044 045 MOV BX, OFFSET errorChk ; get address of 'errorChk' variable in BX. 046 MOV BYTE PTR [BX], 0d ; set 'errorChk' to 0 (decimal) 047 ; --- END resting 048 049 MOV ax, @data ; get address of data to AX 050 MOV ds, ax ; set 'data segment' to value of AX which is 'address of data' 051 MOV dx, offset prompt ; load address of 'prompt' message to DX 052 053 MOV ah, 9h ; Write string to STDOUT (for DOS interrupt) 054 INT 21h ; DOS INT 21h (DOS interrupt) 055 056 MOV cl, 0h ; set CL to 0 (Counter) 057 MOV dx, 0h ; set DX to 0 (Data register used to store user input) 058 059 ; -- BEGIN reading user input 060 while: 061 062 CMP cl, 5d ; compare CL with 10d (5 is the maximum number of digits allowed) 063 JG endwhile ; IF CL > 5 then JUMP to 'endwhile' label 064 065 MOV ah, 1h ; Read character from STDIN into AL (for DOS interrupt) 066 INT 21h ; DOS INT 21h (DOS interrupt) 067 068 CMP al, 0Dh ; compare read value with 0Dh which is ASCII code for ENTER key 069 JE endwhile ; IF AL = 0Dh, Enter key pressed, JUMP to 'endwhile' 070 071 SUB al, 30h ; Substract 30h from input ASCII value to get actual number. (Because ASCII 30h = number '0') 072 MOV dl, al ; Move input value to DL 073 PUSH dx ; Push DL into stack, to get it read to read next input 074 INC cl ; Increment CL (Counter) 075 076 JMP while ; JUMP back to label 'while' if reached 077 078 endwhile: 079 ; -- END reading user input 080 081 DEC cl ; decrement CL by one to reduce increament made in last iteration 082 083 CMP cl, 02h ; compare CL with 02, because only 3 numbers can be accepted as IN RANGE 084 JG overflow ; IF CL (number of input characters) is greater than 3 JUMP to 'overflow' label 085 086 MOV BX, OFFSET errorChk ; get address of 'errorChk' variable in BX. 087 MOV BYTE PTR [BX], cl ; set 'errorChk' to value of CL 088 089 MOV cl, 0h ; set CL to 0, because counter is used in next section again 090 091 ; -- BEGIN processing user input 092 093 ; -- Create actual NUMERIC representation of 094 ;-- number read from user as three characters 095 while2: 096 097 CMP cl,errorChk 098 JG endwhile2 099 100 POP dx ; POP DX value stored in stack, (from least-significant-digit to most-significant-digit) 101 102 MOV ch, 0h ; clear CH which is used in inner loop as counter 103 MOV al, 1d ; initially set AL to 1 (decimal) 104 MOV dh, 10d ; set DH to 10 (decimal) 105 106 ; -- BEGIN loop to create power of 10 for related possition of digit 107 ; -- IF CL is 2 108 ; -- 1st loop will produce 10^0 109 ; -- 2nd loop will produce 10^1 110 ; -- 3rd loop will produce 10^2 111 while3: 112 113 CMP ch, cl ; compare CH with CL 114 JGE endwhile3 ; IF CH >= CL, JUMP to 'endwhile3 115 116 MUL dh ; AX = AL * DH whis is = to (AL * 10) 117 118 INC ch ; increment CH 119 JMP while3 120 121 endwhile3: 122 ; -- END power calculation loop 123 124 ; now AL contains 10^0, 10^1 or 10^2 depending on the value of CL 125 126 MUL dl ; AX = AL * DL, which is actual positional value of number 127 128 JO overflow ; If there is an overflow JUMP to 'overflow'label (for values above 300) 129 130 MOV dl, al ; move restlt of multiplication to DL 131 ADD dl, guess ; add result (actual positional value of number) to value in 'guess' variable 132 133 JC overflow ; If there is an overflow JUMP to 'overflow'label (for values above 255 to 300) 134 135 MOV BX, OFFSET guess ; get address of 'guess' variable in BX. 136 MOV BYTE PTR [BX], dl ; set 'errorChk' to value of DL 137 138 INC cl ; increment CL counter 139 140 JMP while2 ; JUMP back to label 'while2' 141 142 endwhile2: 143 ; -- END processing user input 144 145 MOV ax, @data ; get address of data to AX 146 MOV ds, ax ; set 'data segment' to value of AX which is 'address of data' 147 148 MOV dl, number ; load original 'number' to DL 149 MOV dh, guess ; load guessed 'number' to DH 150 151 CMP dh, dl ; compare DH and DL (DH - DL) 152 153 JC greater ; if DH (GUESS) > DL (NUMBER) cmparision will cause a Carry. Becaus of that if carry has been occured print that 'number is more' 154 JE equal ; IF DH (GUESS) = DL (NUMBER) print that guess is correct 155 JG lower ; IF DH (GUESS)