Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Guessuing_Game/Guessing_Game.asm
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
186 lines (152 sloc)
5.7 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; assembly code for simple letter guessing game | |
; the game prints some information to the screen | |
; As an array of the guesses over the time | |
; Has comparison of the guess with the actual letter thinking of | |
; Some of the concepts in here could be adapted for use your coursework Task 2 | |
section .data | |
;Welcome message for game | |
;Welcome message size | |
welcome db "Welcome to this simple guessing game", 10 | |
welcomeLen equ $-welcome | |
;Message that the guess is correct | |
;Size of the message | |
samemsg db "You guess correctly. They are the same. ", 10 | |
samelen equ $-samemsg | |
;Message that the guess is incorrect | |
;Size of the message | |
notsamemsg db "Your guess is incorrect. They are not the same: ", 10 | |
notsamelen equ $-notsamemsg | |
; Message asking question | |
; Size of the message | |
question db "Guess the letter I am thinking of", 10 | |
questionLen equ $-question | |
; Message explaining the letter that was selected | |
; Message Size | |
letterMessage db "The letter I was thinking of is: " | |
letterMessageLen equ $-letterMessage | |
;Same as endl in C++ | |
cr db 10 | |
;the array we are using to store the answers | |
global listAnswers | |
listAnswers: | |
dq 'a' ; the answer letters are stores in 8 bytes to aid the comparison | |
dq 'b' | |
dq 'c' | |
dq 'd' | |
dq 'e' | |
letter: | |
dq 0 ; where we store each of the answers one at a time | |
segment .bss | |
guess resb 1 ; store the users guess | |
section .text | |
global _start ;must be declared for linker (ld) | |
_start: | |
call displayWelcome | |
call newLine | |
mov rax,5 ;number of answers | |
mov rbx,0 ;RBX will store the letter | |
mov rcx, listAnswers ;RCX will point to the current element array to be guessed | |
;Main function that calls other functions | |
top: | |
mov rbx, [rcx] ; put the current letter being guessed in rbx | |
mov [letter],rbx ; move rbx into a variable letter that stores the current guess | |
push rax ; push rax on stack | |
push rcx ; push rax on stack | |
call displayQuestion ; display the question | |
call reading ; call reading to get the users guess | |
call displayletterMessage ; Display the message for the correct letter | |
call display ; print the letter they should have guessed | |
call newLine ; new line like endl in C++ | |
pop rcx ; get back from stack | |
pop rax ; get back from stack | |
add rcx,8 ;move pointer to next element as 8 bits for each move on by 8 | |
dec rax ;decrement counter so going down | |
jnz top ;if counter not 0, then loop again | |
call done ; end program | |
;Display function | |
display: | |
mov edx,1 ;message length | |
mov ecx, letter ;message to write the letter to be predicted | |
mov ebx, 1 ;file descriptor (stdout) | |
mov eax, 4 ;system call number (sys_write) | |
int 0x80 ;call kernel | |
ret | |
;function to read the user guess and do comparison with the answer | |
reading: | |
mov eax, 3 ; read from keyboard | |
mov ebx, 2; | |
mov ecx, guess ; move guess into ecx | |
mov edx, 1 ; As single letter using 1 byte | |
int 80h ; call interrupt | |
mov rax, [guess] ; move guess by user into rax | |
cmp rax, [letter] ; compare correct answer with what in rax | |
je same ; if guess was correct jump to same function | |
call Notsame ; if the guess is incorrect then go to Notsame function | |
ret | |
; function to show message that answer was not correct answer | |
Notsame: | |
mov ecx,notsamemsg ; Not same message | |
mov edx, notsamelen ; length of same message | |
mov ebx,1 ;file descriptor (stdout) | |
mov eax,4 ;system call number (sys_write) | |
int 80h | |
mov eax, 3 ; read previous enter key press | |
mov ebx, 2; | |
mov ecx, guess ; Deal with previous enter key press so it doesnot messy up loop | |
mov edx, 1 ; As single letter using 1 byte | |
int 80h ; call interrupt | |
ret | |
; function to show message answer was correct | |
same: | |
mov ecx,samemsg ; same message | |
mov edx, samelen ; length of same message | |
mov ebx,1 ;file descriptor (stdout) | |
mov eax,4 ;system call number (sys_write) | |
int 80h | |
mov eax, 3 ; read previous enter key press from keyboard | |
mov ebx, 2; | |
mov ecx, guess ; Deal with previous enter key press so it doesnot messy up loop | |
mov edx, 1 ; As single letter using 1 byte | |
int 80h ; call interrupt | |
ret | |
; Function to create a New line like endl in C++ | |
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 welcome to game message | |
displayWelcome: | |
mov edx,welcomeLen ;message length | |
mov ecx, welcome ;message to write | |
mov ebx, 1 ;file descriptor (stdout) | |
mov eax, 4 ;system call number (sys_write) | |
int 0x80 ;call kernel | |
ret | |
;Function to display question for quiz | |
displayQuestion: | |
mov edx,questionLen ;message length | |
mov ecx, question ;message to write | |
mov ebx, 1 ;file descriptor (stdout) | |
mov eax, 4 ;system call number (sys_write) | |
int 0x80 ;call kernel | |
ret | |
; 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 ;call kernel | |
ret | |
; Function to end the program | |
done: | |
mov eax, 1 ;system call number (sys_exit) | |
int 0x80 ;call kernel |