Lec09 1
Lec09 1
LECTURE 09-1
TODAY
▸ We'll look at MIPS32 assembly language programs.
▸ We'll run them using the SPIM simulator.
▸ I've posted the examples on-line, along with a copy of SPIM.
LECTURE 09-1 MIPS ASSEMBLY
▸ This MIPS32 program outputs a prompt, gets an input, sums up to that value.
1. .data
2. prompt: .asciiz "Enter an integer: "
3. feedback: .asciiz "The sum from 1 up to its value is "
4. ending: .asciiz ".\n"
5.
6. .globl main
7. .text
8. main:
9. li $t0, 0 # sum = 0
10. li $t1, 1 # inc = 1
11. li $t2, 0 # count = 0
12.
13. li $v0, 4 # print(prompt) -- syscall #4
14. la $a0, prompt #
15. syscall #
16. li $v0, 5 # last = input() -- syscall #5, result in $v0
17. syscall #
18. move $t3, $v0 #
19.
20. loop:
21. bgt $t3, $t2, done # if last == count goto done
22. add $t2, $t2, $t1 # count += inc
23. add $t0, $t0, $t2 # sum += count
24. b loop
25.
LECTURE 09-1 MIPS ASSEMBLY
▸ This MIPS32 program outputs a prompt, gets an input, sums up to that value.
20. loop:
21. bgt $t3, $t2, done # if last == count goto done
22. add $t2, $t2, $t1 # count += inc
23. add $t0, $t0, $t2 # sum += count
24. b loop
25.
26. done:
27. li $v0, 4 # print(feedback)
28. la $a0, feedback #
29. syscall #
30.
31. li $v0, 1 # print(sum)
32. move $a0, $t0 #
33. syscall #
34.
35. li $v0, 4 # print(ending)
36. la $a0, ending #
37. syscall #
38.
39. li $v0, 0 # return 0
40. jr $ra #
LECTURE 09-1 MIPS ASSEMBLY
A VARIANT OF SUMMING
▸ Here is a similar loop, but counts down, and skips summing each odd value...
1. li $t0, 0 # sum = 0
2. move $t2, $t3 # count = last
3.
4. loop:
5. beqz $t2, done # if count == 0 goto done
6. andi $t4, $t2, 0x1 # tmp = count % 2
7. bgtz $t4, skip_add # if tmp > 0 go to skip_add
8.
9. dont_skip_add:
10. add $t0, $t0, $t2 # sum += count
11.
12. skip_add:
13. addi $t2, $t2, -1 # count -= 1
14. b loop
15. done:
LECTURE 09-1 MIPS ASSEMBLY
put_int(product);
LECTURE 09-1 MIPS ASSEMBLY
main
put(t0);
LECTURE 09-1 MIPS ASSEMBLY
main
statements. put(t0);
LECTURE 09-1 MIPS ASSEMBLY
main
instructions. put(t0);
LECTURE 09-1 MIPS ASSEMBLY
➡ You tell the system which should run by setting register v0 to that number.
LECTURE 09-1 MIPS ASSEMBLY
SYSTEM CALLS
▸ Getting an integer input is system call #5.
t1 = get_int(); LI $v0,5
syscall
MOVE $t1,$v0
▸ Meaning of the MIPS code:
• We load register v0 with the system call number.
• We make the system call.
➡A special sequence of instructions gets executed by the system to read input
from the console. The integer read gets placed in register v0.
• We copy (i.e. "move") its value to register t1, our storage for the first multiplicand.
LECTURE 09-1 MIPS ASSEMBLY
SYSTEM CALLS
▸ Outputting an integer is system call #1.
put_int(t0); LI $v0,1
MOVE $a0,$t0
syscall
▸ Meaning of the MIPS code:
• We load register v0 with the system call number.
• We load register a0 with the "argument", the value we want the system to output.
➡This has us copy (again, "move") the register for the product into a0.
▸ We'll see (on Friday) how to write functions and procedures and use them.
▸ These two instructions hint at how that works. In short:
• Functions, by convention, return a value by setting register v0 to that value.
• A register named ra stores information about who called main.
➡ This is called the return address of the "caller" code.
➡The JR instruction "jumps back" to that instruction.
▸ For now, just make sure your main code always ends with these two lines.
LECTURE 09-1 MIPS ASSEMBLY