CSC 435 Computer Organization

More work with SPIM, a MIPS Simulator

Objectives


Today in class we will run SPIM and will try to understand the assembly language and use of registers.  You will save your responses in yourusernameA4.

  1. Read PCSpim.pdf  which is from your text's CD.

  2. Then open PCSpim and change your settings as follows:
    PCSpim Settings
    Note that we have checked the box for allowing pseudoinstructions.  A pseudoinstruction is not a valid assembly language instruction because it cannot be directly implemented in hardware, but PCSpim allows pseudoinstructions, so how does this work?  Pseudoinstructions are typically converted by the MIPS Assembler as more. than one actual MIPS instruction.

  3. Next you will run a MIPS "hello world" program. Note that the  program is divided into regions labeled with the ".data" directive where data is stored, and regions labeled  ".text" directive where code is stored.  Also notice the use of the labeled statement ".asciiz" to store a string named "hi".  Look carefully at using syscalls for output.Copy and paste the following into a Notepad file named hello.asm:
    # This is a spim "hello world" program named hello.asm
    # a spim comment always begins with "#" and continues until the end of the line

    .data # puts information into the data segment
    hi: .asciiz "Hello world!\n"
    # declare hi to be the given string
    .text # puts information into the text segment
    main: # main is a label which names the function main
    li $v0, 4 # system call code for print_str()
    # li is load immediate into an integer register
    la $a0, hi # la is load computed address into an integer register
    syscall # print the string
    jr $ra # return from main
    # jr is jump return from function call
    # $ra contains return address
    # Be sure to include a final return line in your file!

  4. Read the above program and the comment lines.

  5. Optional Aside: If you are interested in why we are using hello world, see Wikipedia.org/wiki/Hello_world.

  6. Next Open PCSpim, load hello.asm using the Open File command, and run it using F5 or the "Go" command.  I know you will be very excited to see the program say hi to the world.... Of course, I also know that you are dying to dig into the code to understand how this works, so I will help with this.  It is a challenge to try to look at everything all at once, so we will look at each portion of PCSpim separately.

  7. First we will try to better understand registers.  Reload hello.asm and do the following:
    1. Note that the registers are displayed in the uppermost display area.  Note also that almost all of the registers are zeroed out. 
    2. Note that the generated MIPS instructions are displayed in the area just below the display of registers.  Note also that there are more MIPS instructions than instructions typed in hello.asm.  In yourusernameA4 give some reasons why you think there are more MIPS instructions in PCSpim. 
    3. Reload hello.asm.  Then in yourusernameA4 list the names of the 4 the registers and their contents which contain something other than 0.  Use the green MIPS reference card at the front of your text and/or the Internet to determine the purpose of each of these non-zero registers, describing in yourusernameA4 what each is and why it makes sense that it would be non-zero immediately after loading the program.
    4. Next, watching the resisters, step through your program one MIPS instruction at a time using F10.  Only one register will change every single time you push F10. Describe which register this is and why this happens.  Other registers will only change value occasionally.  Looking at the relevant MIPS instructions in the second PCSpim display, describe each of these other register changes and specify why each occurs based upon the associated MIPS instruction.  Hint: You may need your text's green card to help with this.
    5. Does the data area (the third area down and labeled with DATA) ever change during the running of the program.  If it does, explain in yourusernameA4 how.  If it does not, explain in yourusernameA4 why it does not.

  8. Next we will try to better understand the way MIPS uses instructions and memory.  Copy and paste the following into a Notepad file named space.asm:
    # This is a spim program named space.asm
    # It introduces the .space directive for memory allocations

    .data # Tells SPIM that what follows will be data
    myString:
    .space 4 # This sets aside 4 bytes of memory labeled myString
    .text # Indicates what follows will be actual code
    main: # The required main label which symbolizes the start of the program
    # The next three lines set registers to appropriate values before we call syscall 
    # On calling "syscall," the "system call code" will determine what function syscall performs. 
    li $v0, 8 # The "system call code" is stored in the register $v0. (See page A-43)
    # The system call code for reading a string is 8, so we store 8 into register $v0 using "li".
    la $a0, myString
    # The $a0 register must be set to the memory location in which the computer will record the input.
    # This is accomplished by loading the address (using la) of myString into $a0.
    li $a1, 32 # This says "$a1 = length", which is set to the maximum number of characters that should be read in.
    # Note that this space set aside includes the terminating null '\0' character.
    syscall # Since $a0 and $a1 registers are set, syscall will work properly. 
    # Since $v0 is 8, it reads in a line from the SPIM console,
    # and writes the input to the memory location referenced by $a0, which was set to myString,
    # for a string of maximum length of $a1.
     jr $ra # This jump-returns to the return address in the $ra register.
    # Again, be sure to include a final return line in your file!
  9. Read the above program and the comment lines.

  10. Load space.asm and do the following:
    1. Run the program using "Go."  When the console opens type a few letters such as "C++" and "Jan" into it and push return, watching how the DATA area changes when you push return.  Try entering different text until you understand what is going on.
    2. Understanding that MIPS displays are all in hexadecimal, explicitly describe in yourusernameA4 what you see in the DATA area when you type "Jan" into the Console, being sure to describe anything that you find unintuitive or unexpected.  Hint: You may want to use a table such as is on the back of your green page in your text or online such as http://www.ascii.cl/.
    3. Next, explicitly describe in yourusernameA4 what you see in the DATA area when you type "Berea" into the Console, being sure to describe what happens, especially anything that you find unintuitive or unexpected.  
  11. The next program introduces the use of temporary registers.

  12. Copy and paste the following into a Notepad file named add.asm:
    ## Program which loads 42 and5 and obtains the sum...
    .text
    main:
    ori $8,$0,0x42 # put 42 into register $8
    ori $9,$0,0x5 # put 5 into register $9
    addu $10,$8,$9 # add register $8 and $9, put result in $10
    # Again, be sure to include a final return line in your file!
  13. Read the above program and the comment lines. Do not just use "Go" for this program.  Instead, step through the instructions using F10.

  14. First we will try to better understand temporary registers and instructions:
    1. Explain in yourusernameA4 how the first instruction you see above accomplishes the tasks of putting a 2 into one of the temporary registers.  Which temporary register does it go into?
    2. Explain in yourusernameA4 what happens with the next instruction you see above.
    3. Finally, explain in yourusernameA4 what happens with the third and last instruction you see above.
    4. There are only 3 instructions listed above, but there are more when the program is loaded into PCSpim.  Reload add.asm, and then step through the MIPS program line by line using F10, explaining in yourusernameA4 what each MIPS instruction is doing.  Hint:  Use your green sheet and your text for this.  In addition, you may want to reload all of the above programs to see the first lines of each of these programs.

  15. Finally, write your own PCSpim program to do anything you wish.  This program must be entirely your own work, may NOT use anything found on the Internet, and must be very well-commented. Better programs will build upon the ideas demonstrated in the above examples or will use a different idea or instruction from the text and will not be just simplistically changed from the programs given above.  Copy your new program into yourusernameA4, and then write a paragraph of documentation for how you decided what you wanted to try to program.  Describe in yourusernameA4 under what conditions your program works.

Enjoy your explorations--Submit yourusernameA4 to Blackboard.
Back to Computer Organization