Write a program that instead of you guessing the computers number, the computer
ID: 3734898 • Letter: W
Question
Write a program that instead of you guessing the computers number, the computer tries to guess your number from 1 to 100. you think of a number. computer takes a guess. you type "too high" or "too low" bases on that, the computer adjusts its guess. devise an algorithm that solves this in 10 or less steps. In Assembly Language. I need help adjusting the highest and lowest number range.
Here is my code so far:
include Irvine32.inc
.data
welcome BYTE "Welcome to my game! It is so cool.", 0
think BYTE "Think of a number between 1 and 100", 0
guess DWORD ?
lowest DWORD 1
highest DWORD 100
tries DWORD 0
guessIs BYTE "My guess is: ", 0
response BYTE 10 DUP(0)
directions BYTE "(yes) or no (too high / too low): ", 0
success BYTE "YAYYYY. I did it!", 0
yesStr BYTE "yes", 0
tooHighStr BYTE "too high", 0
tooLowStr BYTE "too low",0
triesStr BYTE "It only took ", 0
triesStr2 BYTE " tries.", 0
.code
main proc
mov edx, OFFSET welcome
call WriteString
call Crlf
mov edx, OFFSET think
call WriteString
call Crlf
;initial guess
mov guess, 50
TELLGUESS:
inc tries
mov edx, OFFSET guessIs
call WriteString
mov eax, guess
call WriteInt
call Crlf
mov edx, OFFSET directions
call WriteString
mov edx, OFFSET response
mov ecx, 9
call ReadString
INVOKE Str_compare, ADDR response, ADDR yesStr
je YES
INVOKE Str_compare, ADDR response, ADDR tooLowStr
je TOOLOW
INVOKE Str_compare, ADDR response, ADDR tooHighStr
je TOOHIGH
TOOLOW:
mov eax, guess
mov esi, highest
mov edi, lowest
mov edi, eax
add edi, esi
mov ebx, 2
mov eax, edi
xor edx, edx
div ebx
mov lowest, eax
mov guess, eax
mov lowest, eax
mov highest, esi
jmp TELLGUESS
TOOHIGH:
mov eax, guess
mov esi, highest
mov edi, lowest
mov esi, eax
add esi, edi
mov ebx, 2
mov eax, esi
xor edx, edx
div ebx
mov highest, eax
mov guess, eax
mov lowest, edi
jmp TELLGUESS
YES:
mov edx, OFFSET success
call WriteString
call Crlf
mov edx, OFFSET triesStr
call WriteString
mov eax, tries
call WriteInt
mov edx, OFFSET triesStr2
call WriteString
call Crlf
invoke ExitProcess,0
main endp
end main
Explanation / Answer
.string " Guess the random number: "
.LC1:
.string " Too low "
.LC2:
.string "Too high "
.LC3:
.string " *****Right***** "
.LC4:
.string "No of trys: "
.LC5:
.string " "
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 0
mov edi, 0
call time
mov edi, eax
call srand
call rand
mov ecx, eax
mov edx, 1431655766
mov eax, ecx
imul edx
mov eax, ecx
sar eax, 31
sub edx, eax
mov eax, edx
add eax, eax
add eax, edx
sub ecx, eax
mov edx, ecx
lea eax, [rdx+1]
mov DWORD PTR [rbp-8], eax
.L6:
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
lea rax, [rbp-12]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
add DWORD PTR [rbp-4], 1
mov eax, DWORD PTR [rbp-12]
cmp DWORD PTR [rbp-8], eax
jle .L2
mov esi, OFFSET FLAT:.LC1
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-12]
cmp DWORD PTR [rbp-8], eax
jge .L4
mov esi, OFFSET FLAT:.LC2
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
jmp .L3
.L4:
mov eax, DWORD PTR [rbp-12]
cmp DWORD PTR [rbp-8], eax
jne .L3
mov esi, OFFSET FLAT:.LC3
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov esi, OFFSET FLAT:.LC4
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov rdx, rax
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC5
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
.L3:
mov eax, DWORD PTR [rbp-12]
cmp DWORD PTR [rbp-8], eax
je .L5
jmp .L6
.L5:
mov eax, 0
leave
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L10
cmp DWORD PTR [rbp-8], 65535
jne .L10
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L10:
nop
leave
ret
_GLOBAL__sub_I_main:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret
C++ Code
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int random, guess, counter = 0;
srand((unsigned) time(0));
random = (rand()%3)+1;
do{
cout << " Guess the random number: ";
cin >> guess;
counter++;
if (guess < random)
cout <<" Too low ";
else if (guess > random)
cout << "Too high ";
else if (guess == random)
{
cout << " *****Right***** ";
cout << "No of trys: " << counter <<" ";
}
} while (guess != random);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.