ASCII Hangman Assembly Game | 2008-04-04

Portfolio » Application Programming

Create a hangman game in assembly as described in class. 

1) You will store your first and lastname as words in an array. 
2) The player will be prompted to guess letters.
3) If it is a correct guess, you will print out the correct letter.
4) If it is an incorrect guess, you will
         a) store the incorrect letter in an array
         b) print out the entire list of incorrect letters guessed so far
         c) draw out a part of the hangman using ASCII art.
5) You will then prompt the user to guess the next letter.
6) the game ends when either the player guesses your first and last name OR makes 6 incorrect guesses, at which point the entire hangman is drawn out.  Be sure to print out a WINNING MESSAGE or a LOSING MESSAGE at the end of the game.

This is an example of what the user interface should look like:

|========
||             |
||             |
||            ()
||            /|\
||          
||
||
||
||+++++++++++
++++++++++++


||================||
||  AN**DY_N**         ||
||================||
INCORRECT GUESSES:  B D F W

GUESS A LETTER:

...

#Written by [email protected]

.data   

a1:            .word 'a' 'n' 'd' 'y' ' ' 'n' 'g'
a2:            .word '-' '-' '-' '-' '-' '-' '-'
n:            .word 8
cnt:        .word 6    # Chance Left
pos:        .word 0
rslt:        .word 1
norslt:     .word 0

# Instructions
letter1:    .asciiz "Your letter guess is correct! Go ahead."
letter2:    .asciiz "Sorry! Your letter guess is wrong, lose one chance."
rslt1:        .asciiz "Congratulations! You answer is correct!"
rslt2:        .asciiz "Game Over!"
rslt3:        .asciiz "Correct answer should be: "
rslt4:        .asciiz "Your good guess are: "
ask1:        .asciiz "Name: "
ask2:        .asciiz "Guess a letter: "
chance:        .asciiz "Chances left: "

# ASCII Graphs
graph0:            .asciiz "_______    \n"
graph1:            .asciiz "|   |        \n"
graph2:            .asciiz "|   O        \n"
graph3_1:        .asciiz "|  /        \n"
graph3_2:        .asciiz "|  /|        \n"
graph3_3:        .asciiz "|  /|\        \n"
graph4_1:        .asciiz "|  /        \n"
graph4_2:        .asciiz "|  / \        \n"
graph5:            .asciiz "|            \n"

############################################################################
.text

.globl main
main:

display:
    # print new line       
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall


    # print user ask prompt
    addi $v0, $zero, 4        # 4 == print string
    la $a0, ask1            # print "NAME: "
    syscall

    la $t0, n                # $t0 = address of n
    lw $s0, 0($t0)            # $s0 = n
    la $s1, a2                # $s1 = address of a2
    add $s2, $0, $0            # $s2 = 0
    add $s3, $0, $0            # $s3 (i) = 0
   
############################################################################

loop1:
    ***************************************
    ***************************************
    ***************************************
    j loop1
   
############################################################################

end1:
    # print new line       
    addi $v0, $zero, 11        # 11 == print character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall

    addi $v0, $zero, 4        # 4 == print string
    la $a0, ask2            # print "Guess a letter: "
    syscall

    # capture user input
    addi $v0, $zero, 12        # $v0 = read character
    syscall                    # $v0 = char read

    la $t0, pos                # $t0 = address of pos
    lw $s3, 0($t0)            # $s0 = pos
    la $s1, a1                # $s1 = address of a1
    add $s2, $0, $0            # $s2 = 0

    sll $t0, $s3, 2            # $s0 = pos * 4
    add $t1, $s1, $t0        # $t1 = address of a1[i]

    ******

    beq $v0, $t4, goon1

    la $s0, rslt
    la $s2, norslt
    lw $s3, 0($s2)

    sw $s3, 0($s0)
   
    la $s0, cnt                # $s0 = address of cnt
    lw $s1, 0($s0)            # $s1 = cnt
    sub $s1, $s1, 1            # $s1 = $s1 - 1
    la $s2, cnt
    sw $s1, 0($s2)

    addi $s1, $s1, 48

    # print new line       
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall

    # print user prompt
    addi $v0, $zero, 4        # 4 == print string
    la $a0, letter2            # print "Sorry! Your letter guess is wrong, lose one chance."
    syscall

    # print new line       
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall

    # print user prompt
    addi $v0, $zero, 4        # 4 == print string
    la $a0, chance            # print "Chances left: "
    syscall
   
    # print left chance       
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, $s1        # 10 == ASCII value of new line
    syscall
   
    sub $s1, $s1, 48


    addi $v0, $zero, 11        # 11 == print character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall
   
    # show graph base on counting chance left
    beq $s1, 5, showgraph1   
    beq $s1, 4, showgraph2
    beq $s1, 3, showgraph3
    beq $s1, 2, showgraph4
    beq $s1, 1, showgraph5
    beq $s1, 0, showgraph6

