Sunteți pe pagina 1din 3

LAB 2

Tutorial 1

In this tutorial, you will learn how to use branch and jump instructions.

1. Type these instructions into your word editor and save it as Lab2a.s into folder lab
KP30103 you have created before.

#####################################
# LOOP
#####################################

# This exercise will count the sum between 0..10

########################
# Data segment
########################
.data

# Create words
ten: .word 10
one : .word 1
total: .word 0

########################
# Text segment
########################
.text
main: lw $s0, ten # Load word into $s0 from memory[ten]
lw $t0, one # Load word into $s0 from memory[one]
move $s1, $s0 # Copy ($s0) to $s1

loop: sub $s0, $s0, $t0 # ($s0)=($s0)-1


add $s1, $s0, $s1 # ($s1)=($s0)+($s1)
beqz $s0, done # The execution branch to 'done' if ($s0)=0
j loop # The execution jump back to 'loops'

done: sw $s1, total # Store ($s1) into memory[total]


li $v0, 10 # syscall number 10 will exit from program
syscall # actually exit the program

2. Load the program into PCSpim.


3. Simulate the program by pressing F10.
4. The result of each loop is stored in register $s1. The value stored in register $s1 is
changed after every loop.
5. The final result is transferred into memory with an address represented by total from
register $s1.
Tutorial 2

($v0) Function Arguments


Output 1 Print integer Integer in $a0
2 Print floating-point Float in $f12
3 Print double-float Double-float in $f12, $f13
4 Print string Pointer in $a0
Input 5 Read integer
6 Read floating-point
7 Read double-float
8 Read string Pointer in $a0, length in $a1
Control 9 Allocate memory Number of bytes in $a0
10 Exit from program

Table 1

Table shows the input/output and control functions of syscall in PCSpim. Suppose you want to
read a string from a user and store the string into memory as follows:

.data
theString: .space 64

.text
main:
li $v0, 8
la $a0, theString
li $a1, 64
syscall

li $v0,10
syscall

The ".space 64" sets aside 64 bytes for use of whatever purpose we want, the first byte of which
may be referenced by the label "theString:", which appears on the line before.

li $v0, 8

When you call "syscall" in your code, a value called the "system call code" will determine what
function syscall performs. The "system call code" is stored in the register $v0. The system call
code for reading a string is 8.

la $a0, theString

If you look at the "arguments" column in the table, it says "$a0 = buffer, $a1 = length". What this
means is that the $a0 register must be set to the location in memory to which the computer will
record the input. This is accomplished by loading the address (la) of theString into $a0.
li $a1, 64

The second part of the arguments entry in the table says "$a1 = length", which you set to the
maximum number of characters that should be read in. You can have it be 50, or 200, or 37, or
whatever you like, but you shouldn't go above 64 (in this example) because in the first part of
this program you only set aside 64 bytes using the ".space" directive. Note that this space set
aside includes the terminating null '\0' character, so it will actually read only up to 63 characters
from the buffer when the syscall executes.

syscall

At long last, having set your argument ($a0, $a1) registers and your call code register ($v0), you
call syscall. Since $v0 == 8 the simulator knows to read in a line from the SPIM console, and to
write the input to the memory location referenced by $a0 (which was set to theString), for a
string of maximum length of $a1 (which we set to 64).

It reads input until it encounters a '\n' character or reaches the maximum number of characters
it can reach (which we stored in $a1 as 64), and stores that input (including the '\n' character)
into a string null-terminated with a '\0'. Notice that the maximum length of the string includes
the '\0' terminating null character.

Simulate the above codes by pressing the F5 key. The simulator console will pop up. Type Hello
on the console followed by pressing the enter key. The data memory will store the ascii values of
the entered text. The ascii values of characters can be found in ascii table
(http://www.asciitable.com/).

To read integer value instead of string, the following codes are used:

li $v0, 5
syscall

Code for reading integer is 5. The above code requests user to enter an integer value. The
entered value will be kept in register $v0.

sw $v0, theString

S-ar putea să vă placă și