Day 12
I. Last Time
   Hw Due on Monday!! (in mailbox by 5pm)
   Hw Due on Thurs!!!

   A. Computer Organization
      Block Diagram
      Instructions Classes: Data Movement, Control, and Data Manipulation
      Highlights:
          32, 32-bit GENERAL PURPOSE registers
          (General Purpose - Few registers have special 
              meaning to the hardware. They DO have special 
              meaning to the programmer though!)

   B. Basic Structure/Syntax of an assembler program: 
      Register Names/Numbers: Page A-23
   C. Commands: add, sub, addu, subu
   D. The R-Format
      Assembler->R-Format->Linked->Exe
      Control->Reads R-Format!!!

II. New Stuff

    Assembly language is like solving puzzles...
    (Bizarre, Twisted Logic Puzzles)
            
   A. Memory and Memory usage: An array of BYTES
       (I.e. the addresses are addresses of BYTES)
       BUT, we are working with words! 
   B. Retreiving from Memory: lw
      lw - load a WORD (32-bits for MIPS) out of memory
      Syntax/Format: lw dest, offset(index)
      Meaning: Read the Word that begins at Memory[offset+reg[index]]
               and put into the dest register.
               Offset is a number (specifically a 16-bit signed number)
      Example: lw $s0, 4($s1)
               Means get the word that's 4 bytes past the address(index)
               contained in $s1
      Ex: Using a Big Endian Machine!!!
          Mem[0] = 0x00
          Mem[1] = 0x00
          Mem[2] = 0x02
          Mem[3] = 0x01
          Mem[4] = 0x00
          Mem[5] = 0x00
          Mem[6] = 0x02
          Mem[7] = 0x04
          Mem[8] = 0x00
          Mem[9] = 0x00
          Mem[10] = 0x01
          Mem[11] = 0x01
      Q. If register s1 contains 0:
         What's the reqult of lw $s2,0($s1) -> s2=0x201
                              lw $s3,4($s1) -> s3=0x204
      Q. If register s1 contains 4:
         What's the result of lw $s2,0($s1) -> s2=0x204
                              lw $s3,4($s1) -> s2=0x101
      Q. If register s1 contains 4:
         What's the result of lw $s2,-4($s1) -> s2=0x201

      Q. What if this was a Little Endian Machine instead?

      NOTE: This just "copies" out of memory into the register
      (A data movement command)

   C. Putting data back into memory: sw
      sw - store a WORD (32-bits for MIPS) from reg into mem
      Syntax/Format: sw src, offset(index)
      Meaning: Store the Word from the register into 
               Memory[offset+reg[index]]
      Example: sw $s0, 4($s1)
               Means copy what's in register $so into the 
               word of memory begining at Mem[4+reg[s1]].
      Q. What happens when: sw $s3,0($s1)
                            sw $s3,-4($s1)


   D. Simple Math & Arrays:
      From Book: A[8]=h+A[8]. 
      h is in register s2, A's "Base Address" is in $s3
      A is an array of WORDS
      1. Retrieve A[8]:
         lw $s4,32($s3)   # s4 = A[8]. (8th word = 4*8 bytes = 32)
      2. Add:
         add $s4,$s4,$s2  # s4=h+A[8]
      3. Store: 
         sw $s4,32($s3)   # A[8]=h+A[8]

      g=h+A[i]
      A's "base" (Address) is in $s3
      g,h,i in $s1,$s2, and $s4
      1. Compute Address:
         add $s1,$s4,$s4 # s1=i+i=2*i
         add $s1,$s1,$s1 # s1=s1+s1=2*s1=2*2*i = 4i
         add $s1,$s1,$s3 # s1=s1+Base of A = A+4i
      2. Load A[i]
         lw $s1,0($s1)   # Load A[i] 
      3. Add h to A[i]:
         add $s1,$s2,$s1 # g=h+A[i]                     

   E. Alignment: MIPS designers designed the machine around
      "aligned" words - lw and sw can ONLY access words that
      are on an address/index that is a multiple of 4.
      10111*100 (multiplication in binary) = 1011100
      Basically this means that every (binary) address seen by 
      lw & sw MUST end in 2 zeros (I.e. it MUST be a multiple 
      of 4)

      Things that won't work: if $s0=0 and $s1=1:
        lw $s4,0($s1), lw $s4,1($s1), lw $s4,1($s0), etc.

      There are some good reasons for this choice.
      (And it typically doesn't really waste much space)
      We'll see one advantage of this later in the jump instruction

   F. I-Format Instructions - 
         Immeadiate data - Some Data is included in 
         the instructions. Data Manipulation and some 
         control instructions

      I-Format Instructions: Immediate data is encoded within the 
      instruction itself.

      Ex: Lw & Sw, The OFFSET is a part of the instruction!!!
          (I.e. immeadiately available)
      Format: 
          6 bits  5 bits 5 bits      16 bits
          op code   rs     rt    Immediate data         
      Ex:
         lw $s4,32($s3)   # s4 = A[8]. (8th word = 4*8 bytes = 32)
       lw op = 35d = 10 0011b
       rs = $s3 = $19 = 1 0011b
       rt = $s4 = $20 = 1 0100b
       offset = 32 = 10 0000b = 0000 0000 0010 0000b (16-bit)
       10 0011 | 1 0011 | 1 0100 | 0000 0000 0010 0000b
       1000 1110 0111 0100 0000 0000 0010 0000b
       0x8E740020
           
       NOTE: The immediate data is limited to 16 bits. 
             So, the largest value is 64k unsigned, -32k-32K signed.
       NOTE: We can also "reverse" the process. 

   G. Misc. Working with Bytes: lb & sb can be used to 
      load and store bytes in the same way that lw & sw 
      can be used to store words. 
      Differences: No Word Alignment Constraints
                   Loads byte into LOWER byte of reg, rest is 0
                   Stores byte from LOWER byte of reg


   H. Syscalls - How SPIM simulates an OS for IO and Mem alloc
      1. Exceptions, Interrupts, and dealing with the un-expected
         External Problem/Request - an INTERRUPT
             I.e. an external device has some data ready for the CPU
                  or user software needs something from the OS
         Internal - an EXCEPTION
             I.e. a bad inst., a bad address - an ERROR
         MIPS - Refers to EVERYTHING as an exception.
                Uses a special co-processor (co-pro 0) to deal with
         What happens during an interrupt - Control is transfered
         to a special set of instructions called an Interrupt handler.

         Tricky to write, because they must be bullet proof.

         We'll be using syscalls for IO, mem allocation, and 
         program termination.
      2. How syscalls work:
         See Page A-49
         1. Pick the Function to be performed via v0
         2. Set up any "arguments" needed via a0,a1,and f12
            (a0 and a1 - "Argument" registers. F12 will be used later
         3. Perform syscall - I.e. Tell the OS we're ready for it
         4. Collect/deal with any results. 
            (Ex: integers are returned in v0 - we may need to move
             this results elsewhere so we can get another int)
      3. A simple syscall:
        .data
        mesg_pmt:  .asciiz "Hello World!!!\n"
        .text
        main:
              li $v0,4
              la $a0,mesg_pmt
              syscall
              
              li $v0,10
              syscall

        Misc:
            main - A Label telling of the ADDRESS that the 
                   OS should start executing our program.
            mesg_pmt - An address of initialized data in the 
                   data segment. In this case our message.
            li - Load an immediate value. Pseudo-op for ori.
            la - Load an Address. Pseudo-of for ori and lui.
            Both of these are pseudo ops, and they both do the 
            same thing! - More on this later.
            syscall - Generates a software interrupt/exception. 
                 the OS looks at an exception code to deal 
                 with and performs function specified in v0
                 with args specified by a0,a1, and f12.
                 Then returns to caller.
       4. What WE use syscalls for:
          ALL IO - to screen only. 
              print/read int,string,float
              Terminate program
              allocate memory (sbrk)             

III. Next Time
     A. Continue w/ ASM