Day 15
I. Last Time:
Hw #4 Due NOW
Hw #5 Posted
Lab #1 Due next week
A. Insts so far:
add, addu, sub, subu, addi, addiu
lw, sw, lb, sb
beq, bne, j, slt, sltu,

Pseudo-Ops make reading/writing code easier:
(Helps explain "why"/logic)
li, la, move
blt, bltu, bgt, bgtu, ble, bleu, bge, bgeu
bnez, beqz

   B. How does a program do IO/talk to hardware/get memory? OS!
      How does the computer contact the OS? 
      In Many os's through syscalls (There are other mechanisms 
      though like jump gates)

II. New Stuff:   
A. Branching and jumping formats:
I-Format: Label is "displacement" (16-bits)
I.e. how far (in bytes) to go forward or back.
J-Format:
26-bit field is the 28-bit (word alignment)
Target Address

       Instructions are ALWAYS word aligned - so we know the 
       last 2 bits will always be 0. (Mult by 4 in binary)
       Since we know this, we never store them - the 26 bits 
       we store are bits 27-2.
       (Bits 28-31 come from the current address - so the 
        jump is only pseudo-absolute)

       If we use the assembler, it just uses labels and 
       computes the correct number for us - saves us a lot
       of time and trouble. So we don't have to worry about 
       these details soo much.

       Any change to the program (insert or delete an inst) 
       will prob. require that these addresses be re-computed.
       
       Virtual Memory is usually utilized to make a program 
       "re-locatable"

    B. How the assembler uses memory
       1. Memory is divided into "segments" (See A-9)
       2. Each of these segments has a specific use:
          Reserved: ? OS/Hardware/whatever. We can't use it.
          Text Segment: For the program ("text")
              This is where the actual instructions are 
              stored. Remember that they are word aligned.
              If you see an address in this range - it's 
              refering to an instruction!
          Data Segment: Static and Dynamic:
              Static: For "static" (global) variables 
                 and "constant" arrays.
                 Ex: .asciiz "Hi ..." 
                 In a C prog. and initialized arrays 
                 initial values. NOT the array itself, 
                 just the stuff to initialize it with.
              Dynamic: the "Heap" - where the new/alloc 
                 memory comes from. (Free store)
                 Note: This can "Grow" Upward
          Stack: A "stack" used for functions.

       Assembler reads through program and packs into approp.
       segments

       As it finds labels they are added to a table and later
       the actual numeric address is filled in.

      As it finds labels they are added to a table and later
      the actual numeric address is filled in.

          Memory "MAP" / Usage Notes:
       0->0x400,000 Reserved:
                  Probably part of the OS instruction/Hardware
                  Memory Mapped Hardware
       0x400,000->0x10,000,000 Text Segment
                  This is where ALL instructions of your 
                  programs are placed.
                  (Program Must have less than 66 Million Insts?)
                  We can think of "constants" being stored here too.

      0x10,000,000-> Static Data Segment -"Load" Time allocated space
                      The things that exist for the entire
                      lifetime (global scope) of the program:
                      C/C++: Global Variables, 
                         Local Static Variables,
                         Initializers for arrays, classes
                         Char Constants (E.g.: "Hello")
                      ASM: 
                      Anything following a .data directive
                    Dynamic Data Segment - Run Time allocated space
                     C/C++: Space allocated while the program 
                      is running: new/malloc/alloc/realloc
                      ("Free Store"/"Heap")
                      This stuff is typically dynamically 
                      allocate because it's space isn't 
                      known in advance.
                    ASM: Anything allocated with the 
                         sbrk syscall 
               Note: This can "Grow" Upward
       0x7fffffff-> Stack Segment - Where all "local" variables
                   will be created/destroyed
                   Local Variables to functions (ALL Functions)
                   C/C++: ALL locally scopped (non-static) variables.
                     I.e. almost all variables declared in functions.

       Assembler reads through program and packs into approp.
       segments

       As it finds labels they are added to a table and later
       the actual numeric address is filled in.

C. Other useful assembler directives:
Declaring Space/Data (Uninitialized):
.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

   D. How does a program do IO/talk to hardware/get memory? OS!
      How does the computer contact the OS? 
      In Many os's through syscalls (There are other mechanisms 
      though like jump gates)

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)             


    E. Hello World SPIM Example:
       .text, .data directives
       labels and where they point to
       Execution and the PC

       A diagram of memory and where things are. 
       Both insts. and data.
       Note J inst. Look at address

III. Next Time:
A. More Mips Memory and Functions
B. MIPS memory segmentation
C. Using SPIM