Assembly Language Programs

 

1.      All spim programs end with a “.s” extension, e.g. file.s  Be sure to use comments frequently.  Comments begin with # and go to the end of the line.

 

2.      Integer/float/string declarations should be done after the .data  e.g.

 

 .data

str1:  .asciiz “hello world. \n”

 

This defines a null terminated string str1.  Note that \n and \t have the same meaning as in C.

 

num:  .word  17    #  same as int num = 17;

 

 

3.      The instruction area starts with .text and __start: lines, as below:

 

            .text

__start:           # equivalent to main()

 

4.      See Appendix A in the text for some system calls.  Note that you should use

 

li $v0, 10

syscall

 

rather than done or exit.

 

 

Using XSPIM to debug and run assembly programs

 

1.      Start a session of Xwin32 by clicking the Xwin32 icon.

2.      Log on to program.cs.fsu.edu using your login name and password.

3.      Go to the directory where you have your assembly programs.

4.      Type xspim &.

 

 This opens the xspim interface (see p. A-41).

a)     The top half of the display shows values of all registers & change as the program is run.

b)     Below this are useful buttons for running the simulation.

c)     Next are the Text and Data segment panes.

d)     Last is the window where SPIM will print any error messages.

 

Loading an Assembly Program

 

Press the “load” button.  A window will appear asking for a filename.  Type in the name of your program and press “assembly file.”  xspim will proceed to load and assemble your program

file.s

 

Excecuting an Assembly Program

 

If there are any errors, xspim will show these in the Message pane below the Data segment pane.

Note that each time you run a program you need to:

-        clear memory & registers: press clear button & release it on “registers & memory”

-        load a program:  click on load button, enter file.s in dialog box, click on “assembly” or return key

-        run program:  click the run button & click on “ok” in dialog box.

 

Make sure you check the Message pane at the bottom of the xspim window after the load step to check for assembly errors.  If you run a program that did not assemble correctly, the behavior will not be as expected.

 

Debugging an Assembly Program

 

Debugging with xspim is typically single-stepping through each instruction in the program while watching the changes in the state of the registers.  To step through your program you must first declare a breakpoint.  When xspim reaches the breakpoint it halts so you can see all register contents.  After observing the state, you can instruct xspim to finish executing in its entirety or until it reaches another breakpoint, or you can step through your program, line by line.

 

Setting Breakpoints:  To set a breakpoint you must first know the address of the instruction at which you want to suspend execution (the address is the first column of the Text segment pane in the xspim window).  Press the “Breakpoints” button, then type in the address of the breakpoint and press “add.”  When you run your program, xspim will suspend execution at the breakpoint you set.  To continue execution of your program, press the “Step” button which brings up the step window, allowing you to either step through your program (press “step”) or continue execution of your program (press “continue”).

 

Note that you can set multiple breakpoints.  The “List” button in the Breakpoints window shows all the current breakpoints set by the user.

 

Control buttons in xspim

 

1.      quit:  quit xspim

2.      load:  load assembly program, e.g. file.s

3.      run:  run program.  Note you need to clear register & memory contents and reload the program if you have changed anything.

4.      step: single stepping used with breakpoints to debug the program.  It asks for step size (“Number of steps”) which is the number of instructions which will be performed every time step is clicked.  Best to leave at 1 and click “ok.”

5.      clear: used to clear register and/or memory

6.      set value:  can be used to set the value of any register which you may need in the program, e.g. $a0 = 17

7.      print: can be used to print various memory locations or global symbols used

8.      breakpoint: as described above, used to set/delete/list breakpoints

 

 

Terminal interface spim

 

A simpler version of SPIM, spim, does not require a bit-mapped display and can be run from any terminal.  This is UNIX–based and we will use it in discussions and in recitations.  Once you have removed all syntax errors, but the results are not as you expected, each time you enter the following, the next line of code is executed

 

(spim): step

 

allowing you to step through your program line by line (once you have loaded your program).  After each step you can print the contents of any register, say $t0 by

 

(spim):  print $t0

 

This is very helpful in debugging any logic errors you may have.

 

See p. A-45 for other commands.

 

General Form of Assembly Language Program

 

           

            .data

            <declarations of variables to be passed to function>

            .text

            .align 2

__start:

            <instructions to load values into registers to be used in function>

            jal <function>

            li $v0, 10                    # exit/done

            syscall

function:

            <function instructions>

            jr $31

 

 

 

You will run your programs on program.cs.fsu.edu as follows:

 

ssh program.cs.fsu.edu

<login>

spim

(spim): load “file.s”

(spim): run

 

Note:  all MIPS programs end with the ‘.s’ extension as shown above.  Be sure to include the double quotes around the name of your file as shown for “file.s”

 

 

Sample Program to add 2 to a number:

 

            .data                 # data part of program

numb:   .word 6             # .word declares a value

            .text

            .align 2

__start:

            la $a1, numb    # get address of numb

            jal func              # go to func ( a routine to add 2 to a number)

            li $v0, 10        #system code for exit/done

            syscall

func:

            lw $t0, 0($a1)              # address of numb passed to func

            addiu $t0, $t0, 2                     # add 2 to num

            li $v0, 1                         # system code to print integer

            or $a0, $zero, $t0            # put integer in $a0

            syscall

            jr $31