diff --git a/Main b/Main new file mode 100644 index 0000000..a86fc96 --- /dev/null +++ b/Main @@ -0,0 +1,561 @@ +; Assembly code for a simple True or False game for football +; the game prints some questions on the screen +; As an array of the guesses over the time +; Has comparison of the guess with the actual letter thinking of + +section .data +introduction db "introduction to this simple quiz about football", 10 ;introduction message for game +introductionLen equ $-introduction ; calculate thec introduction message length + + +instructions db "Use captial T and F to answer the questions", 10 +instructionslen equ $-instructions + + +statementmsg db "The answer you provide is correct.", 10 ;Message that the guess is correct +statementlen equ $-statementmsg ;Calculate the length of the message + +statement2msg db "The answer you provide is correct.", 10 ;Message that the guess is correct +statement2len equ $-statement2msg ;Calculate the length of the message + + +notstatementmsg db "The answer you provide is incorrect:", 10 ;Message that the guess is incorrect +notstatementlen equ $-notstatementmsg ;Calculate the length of the message + +notstatement2msg db "The answer you provide is incorrect:", 10 ;Message that the guess is incorrect +notstatement2len equ $-notstatementmsg ;Calculate the length of the message + +character1input db "character 1 enter your answer:",10 ;Display message asking character 1 to enter the answer +character1inputlen equ $-character1input + +character2input db "character 2 enter your answer:",10 ;Display message asking character 2 to enter the answer +character2inputlen equ $-character2input + +draw db "It was a draw for both characters.",10 ; +drawlen equ $-draw + +winner db "The winner is:",10 +winnerlen equ $-winner + + + +character1winner db "character 1 is the winner",10 +character1winnerlen equ $-character1winner + + +character2winner db "character 2 is the winner",10 +character2winnerlen equ $-character2winner +call done + + + +ques db " Questioun 1: Is Moscow is a capital of Russia?", 10 ; Message displaying the quess +queslen equ $-ques ; Used for calcualating the length of the quess + +ques1 db " Questioun 2: Is Kyiv is a capital of Ukraine?", 10 +ques1len equ $-ques1 + +ques2 db " Questioun 3: Do UK have 5 capital cities?", 10 +ques2len equ $-ques2 + +ques3 db " Questioun 4: Do Unites States of America have 50 States? ",10 +ques3len equ $-ques3 + +ques4 db " Questioun 5: Do India have 22 states? ",10 +ques4len equ $-ques4 + +ques5 db " Questioun 6: Is Kyiv is a capital of Ukraine?? ",10 +ques5len equ $-ques5 + +ques6 db " Questioun 7: Do India have 22 states? ",10 +ques6len equ $-ques6 + +ques7 db " Questioun 8: Is Kyiv is a capital of Ukraine? Do India have 22 states? ",10 +ques7len equ $-ques7 + +ques8 db " Questioun 9: Do India have 22 states? ",10 +ques8len equ $-ques8 + +ques9 db " Questioun 10: Do India have 22 states? ",10 +ques9len equ $-ques9 + +ques10 db " Is Kyiv is a capital of Ukraine?? ",10 +ques10len equ $-ques10 + +expl db " this is zero", 10 ; +expllen equ $-expl ; + +expl1 db " this is expl first ", 10 ; +expl1len equ $-expl1 ; + +expl2 db " t this is expla econd ", 10 ; +expl2len equ $-expl2 ; + +expl3 db " this is explain third ", 10 ; +expl3len equ $-expl3 ; + +expl4 db " th this is explai fourth ", 10 ; +expl4len equ $-expl4 ; + +expl5 db " th this is explai fifth ", 10 ; +expl5len equ $-expl5 ; + +expl6 db " th this is explai six ", 10 ; +expl6len equ $-expl6 ; + +expl7 db " th this is explaint seven ", 10 ; +expl7len equ $-expl7 ; + +expl8 db " this is a explation eigth ", 10 ; +expl8len equ $-expl8 ; + +expl9 db " thi this is nine expl ", 10 ; +expl9len equ $-expl9 ; + +letterMessage db "The correct answer is:" ; Message explaining what the correct answer is. +letterMessageLen equ $-letterMessage ; Used for calculating the length of the Message +cr db 10 ;for creating a new line same as endl in C++ + +score dq 0 ; Used for storing score of character 1 +score2 dq 0 ; Used for storing score of character 2 + +print_my_score db "character 1 score is :" ; Displaying score of character 1 +print_my_scorelen equ $ -print_my_score + +print_my_score2 db "character 2 score is :" ; Displaying score of character 2 +print_my_score2len equ $ -print_my_score2 + +;The array use to direct to the quess +global listques +listques: + dq ques + dq ques1 + dq ques2; the pointer to the quess are stored in 8 bytes + dq ques3 + dq ques4 + dq ques5 + dq ques6 + dq ques7 + dq ques8 + dq ques9 + dq ques10 + +;The array use to point to the quess lengths +listquesLen: + dq queslen + dq ques1len + dq ques2len ; the pointer to the quess lengths are stored in 8 bytes + dq ques3len + dq ques4len + dq ques5len + dq ques6len + dq ques7len + dq ques8len + dq ques9len + dq ques10len + + +;the array used for storing the correct answers of the given quess +global listAnswers +listAnswers: + dq 'T' ; the answer letters are stores in 8 bytes to aid the comparison that is why we are using the data type dq + dq 'T' + dq 'F' + dq 'T' + dq 'F' + dq 'F' + dq 'F' + dq 'F' + dq 'F' + dq 'F' + + +global listexpl +lestexpl: + dq expl + dq expl1 + dq expl2 + dq expl3 + dq expl4 + dq expl5 + dq expl6 + dq expl7 + dq expl8 + dq expl9 + +listexpllen: + dq expllen + dq expl1len + dq expl2len + dq expl3len + dq expl4len + dq expl5len + dq expl6len + dq expl7len + dq expl8len + dq expl9len + +letter: + dq 0 ; where we store each of the answers one at a time + +section .bss + +guess resq 1 ; variable to store the character 1 character guess +guess2 resq 1 ; variable to store the character 2 character guess + +printScoreBss resb 1 +printScore2Bss resb 1 + + +section .text + global _start ;must be declared for linker (ld) +_start: +call displayintroduction +call newLine +mov rax,10 ;number of answers and so possible quess +mov rbx,0 ;RBX will store the answer currently being entered +mov rcx, listAnswers ;RCX will point to the current element in array to be guessed +mov rdi, listques +mov r9, listquesLen +mov r10, lestexpl +mov r8, listexpllen + +;Main function which will call other functions +top: + mov rbx, [rcx] ; put the current answer being entered in rbx + mov [letter],rbx ; move rbx into a variable letter that stores the current correct answer + push rax ; push rax on stack + push rcx ; push rax on stack + call displayques ; call subroutine to display the ques + call newLine ; call for going on a new line + call input1 ; call to subroutine to display message asking character 1 to eneter the answe + call reading ; call subroutine to get the character 1 guess and compare the guess with the correct answer stored in the array + call newLine ; call for going on a new line + call input2 ; call to subroutine to display message asking character w to eneter the answer + call reading2 ; call subroutine to get the character 2 guess and compare the guess with the correct answer stored in the array + call newLine ; call for going on a new line + call displayletterMessage ; call subroutine to display the message for the correct answer + call newLine + call display ; call subroutine to print the correct answer + call newLine + call displayScore + call displayScore2 + call newLine + call displayexpl + call newLine + pop rcx ; get back from stack + pop rax ; get back from stack + add rdi,8 ;move pointer to next element in the essay. As 8 bits for each letter move on by 8 + add r9,8 + add r10,8 + add r8,8 + add rcx,8 + dec rax ;decrement counter by one so going down + jnz top ;if counter not 0, then loop again + + call displayScore + call displayScore2 + call newLine + call dislplaywinner + call comparecharacter + + call done ; call subroutine to end program + +;Display function +display: + mov edx,1 ;message length + mov ecx, letter ;message to write the correct answer + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;system call number (sys_write) + int 0x80 ;call kernel + ret + + +displayScore: + + mov eax, [score] ;move the incValue variable in eax + add eax, 48 ; convert the integer into ascii value to print + mov [printScoreBss], eax ; put the value into the printIncValue variable + +; write the incremented statement to screen. + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ecx, print_my_score; print inc message + mov edx, print_my_scorelen; length of message + int 80h ; interrupt + +; write the incremented value to screen. + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ecx, printScoreBss; print value + mov edx, 1; One byte is size + int 80h ; interrupt + +; write the news line to screen. Same as endl in C++ + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ecx, cr; newline + mov edx, 1; One byte is the size of newline + int 80h ; interrupt + ret + +displayScore2: + + mov eax, [score2] ;move the incValue variable in eax + add eax, 48 ; convert the integer into ascii value to print + mov [printScore2Bss], eax ; put the value into the printIncValue variable + +; write the incremented statement to screen. + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ecx, print_my_score2; print inc message + mov edx, print_my_score2len; length of message + int 80h ; interrupt2 + +; write the incremented value to screen. + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ebx, 1 ; standard out + mov ecx, printScore2Bss; print value + mov edx, 1; One byte is size + int 80h ; interrupt + +; write the news line to screen. Same as endl in C++ + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ecx, cr; newline + mov edx, 1; One byte is the size of newline + int 80h ; interrupt + ret + + + + + + +;function to read the input of character 1 and do comparison with the answer +reading: + mov eax, 3 ; read from keyboard + mov ebx, 2; stdin + mov ecx, guess ; move user guess into ecx + mov edx, 1 ; As single letter using 1 byte + int 80h ; invoke the kernel to get the user's guess + mov rax, [guess] ; move guess by user into rax + cmp rax, [letter] ; compare correct answer with what in rax + je statement ; if guess of character 1 was correct jump to same function + call Notstatement ; if the guess of character 1 is incorrect then go to Notstatement function + ret ; return to the main section + +;function to read the input of character 2 and do comparison with the answer +reading2: + mov eax, 3 ; read from keyboard + mov ebx, 2; stdin + mov ecx, guess2 ; move user guess into ecx + mov edx, 1 ; As single letter using 1 byte + int 80h ; invoke the kernel to get the user's guess + mov rax, [guess2] ; move guess by user into rax + cmp rax, [letter] ; compare correct answer with what in rax + je statement2 ; if guess of character 2 was correct jump to statement function + call Notstatement ; if the guess character 2 is incorrect then go to Notstatement function + ret ; return to the main section + + +;function to display message that the character answer was not correct answer +Notstatement: + mov ecx,notstatementmsg ; Not statement message + mov edx,notstatementlen ; length of statement message + mov ebx,1 ;file descriptor (stdout) + mov eax,4 ;system call number (sys_write) + int 80h ; invoke the kernel to display message + + mov eax, 3 ; read to clear the keyboard buffer + mov ebx, 2 ; stdin + mov ecx, guess ; Clear the key press from the user input so it does not messy up loop + mov edx, 1 ; As single character using 1 byte + int 80h ; invoke the kernel to take the enter key press to clear the keyboard buffer + ret ; return to main code + +; function to display message that answer was correct for character 1 +statement: + mov ecx,statementmsg ; statement message + mov edx, statementlen ; length of same message + mov ebx,1 ;file descriptor (stdout) + mov eax,4 ;system call number (sys_write) + int 80h ; invoke the kernel to display message + + + mov eax, 3 ; read to clear the keyboard buffer + mov ebx, 2 ; stdin + mov ecx, guess ; Clear the key press from the user input so it does not messy up loop + mov edx, 1 ; As single character using 1 byte + int 80h ; invoke the kernel to take the enter key press to clear the keyboard buffer + + + inc byte [score] + mov eax, [score] + ret + +; function to display message that answer was correct for character 2 +statement2: + mov ecx,statement2msg ; statement message + mov edx, statement2len ; length of same message + mov ebx,1 ;file descriptor (stdout) + mov eax,4 ;system call number (sys_write) + int 80h ; invoke the kernel to display message + + mov eax, 3 ; read to clear the keyboard buffer + mov ebx, 2 ; stdin + mov ecx, guess2 ; Clear the key press from the user input so it does not messy up loop + mov edx, 1 ; As single character using 1 byte + int 80h ; invoke the kernel to take the enter key press to clear the keyboard buffer + + + inc byte [score2] + mov eax, [score2] + + + ret ; return to main code + + +; Function to create a New line + newLine: + mov eax,4 ; Put 4 in eax register into which is system + ;call for write (sys_write) + mov ebx,1 ; Put 1 in ebx register which is the standard + ; output to the screen + mov ecx, cr ; Put the newline value into ecx register + mov edx, 1 ; Put the length of the newline value into edx + ; register + int 80h ; Call the kernel with interrupt to check the + ; registers and perform the action of moving to + ; the next line like endl in c++ + ret ; return to previous position in code + +;Function to display introduction to game message +displayintroduction: + mov edx,introductionLen ;message length + mov ecx, introduction ;message to write + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;system call number (sys_write) + int 0x80 ;invoke the kernel to print the message + call newLine + + + mov edx,instructionslen + mov ecx,instructions + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;system call number (sys_write) + int 0x80 ;invoke the kernel to print the message + + ret ;return to the main section + +;Funciton to display message asking character 1 to enter the answer. +input1: + mov edx, character1inputlen + mov ecx, character1input + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;system call number (sys_write) + int 0x80 ;invoke the kernel to print the message + + ret ;return to the main section + +;Funciton to display message asking character 2 to enter the answer. +input2: + mov edx, character2inputlen + mov ecx, character2input + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;system call number (sys_write) + int 0x80 ;invoke the kernel to print the message + + ret ;return to the main section + + + +dislplaywinner: + mov edx, winnerlen + mov ecx, winner + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;system call number (sys_write) + int 0x80 ;invoke the kernel to print the message + ret + +comparecharacter : + + + ;mov eax, 3 ; read from keyboard using system call SYS_READ + ;mov ebx, 2; stdin + ;mov ecx, score ; Move the character entered by the user into variable guess + ;mov edx, 4 ; As four letters length is 4 bytes + ;syscall ; Invoke the kernel to get the user input + ;xor rax,rax + ;xor rbx,rbx + mov eax, [score] ; move the value in the variable guess into register eax + mov ebx, [score2] + cmp eax, ebx ; compare correct answer with what is in eax (the guess by the user) + je drawcharacter + jg character1 ; if value in eax (the guess by the user) and variable answer (the correct answer) are the same jump to same subroutine + call character2 + call done + ;call character 2wins ; go to notSame subroutine if not same + + + +drawcharacter: + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ecx, draw; + mov edx, drawlen; length of message + int 80h ; interrupt2 + ret + + +character1: + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ecx, character1winner; print inc message + mov edx, character1winnerlen; length of message + int 80h ; interrupt2 + ret + + +character2: + mov eax, 4 ; system call the screen + mov ebx, 1 ; standard out + mov ecx, character2winner; print inc message + mov edx, character2winnerlen; length of message + int 80h ; interrupt2 + ret + + +;Function for displaying quess to the character. +displayques: + mov edx, [r9] ;quesLen message length + mov ecx, [rdi] ;ques message to write + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;invoke the kernel to print the message + int 0x80 ;call kernel + ret ;return to the main section + +;Function for displaying quess to the character. +displayexpl: + mov edx, [r8] ;quesLen message length + mov ecx, [r10] ;ques message to write + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;invoke the kernel to print the message + int 0x80 ;call kernel + ret ;return to the main section + + +; Function to display the correct answer sentence +displayletterMessage: + mov edx, letterMessageLen ;message length + mov ecx, letterMessage ;message to write + mov ebx, 1 ;file descriptor (stdout) + mov eax, 4 ;system call number (sys_write) + int 0x80 ;invoke the kernel to print the message + ret ; return to the main section + +; Function to end the program +done: + mov eax, 1 ;system call number (sys_exit) + int 0x80 ;invoke the kernel to end the program