############################################################################

aftergraph:
    beq $s1, 0, incorrect
    ***
   
############################################################################

goon1:
   ************************

    sll $t0, $s3, 2            # $s0 = pos * 4
    add $t1, $s1, $t0        # $t1 = address of a2[i]

    sw $v0, 0($t1)            # set entered character to a2

    # print new line       
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall

    # print user prompt
    addi $v0, $zero, 4        # 4 == print string
    la $a0, letter1            # print "Your letter guess is correct! Go ahead"
    syscall
   
############################################################################

goon2:
    # print new line       
    addi $v0, $zero, 11        # 11 == print character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall
   
    la $s0, pos                # $s0 = address of pos
    lw $s1, 0($s0)            # $s1 = pos
    addi $s1, $s1, 1        # $s1 = $s1 + 1
    la $s2, pos
    sw $s1, 0($s2)   
   
    beq $s1, 8, result

    j display
   
############################################################################

result:
    # print new line       
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall


    # print user prompt
    addi $v0, $zero, 4        # 4 == print string
    la $a0, rslt3            # print "Correct answer should be: "
    syscall

    la $t0, n                # $t0 = address of n
    lw ******            # $s0 = n
    la ***, a1                # $s1 = address of a2
    add $s2, $0, $0            # $s2 = 0
    add $s3, $0, $0            # $s3 (i) = 0
   
############################################################################

loop3:
    slt $t0, $s3, $s0        # $t0 = 1 if i < n
    beq $t0, $zero, end3    # exit loop if i >= n
   
    # load b[i] into register
    sll $t0, $s3, 2            # $s0 = i * 4
    add $t1, $s1, $t0        # $t1 = address of a2[i]
    lw $s4, 0($t1)            # $s4 = b[i]
   
    # loop body
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, $s4        # ASCII value of b[i]
    syscall
   
    addi $s3, $s3, 1        # i++
    j loop3
   
############################################################################

end3:
    # print new line       
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall


    # print user prompt
    addi $v0, $zero, 4        # 4 == print string
    la $a0, rslt4            # print "Your good guess are: "
    syscall

    la $t0, n                # $t0 = address of n
    lw $s0, 0($t0)            # $s0 = n
    la $s1, a2                # $s1 = address of a2
    add $s2, $0, $0            # $s2 = 0
    add $s3, $0, $0            # $s3 (i) = 0
   
############################################################################

loop4:
    slt $t0, $s3, $s0    # $t0 = 1 if i < n
    beq $t0, $zero, end4    # exit loop if i >= n
   
    # load b[i] into register
    sll $t0, $s3, 2            # $s0 = i * 4
    add $t1, $s1, $t0        # $t1 = address of a2[i]
    lw $s4, 0($t1)            # $s4 = b[i]
   
    # loop body
    addi $v0, $zero, 11        # 11 == print_character
    add $a0, $zero, $s4        # ASCII value of b[i]
    syscall
    addi $s3, $s3, 1        # i++
    j loop4
   
############################################################################

end4:
    la $s0, rslt
    lw $s1, 0($s0)
   
    beq $s1, 0, incorrect

    # print new line       
    addi $v0, $zero, 11        # 11 == print character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall

    addi $v0, $zero, 4        # 4 == print string
    la $a0, rslt1            # print "Congratulations! Your answer is correct!"
    syscall
   
    j end
   
############################################################################

incorrect:
    # print new line       
    addi $v0, $zero, 11        # 11 == print character
    add $a0, $zero, 10        # 10 == ASCII value of new line
    syscall

    addi $v0, $zero, 4        # 4 == print string
    la $a0, rslt2            # print "Game Over!"
    syscall
   
############################################################################

showgraph1:
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph0            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph1            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph2            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph5            # print graph
    syscall
   
    j aftergraph
   
showgraph2:
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph0            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph1            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph2            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph3_1            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph5            # print graph
    syscall
   
    j aftergraph
   
showgraph3:
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph0            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph1            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph2            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph3_2        # print graph
    syscall
   
    j aftergraph
   
showgraph4:
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph0            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph1            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph2            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph3_3        # print graph
    syscall
   
    j aftergraph

showgraph5:
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph0            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph1            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph2            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph3_3        # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph4_1        # print graph
    syscall
   
    j aftergraph
   
showgraph6:
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph0            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph1            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph2            # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph3_3        # print graph
    syscall
   
    addi $v0, $zero, 4        # 4 == print string
    la $a0, graph4_2        # print graph
    syscall
   
    j end
   
############################################################################

end:

*** indicated that some partial codes has been removed