you need to learn how to run SPIM on the code produced by your project and also how do output through syscalls. the following spim codes illustrate the syscall mechanism and the structure of a spim program. they also show a few more useful commands.
consider that the spim program is divided into regions for data and instructions. data is delimited by a ".data" command, whereas code is shown by ".text" command. you can toggle between these two regions in a spim program.
also notice the use of the labelled statement ".asciiz" to store a string called "greet".
# This is a simple program to print hello world # a comment starts with a # till the end of the line .data # start putting stuff in the data segment greet: .asciiz "Hello world\n" # declare greet to be the string .text # start putting stuff in the text segment main: # main is a label here. Names a function li $v0, 4 # system call code for print_str(sect1.5 appen) la $a0, greet # address of string to print syscall # print the string jr $ra # return from main # here li is load immediate into an integer register # la is load computed address into an integer register # jr is standard return from function call...$ra contains return # address
the use of syscall here is standard: the system call code is loaded into $v0 (see table 1 in appendix) and then the arguments into registers $a0-$a3 (or $f12 for floating point numbers). return values, if any, are returned in register $v0. there are 4 types of writes and reads each.
this next program shows how to print an integer value and to code a loop with its loop test using a branch instruction. note the data statement here appears at the end of the program.
# this program adds the numbers 1 to 10 .text main: move $t0, $zero # initialize sum (t0) to 0 move $t1, $zero # initialize counter (t1) to 0 loop: addi $t1, $t1, 1 # increment counter by 1 add $t0, $t0, $t1 # add counter to sum blt $t1, 10, loop # if counter < 10, goto loop # this is outside the loop # code to print the sum li $v0, 1 # system call for print_int move $a0, $t0 # the sum to print syscall # print the sum # code to print a newline li $v0, 4 # system call for print_str la $a0, newline # address of str to print syscall # wind up the program jr $ra # return from main .data newline:.asciiz "\n" # declare the string newline # note, the decl follows the use. # it may also be within the code as long # as we toggle between .text and .data # correctly
the third program demonstrates the floating instructions. notice that we toggle from .data to .text to .data here
# this program evaluates 3.1428 + 2.7182 + 17 # the numbers are not meaningful out here but are approximations to # pi, e and 17 is a prime number .data pi: .float 3.1428 .text main: li.s $f2, 2.7182 # load immediate e(2.7182) into float.reg.f2 l.s $f4, pi # note, even numbered floating reg. used add.s $f2, $f2, $f4 # add pi and e. Result in f2 li $t0, 17 # load 17 into a CPU reg. mtc1 $t0, $f6 # move 17 to f6 cvt.s.w $f4, $f6 # convert 17 to a floating number, result in f4 add.s $f2, $f4, $f2 # add 17.0 to pi + e li $v0, 2 # system call for print_float mov.s $f12,$f2 # notice the two stepped move from f2 to f12 syscall li $v0, 4 # system call for print_str la $a0, newline # address of str to print syscall jr $ra # return .data newline:.asciiz "\n"
the output obtained when running these three programs:
46 remus!spim> spim -file hello.s SPIM Version 6.0 of July 21, 1997 Copyright 1990-1997 by James R. Larus (email@example.com). All Rights Reserved. See the file README for a full copyright notice. Loaded: /usr/local/lib/spim/trap.handler Hello world 47 remus!spim> spim -file add.s SPIM Version 6.0 of July 21, 1997 Copyright 1990-1997 by James R. Larus (firstname.lastname@example.org). All Rights Reserved. See the file README for a full copyright notice. Loaded: /usr/local/lib/spim/trap.handler 55 48 remus!spim> spim -file floats.s SPIM Version 6.0 of July 21, 1997 Copyright 1990-1997 by James R. Larus (email@example.com). All Rights Reserved. See the file README for a full copyright notice. Loaded: /usr/local/lib/spim/trap.handler 22.861000 49 remus!spim>