Using MARS


  1. Check the MIPS Help Pages.

  2. Download MARS Mars.jar, a MIPS assembler and runtime simulator, at https://dpetersanderson.github.io/ to a folder such as c:\Mars .
  3. The result jar archive is therefore located at c:\Mars\Mars4_5.jar, for example.

  4. Download and install Java J2SE 1.5 (or later) SDK on your computer if it is not there yet.

  5. Double-click the file name such as Mars4_5.jar, an executable jar file, and the following MARS IDE window will pop up:



  6. Write a MIPS assembly program such as C:\Mars-workspace\Fib.asm by using the MARS editor or any text editor such as Notepad or Wordpad:

  7. C:\Mars-workspace\Fib.asm
    01# Compute first twelve Fibonacci numbers and put in array, then print.
    02 
    03        .data
    04fibs:   .word  0 : 12         # "array" of 12 words to contain fib values
    05size:   .word  12             # size of "array"
    06        .text
    07 
    08main:
    09        la    $t0, fibs        # load address of array
    10        la    $t5, size        # load address of size variable
    11        lw    $t5, 0($t5)      # load array size
    12        li    $t2, 1           # 1 is first and second Fib. number
    13        add.d $f0, $f2, $f4
    14        sw    $t2, 0($t0)      # F[0] = 1
    15        sw    $t2, 4($t0)      # F[1] = F[0] = 1
    16        addi  $t1, $t5, -2     # Counter for loop, will execute (size-2) times
    17 
    18loop:   lw    $t3, 0($t0)      # Get value from array F[n]
    19        lw    $t4, 4($t0)      # Get value from array F[n+1]
    20        add   $t2, $t3, $t4    # $t2 = F[n] + F[n+1]
    21        sw    $t2, 8($t0)      # Store F[n+2] = F[n] + F[n+1] in array
    22        addi  $t0, $t0, 4      # increment address of Fib. number source
    23        addi  $t1, $t1, -1     # decrement loop counter
    24        bgtz  $t1, loop        # repeat if not finished yet.
    25        la    $a0, fibs        # first argument for print (array)
    26        add   $a1, $zero, $t5  # second argument for print (size)
    27        jal   print            # call print routine.
    28        li    $v0, 10          # system call for exit
    29        syscall                # we are out of here.
    30                 
    31 
    32#########  Routine to print the numbers on one line.
    33 
    34        .data
    35space:  .asciiz  " "           # space to insert between numbers
    36head:   .asciiz  "The Fibonacci numbers are:\n"
    37        .text
    38 
    39print:
    40        add   $t0, $zero, $a0  # starting address of array
    41        add   $t1, $zero, $a1  # initialize loop counter to array size
    42        la    $a0, head        # load address of print heading
    43        li    $v0, 4           # specify Print String service
    44        syscall                # print heading
    45 
    46out:    lw    $a0, 0($t0)      # load fibonacci number for syscall
    47        li    $v0, 1           # specify Print Integer service
    48        syscall                # print fibonacci number
    49        la    $a0, space       # load address of spacer for syscall
    50        li    $v0, 4           # specify Print String service
    51        syscall                # output string
    52        addi  $t0, $t0, 4      # increment address
    53        addi  $t1, $t1, -1     # decrement loop counter
    54        bgtz  $t1, out         # repeat if not finished
    55        jr    $ra              # return

  8. Open the program by selecting the following options from the MARS interface:
         File ⇒ Open
  9. Assuming the file Fib.asm is opened, the interface will be as follows:




  10. Assemble the program by selecting the following options from the MARS interface:
         Run ⇒ Assemble



  11. Execute the program by selecting the following options from the MARS interface:
         Run ⇒ Go





      What do you get when you cross a dyslexic, an insomniac, and an agnostic?    
      Someone who lays awake at night wondering if there is a dog.