/* CS 341 Spring 2002 */
/* Note segment 4 */
/* 06-Feb-2002 */
/* Taken by Apostolos Paul Pantazis */


     Assembler functions
     1. What goes on every location in memory (Segments).
     2. Assembler is there for us not to do the math.

     Segments:
     .word 123  /* a 123 Bit word */
     .word 17   /* a 17 Bit word */
     .word "ABC" /* A | B | C |0 */ --> Big Endian machine.

     /* To get the 2nd byte */
    
     LDUB r4, ABC + 1

     Symbol table: held Internaly By assembler.

     /* More Instruction Examples */

     LDUB [ABC] , r3  ! Immediate value.
     LDUB [ABC + 1], r4

     /* To allign by a multiple of 4 */
 
     .align 4


     /* Assembler in older days */


    |-----------------|   |--------|   |-----|
    |Assembly Program |-->|Assebler|-->|Image|
    |-----------------|   |--------|   |-----|


    |------|
    |foo.s | --> foo.o[symbol table, foo info]  : (1)
    |------|
    

    |------|
    |bar.s | --> bar.o[symbol table, bar info]  : (2)
    |------|


    --> Ld or loader is Run on (1) + (2)
    --> Linker Joins result to Form an Image.
    --> (symbol tables are joined by assembler
        (and symbols are built).
    --> a.out (binary)

    /* How does assembler knows were to start? */
   
    --> A symbol called start is defined (special symbol).

    Look up Data Vs Text Segments.

    .bss segment :

    Utilized when space is needed but it is not wanted to
    exist in the Image file (maybe all 0's or something).
    No values can be ut in the bss segment, we can only
    specify how big is. Extra not initialized space.

    Segments exist for the Linker and the OS.


    ! Program Example.
    ! Program Statement:
    ! Compare 2 strings to see if they are equal. 
    ! Specifics:
    ! a) Store adress of beginning of string and length.
    ! b) String A adress in r1, length in r2
    ! c) String B adress in r3, length in r4.
    ! d) Result in r5
    ! *  |  
    ! *  |--> if(str1 != str2) { 1 } else { 0 }

    ! Draft #1 pseudo C
    ! cmpstr(r1, r2, r3, r4) {
    ! if( r2 != r4) goto differ
    !  while( r2 > 0) {
    !   if( *r1 != *r3) { goto differ }
    !    r2 -= 1 , r1 += 1, r3 += 1
    !     r5 = 0
    !      return
    !      
    !      differ:
    !       r5 = 1
    !        return
    !    
    !      r2 - r4
    !      if (wasnt 0) { goto differ }
    !      W: r2
    !      if (was 0) { goto same }
    !      at = *r1
    !      bt = *r3
    !      at - bt
    !      if (wasnt 0) { goto differ }
    !      r2 -= 1 ;
    !      r1 += 1 ;
    !      r3 += 1 ;
    !      goto W
    !	   differ:
    !      r5 = 1 return ;
    !      same:
    !      r5 = 0 return ;

    ! SPARC
   
    .text
   
    cmpstr:
    	subcc r2, r4 , r0       ! Got to set the condition Codes
        brnz  differ		! Branch ! 0 to label differ
	nop			! Blah!
        
    W:
	addcc r2, r0, r0
	brz   same		! Branch if 0 to label same
        ldub  r1, r6
        ldub  r3, r7
        subcc r6, r7            ! Set Condition Codes prior branch
        brnz  differ		! branch not 0 to label differ
        nop			! I hate them, I hate them ..
        add r2, -1 , r2
        add r1, 1, r1
	add r3, 1 , r3
	br W			! Branch to label W
        nop			! Thats it no more hair on my head
      
    differ:
	add r0, 1, r5
        ta 0
    same:
	add r0, 0 , r5
        ta 0


    Questions? { about the notes ...}
    e-mail maximus@cs.unm.edu or ranger@eece.unm.edu