1 / 16

Week 16

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 .

libby
Télécharger la présentation

Week 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 16 Assembly

  2. 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 

  3. 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

  4. ## ## 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"

  5. Running joke example in PCSpim Register Pane Assembler Pane Data Pane Messages Pane

  6. 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

  7. Quick recap 2 - System Services

  8. 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’)

  9. 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

  10. 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

  11. 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 

  12. 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

  13. Group task • Start implementing the algorithm. • You’ve already seen all the instructions you need to use. • Keep it simple.

  14. 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"

  15. 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"

  16. 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

More Related