Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to create a program that creates a random number in assembly and keep gue

ID: 3565792 • Letter: I

Question

I need to create a program that creates a random number in assembly and keep guessing until i get it between 1 -100.

So the first part of the program will generate a random number between 1 to 100. The next part will ask me in a loop to keep guessing until I get the correct number and asks me if I want to play again.. I just need that, nothing more, nothing less.

For example, the program randomly generates 51

"I am thinking of a number between 1-100"

Guess a number: 70

>too high, guess again

25

>too low, guess again

51

>correct! Would you like to play again?

n

Explanation / Answer

define(PRINTF, ` pushl $1 call printf addl $ 4, %esp ') define(PRINTF_NUM, ` pushl $2 pushl $1 call printf addl $ 8, %esp ') define(FPRINTF_ERR_STR, ` pushl $2 pushl $1 pushl $__iob+32 call fprintf addl $ 12, %esp ') define(FGETS_STR, ` pushl $__iob pushl $bufsize_l pushl $1 call fgets addl $ 12, %esp ') .section ".rodata" greet_s: .string "guess_x86 - Guess a number between 1 and 100 " entry1_s: .string "Enter guess %d: " higher_s: .string "Higher... " lower_s: .string "Lower... " correct_s: .string "Correct! That took %d guesses. " entry2_s: .string "Please enter your name: " file_s: .string "highscores_x86" mode1_s: .string "a" mode2_s: .string "r" err1_s: .string "ERROR: Can't write to %s " err2_s: .string "ERROR: Can't read from %s " save_s: .string "%s %d " prev_s: .string " Previous high scores, " out_s: .string "%s" bufsize_l: .long 256 .section ".bss" reply_b: .skip 256 name_b: .skip 256 buf_b: .skip 256 .data answer_l: .long 0 guess_l: .long 0 num_l: .long 0 fd_l: .long 0 .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $12, %esp PRINTF($greet_s) /* Generate random number */ pushl $0 call time addl $4, %esp pushl %eax call srand call rand addl $4, %esp movl $0, %edx movl $100, %ebx divl %ebx incl %edx movl %edx, answer_l /* Play game */ game: movl num_l, %eax incl %eax movl %eax, num_l PRINTF_NUM($entry1_s, num_l) FGETS_STR($reply_b) pushl $reply_b call atoi addl $4, %esp movl answer_l, %ebx cmpl %ebx, %eax jge .lower PRINTF($higher_s) jmp game .lower: cmp %ebx, %eax je .done PRINTF($lower_s) jmp game .done: PRINTF_NUM($correct_s, num_l) /* Save high score */ PRINTF($entry2_s) FGETS_STR($name_b) movl $name_b, %eax movl $0, %ebx .L1: movb (%eax), %cl cmpb $' ', %cl je .L2 incl %eax incl %ebx cmpl $256, %ebx jge .L2 jmp .L1 .L2: movb $0, (%eax) pushl $mode1_s pushl $file_s call fopen addl $8, %esp movl %eax, fd_l cmpl $0, %eax jne .write FPRINTF_ERR_STR($err1_s, $file_s) pushl $1 pushl (%esp) call exit .write: addl $8, %esp pushl num_l pushl $name_b pushl $save_s pushl fd_l call fprintf addl $16, %esp pushl fd_l call fclose addl $4, %esp /* Print high scores */ PRINTF($prev_s) pushl $mode2_s pushl $file_s call fopen addl $8, %esp movl %eax, fd_l cmpl $0, %eax jne .read FPRINTF_ERR_STR($err2_s, $file_s) pushl $2 pushl (%esp) call exit .read: pushl fd_l pushl $bufsize_l pushl $buf_b call fgets addl $12, %esp cmpl $0, %eax je .eof pushl $buf_b pushl $out_s call printf addl $4, %esp jmp .read .eof: pushl fd_l call fclose addl $4, %esp leave ret .size main, .-main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote