io - 1 Reading and Printing Integers For our next example...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1. Reading and Printing Integers For our next example, we'll write a program named add2.asm that computes the sum of two numbers specified by the user at runtime, and displays the result on the screen. The algorithm this program will follow is: 1. Read the two numbers from the user - We'll need two registers to hold these two numbers. We can use $t0 and $t1 for this. 2. Compute their sum- We'll need a register to hold the result of this addition. We can use $t2 for this. 3. Print the sum. 4. Exit. We already know how to do this, using syscall. The only parts of the algorithm that we don't know how to do yet are to read the numbers from the user, and print out the sum. Luckily, both of these operations can be done with a syscall. Looking again in table from lecture 7, we see that syscall 5 can be used to read an integer into register $v0, and and syscall 1 can be used to print out the integer stored in $a0. The syscall to read an integer leaves the result in register $v0, however, which is a small problem, since we want to put the first number into $t0 and the second into $t1. We find the move instruction, which copies the contents of one register into
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/27/2011 for the course CMPSC 64 taught by Professor Zheng during the Fall '09 term at UCSB.

Page1 / 3

io - 1 Reading and Printing Integers For our next example...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online