120 likes | 643 Views
Algorithm for paper rock scissors. Input V1 Input V2 If (v1 = rock) then If (v2 = rock) goto draw If (v2 = paper) goto lost If (v2 = scissors) goto win If (v1 = paper) then If (v2 = rock) goto win If (v2 = paper) goto draw If (v2 =scissors) goto lost If (v1 = scissors) then
E N D
Algorithm for paper rock scissors Input V1 Input V2 If (v1 = rock) then If (v2 = rock) goto draw If (v2 = paper) goto lost If (v2 = scissors) goto win If (v1 = paper) then If (v2 = rock) goto win If (v2 = paper) goto draw If (v2 =scissors) goto lost If (v1 = scissors) then If (v2 = rock) goto lost If (v2 = paper) goto win If (v2 = scissors) goto draw // if we get here, something was wrong with input values print error message; goto exit draw : print draw message ; goto exit lost : print lost message; goto exit win : print win message; goto exit exit : exit the program
Rock paper scissors solution 1 ## ## rps.s - rock, paper, scissors ## ## ## text segment .globl main .text main : # execution starts here la $a0, input_message_1 li $v0, 4 syscall li $v0, 5 syscall move $s1, $v0 la $a0, input_message_2 li $v0, 4 syscall li $v0, 5 syscall move $s2, $v0 beq $s1, 1, rock_in beq $s1, 2, paper_in beq $s1, 3, scissors_in b input_error ## data segment .data win_message : .asciiz "First input wins\n" draw_message : .asciiz "It's a draw\n" lost_message : .asciiz "First input loses\n" input_error_message : .asciiz "Error in input\n" input_message_1 : .asciiz "Input first value : " input_message_2 : .asciiz "Input second value : " finished : .asciiz "Finished!\n"
Rock paper scissors solution 2 rock_in: beq $s2, 1, draw # it's rock beq $s2, 2, lost # it's paper beq $s2, 3, win # it's scissors b input_error paper_in: beq $s2, 1, win # it's rock beq $s2, 2, draw # it's paper beq $s2, 3, lost # it's scissors b input_error scissors_in: beq $s2, 1, lost # it's rock beq $s2, 2, win # it's paper beq $s2, 3, draw # it's scissors input_error : la $a0, input_error_message b print_message win : la $a0, win_message b print_message draw : la $a0, draw_message b print_message lost : la $a0, lost_message print_message : li $v0, 4 syscall ## data segment .data win_message : .asciiz "First input wins\n" draw_message : .asciiz "It's a draw\n" lost_message : .asciiz "First input loses\n" input_error_message : .asciiz "Error in input\n" input_message_1 : .asciiz "Input first value : " input_message_2 : .asciiz "Input second value : " finished : .asciiz "Finished!\n"
Rock paper scissors solution 3 exit : li $v0, 4 # print message la $a0, finished syscall li $v0, 10 syscall # exit ... ## data segment .data win_message : .asciiz "First input wins\n" draw_message : .asciiz "It's a draw\n" lost_message : .asciiz "First input loses\n" input_error_message : .asciiz "Error in input\n" input_message_1 : .asciiz "Input first value : " input_message_2 : .asciiz "Input second value : " finished : .asciiz "Finished!\n" ## ## end of file rps.s