Day 16
I. Last Time:
   1. Tests - Will finish grading by ~3:00
   2. Lab #1 posted by Thurs (?)
   3. Hw #5 posted tonight
   A. Instructions formats - How's it look in binary
      MIPS - RISC machine. Nice Feature - ALL instructions are
             EXACTLY 32-bits.
      R-format instructions - 
             Working between registers only - all data comes 
             from a register              
      I-Format Instructions - 
             Immeadiate data - Some Data is included in 
             the instructions. Data Manipulation and some 
             control instructions
       R-Format:
         6 bits  5 bits 5 bits 5 bits 5 bits 6 bits
         op code   rs     rt     rd    shamt func
         The op-code and function code together determine 
         the instruction. rs, rt and rd specify the registers 
         to use. Ignore shamt for the moment (assume 0)

       I-Format:
             6 bits  5 bits 5 bits      16 bits
             op code   rs     rt    Immediate data         
           
           NOTE: The immediate data is limited to 16 bits. 
                 So, the largest value is 64k unsigned, -32k-32K signed.
    B. Encoding Instructions:
       1. Lookup the Op-code (and the format)
          (If R-format, lookup function code as well)
       2. Figure out which value belongs in which field - 
          use the back cover of the book as an aid
       3. Convert all numbers to binary
       4. Convert all fields to appropriate widths
       5. Convert to 32-bit hex notation
   C. The other direction - decoding instructions.
      1. Find the op field and look it up to identify the format to use.
         (If it's a 0d that means an R-format inst - find the funct code)
      2. Seperate the remanining fields and decode them
      3. Convert registers to names using chart on A-23
      4. Put fields in the proper order 
         (use the back cover of the book as an aid)

   D. The J-Format - we'll talk about next time

II. New Stuff:        
    A. What's the meaning of 0x8E740020?
       It can be interpreted at least 5 ways: 
          ASCII text,
          Instructions, 
          a signed number, 
          an unsigned number, and 
          a floating point number.
       Note that this is JUST a "number" in binary - This is
       what Von Neumann had in mind for the stored program concept.

    B. Assembler Directives & Memory Segments
       Assembler Directives are used to give the assembler cues
       on how to interpret a source file. 
       The Assembler directives we'll be using most often are 
       used to "declare" data SPACE. 
       Sometimes that space is actually initialized with values.
       Often we'll need to use a "label" to refer to that space.

       The assembler breaks memory into "segments" as shown on page A-20
       (Picture): The Stack, The Dynamic Data Segments, the Static Data 
       Segment and the Text Segment As well as a reserved area.
       
       The TEXT segment is used for your program. (The acutal insts)
       The STATIC Data Segment is used for STATICALLY 
           declared global variables - In C, global variables and 
           textual constants, array initializers, etc.
       The DYNAMIC Data Segment is used for Dynamically 
           Allocated Data - new and malloc in C++/C
       The STACK segment is used to support functions calls
           and recursion - we'll worry about this later

       Common Assembler Directives:
           1. Specify where you want the following stuff put
              .text - Everything that follows is instructions
                      Put it in the text segment
              .data - Everything that Follows is Data, put it
                      in the STATIC data segment
           Declaring Space/Data:
              .space n - Set aside N bytes of space
           Initialized Space:
              .word w1,,,,wn - Put the following words in memory
              .asciiz "..." - Put the following text in memory and 
                              NULL terminate it
              .ascii "..." - Put the following text in memory and 
                             DON'T NULL terminate it.
              .byte b1,,,,bn - Put the following bytes in memory
              .float f1,,,,fn - Put the following floating point 
                                 numbers in memory
              .half h1,,,,hn- Store Half words
              
           Misc. 
            .align N - Force the following items to be aligned
                       to a N-bit boundary
            I.e. .align 2 forces word alignment  

       How do we access this space we've set aside? LABELS!!!
       A LABEL is an ADDRESS - Repeat after me A LABEL is an ADDRESS

       Label Format/Syntax:
         First Char: A-Z, a-z, _, .
         Remaining Characters: A-Z, a-z, 0-9, _, .
         MUST NOT be the same as an opcode!
         Each must be unique

       Common Uses: Adderesses of parts of programs
                    (I.e. where a "set" of insts in mem)
                    Addresses of DATA Area of a Program
       When working with labels we often use an instruction
       called la - Load address (actually this is a peusod op)

       .data
       prompt: .asciiz "Please enter you name"
       name:   .space 25
       greet:  .asciiz "Hello "
       eol:    .asciiz "\n"

    C. SPIM Syscalls 
       SPIM uses "system calls" as a very primitive operating system.
       Basically system calls allow you to do:
             input/output
             allocate
             Terminate the program (AND return to OS)
       System calls have a very rigid format - they use 
              specific registers for arguments and paramterts.

       See Page A-49.

       Example: Printing the prompt from the above program:
       This example requires the use of a new instruction called
       li - load immediate. It's used to put a specific value in a
       register. 
       I.e. li $t0,4 puts the number 4 in register t0.
       It also uses la - Load address (which we will discuss on Thurs)
       la actually is loading a 32-bit constant into a register - in 
       this case, the address of the prompt we want to print.

       la $a0,prompt
       li $v0,4
       syscall

III. Next Time:
     A. Pseudo-ops, Syscalls and a look at SPIM
     B. Continue w/ ASM Lang
     C. Lab #1 will be assigned on Thurs
     D. No Class on Wed - Independence Day