160 likes | 263 Vues
Week 16. Assembly. Assembly. Quick recap on some assembly bits Work in groups designing program Discussion Coding of program Discussion Take a look at C.Sc132 pages – JAM’s slides & PCSpim stuff are good! This is where bits for some of these slides came from .
E N D
Week 16 Assembly
Assembly • Quick recap on some assembly bits • Work in groups designing program • Discussion • Coding of program • Discussion • Take a look at C.Sc132 pages – JAM’s slides & PCSpim stuff are good! • This is where bits for some of these slides came from
Algorithm for joke example print question message Input v1 If (v1 == 1) then goto print_joke print miserable_git message goto exit print_joke: print joke_question message; print joke_answer message exit : exit the program • If the user entered 1 then we skip to the ‘print_joke’ label • If the user didn’t enter 1 then we print ‘miserable_git’ message and skip to the ‘exit’ label
## ## Joke.s - Tells a joke ## ## text segment .globl main.text main : # execution starts herela $a0,message # Output messageli $v0, 4syscall li $v0, 5 # Read a value syscall move $s1, $v0 beq $s1, 1, tell_joke # Jump if user want to hear a joke la $a0, no_joke # Output message if user didn’t want to hear a joke li $v0, 4 syscall b end # Jump to the end tell_joke : # ‘tell_joke’ label la $a0, jokeq # Output first part li $v0, 4 syscall la $a0, jokea # Output second part syscall end: #’end’ label li $v0, 10 syscall # au revoir ... ## data segment.data message : .asciiz "Want to hear a joke? 1=YES\n" no_joke : .asciiz "Miserable git...\n" jokeq : .asciiz "How do you turn a duck into a soul singer?\n" jokea : .asciiz "Put it in a microwave till it's Bill Whithers.\n"
Running joke example in PCSpim Register Pane Assembler Pane Data Pane Messages Pane
Quick recap 1 • How do we print out a message? • The SPIM simulator provides us with a number of built-in, operating-system-like services or system calls that we can make use of. • One of them is “print_string” and this has the call code 4. It also expects the address of the string to be printed to be held in $a0. • We must also load the call code into $v0 la $a0, message li $v0, 4 syscall
Quick recap 3 • li : load immediate li $v0, 4 • Loads the “immediate” value provided(in this case, ‘4’) into the named register (in this case, ‘$v0’) • la : load address la $a0, message • Loads the address, not the value held at that address, into the named register (in this case, ‘$a0’)
Quick recap 4 • j <addr> • means jump to address <addr> which causes the processor to fetch the next instruction from the word at location <addr> • Implementation : just copy the address to the PC
Quick recap 5 • To make things more interesting, we need conditional branches. • Here are some examples of MIPS branches (instructions beginning with ‘b’). beq $t0, $t1, <addr> #Branch to <addr> if $t0 == $t1 beqz $t0, <addr> #Branch to <addr> if $t0 == 0 bne $t0, $t1, <addr> #Branch to <addr> if $t0 != $t1 blt $t0, $t1, <addr> #Branch to <addr> if $t0 < $t1
Group task • Rock paper scissor game: • Write a program that takes two integers as input. 1 = rock, 2 = paper, 3 = scissors • Print out whether the first input wins, loses or draws with the second input. • Write the algorithm • Remember how your Java version worked? • Think about comparisons and jumps • Take a look at earlier example if all else fails
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
Group task • Start implementing the algorithm. • You’ve already seen all the instructions you need to use. • Keep it simple.
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