25.12.2012 Views

MIPS Assembly Language Programming

MIPS Assembly Language Programming

MIPS Assembly Language Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

24 CHAPTER 2. <strong>MIPS</strong> TUTORIAL<br />

(spim) print $t0<br />

Reg 8 = 0x00000003 (3)<br />

The print command displays the register number followed by its contents in both<br />

hexadecimal and decimal notation. Note that SPIM automatically translates from<br />

the symbolic name for the register (in this case, $t0) to the actual register number<br />

(in this case, $8).<br />

2.4 Using syscall: add2.asm<br />

Our program to compute 1+2 is not particularly useful, although it does demonstrate<br />

a number of important details about programming in <strong>MIPS</strong> assembly language and<br />

the SPIM environment. For our next example, we’ll write a program named add2.asm<br />

that computes the sum of two numbers specified by the user at runtime, and displays<br />

the result on the screen.<br />

The algorithm this program will follow is:<br />

1. Read the two numbers from the user.<br />

We’ll need two registers to hold these two numbers. We can use $t0 and $t1<br />

for this.<br />

2. Compute their sum.<br />

We’ll need a register to hold the result of this addition. We can use $t2 for this.<br />

3. Print the sum.<br />

4. Exit. We already know how to do this, using syscall.<br />

Once again, we start by writing a comment. From what we’ve learned from<br />

writing add.asm, we actually know a lot about what we need to do; the rest we’ll<br />

only comment for now:<br />

# Daniel J. Ellard -- 02/21/94<br />

# add2.asm-- A program that computes and prints the sum<br />

# of two numbers specified at runtime by the user.<br />

# Registers used:<br />

# $t0 - used to hold the first number.<br />

# $t1 - used to hold the second number.<br />

# $t2 - used to hold the sum of the $t1 and $t2.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!