Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
; 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
correct db "You guess correctly", 10
corlen equ $-correct
;Message that the guess is incorrect
;Size of the message
incorrect db "Your guess is incorrect", 10
incorlen equ $-incorrect
; Message asking question
; Size of the message
question1 db "Guess the letter I am thinking of", 10
questionLen1 equ $-question1
question2 db "Guess the letter I am thinking of", 10
questionLen2 equ $-question2
question3 db "Guess the letter I am thinking of", 10
questionLen3 equ $-question3
question4 db "Guess the letter I am thinking of", 10
questionLen4 equ $-question4
question5 db "Guess the letter I am thinking of", 10
questionLen5 equ $-question5
listQuotes:
dq question1 ; the pointer to the quotes are stored in 8 bytes
dq question2
dq question3
dq question4
dq question5
;the array we use to point to the quote lengths
listQLen:
dq questionLen1 ; the pointer to the quote lengths are stored in 8 bytes
dq questionLen2
dq questionLen3
dq questionLen4
dq questionLen5
; 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 'h' ; the answer letters are stores in 8 bytes to aid the comparison
dq 'l'
dq 'h'
dq 'l'
dq 'h'
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 Correct ; if guess was correct jump to same function
call Incorrect ; if the guess is incorrect then go to Notsame function
ret
; function to show message that answer was not correct answer
Incorrect:
mov ecx,incorrect ; Not same message
mov edx, incorlen ; 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
Correct:
mov ecx,correct ; same message
mov edx, corlen ; 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, [r9] ;message length content of register r9
mov ecx, [r8];message to write content of register r8
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