MIPS Programming Assembly Language CDA 3101 Discussion Section 03

Report
CDA 3101 Discussion Section 03
MIPS
Assembly Language
Programming
1
Outline
• MIPS simulator – PCSpim
• Problems from textbook
2.29, 2.30
2
PCSpim
• Installation
1. From the textbook CD
2. From the internet
http://www.cs.wisc.edu/~larus/spim.html
3
Writing 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;
}
4
PC Spim
5
PC Spim
• Note the top window – it contains the
state of all registers.
6
PC 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.
7
PC 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.
8
PC Spim
• Click this menu item to change settings
for the emulator.
9
PC 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.
10
PC 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.
11
Understanding 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?
12
Understanding MIPS code
• Problem 2.30
sll
sll
add
add
outer: add
lw
add
inner: add
lw
bne
addi
skip:
addi
bne
addi
bne
$a2, $a2, 2
$a3, $a3, 2
$v0, $zero, $zero
$t0, $zero, $zero
$t4, $a0, $t0
$t4, 0($t4)
$t1, $zero, $zero
$t3, $a1, $t1
$t3, 0($t3)
$t3, $t4, skip
$v0, $v0, 1
$t1, $t1, 4
$t1, $a3, inner
$t0, $t0, 4
$t0, $a2, outer
13
Understanding 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, $zero
loop: beq
$a1, $zero, finish
add
$t0, $t0, $a0
sub
$a1, $a1, 1
j
loop
finish: addi $t0, $t0, 100
add
$v0, $t0, $zero
14

similar documents