1
MIPS MIPS Assembly LanguageAssembly Language ProgrammingProgramming
CDA 3101 Discussion Section 03CDA 3101 Discussion Section 03
3
PCSpimPCSpim
• Installation1. From the textbook CD2. From the internet
http://www.cs.wisc.edu/~larus/spim.html
4
Writing A Basic ProgramWriting A Basic Program• Let’s start by writing a program that
sums all the numbers between 1 and 10.
int main(){ int i = 10; int sum = 0; do { sum = sum + i; i = i - 1; } while( i != 0 );
return sum;}
7
PC SpimPC Spim
• The button on the top right runs the program to its end, after you click “OK” on the dialog box. Note that you won’t see the register changes until the program ends.
8
PC SpimPC Spim
• Click this menu item to reinitialize PC Spim – it’s like rebooting your computer. It’s often necessary to click Reload to run your program again.
10
PC SpimPC Spim
• Click this menu item to change settings for the emulator.• Sometimes it’s helpful to uncheck “General registers in
hexadecimal” so that you can read values as regular numbers.
11
PC SpimPC Spim
• Click the button that looks like a hand to set breakpoints. The program will stop running at positions you indicate, and wait for your authorization to continue upon reaching said point. You will also see the register values updated.
12
Understanding MIPS codeUnderstanding MIPS code• Problem 2.30
Add comments to the following MIPS code and describe in one sentence what it computes.
The code processes 2 arrays and produces a value in $v0. Each array consists of 2500 words indexed 0 through 2499, the base addresses of the arrays are stored in $a0, $a1, and their sizes are stored in $a2, $a3. What will be returned in $v0?
13
Understanding MIPS codeUnderstanding MIPS code• Problem 2.30
sll $a2, $a2, 2sll $a3, $a3, 2add $v0, $zero, $zeroadd $t0, $zero, $zero
outer: add $t4, $a0, $t0lw $t4, 0($t4)add $t1, $zero, $zero
inner: add $t3, $a1, $t1lw $t3, 0($t3)bne $t3, $t4, skipaddi $v0, $v0, 1
skip: addi $t1, $t1, 4bne $t1, $a3, inneraddi $t0, $t0, 4bne $t0, $a2, outer
14
Understanding MIPS codeUnderstanding MIPS code• Problem 2.29
Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output.$a0: input a; $a1: input b; $v0: output;
add $t0, $zero, $zeroloop: beq $a1, $zero, finish
add $t0, $t0, $a0sub $a1, $a1, 1j loop
finish: addi $t0, $t0, 100add $v0, $t0, $zero