Blogger Templates

Sunday, 16 December 2012

Language of Computer


A.Introduction
                         
-is an artificial language designed to communicate instructions to a machine, particularly a computer.

-can be used to create programs that control the behavior of a machine and/or to express algorithms precisely.

- usually split into the two components of syntax (form) and semantics (meaning).

-some languages are defined by a specification document(for example, the C programming language is specified by an ISO Standard).


- MIPS also got it own machine language,such as 


ori $t0,$0,1            # store 1 inside $t0
ori $t1,$0,2            # store 2 inside $t1
add $t2,$t0,$t1      # store answer in $t2 of 1+2

MIPS arithmetic:

Instructions Set:

  v  Language of the Machine
  v  More primitive than higher level languages
o   e.g., no sophisticated control flow
  v  Very restrictive
o   e.g., MIPS Arithmetic Instructions
            ·         a representative of Reduced Instruction Set Computer (RISC)
            ·         similar to other architectures developed since the 1980's
            ·         used by NEC, Nintendo, Silicon Graphics, Sony
    
(Addition & Subtraction)
  v  All instruction have 3 operands.
  v  Operand order is fixed (destination first).
·            Example 1:
 C code:                          A = B + C
 MIPS code:                 add $s0, $s1, $s2 (associated with variables by compiler)
·          Example 2:
C code:          A = B + C + D;
     E = F - A;
 MIPS code:  add $t0, $s1, $s2
                             add $s0, $t0, $s3
                                       sub $s4, $s5, $s0

(Multiplication & Division)

  v  The actual MIPS instruction is
·         mult Rs,Rt,
                which means to multiply the contents of Rs by the contents of Rt.
  v  Since the result of multiplying two n-bit numbers can be up to 2n bits, MIPS uses two special registers, called lo and hi, to hold the results of multiplication.
  v  Special move instructions copy data from general registers (r0 ... r31) to/from lo and hi.

       Instruction                             
       Effect
       mfhi rs
       Rsàremainder
       mflo rs    
       Rsàquotient

           Example 1:

      a=b*c
mult       $s2,$s3                   #b*c
mfhi       $s0                         #upper half of product into $s0
mflo       $s1                         #lower half of product into $s1

Example 2:

a=c/d            a=c%d
Let c=$s2 ; d=$s3 ; a=$s0 ; b=$s1
div         $s2,$s3                 #lo=c/d , hi=c%d
mflo       $s0                         #get quotient
mfhi       $s1                         #get remainder

Written By Andy Low Fu Hwa

Type of Operand

Register Operand
-          Arithmetic instructions use register operands
-          Register operands refer to data stored in registers.
-          MIPS has a 32 × 32-bit register file
-          Use for frequently accessed data
-          Numbered 0 to 31
-          32-bit data called a “word”=4 byte
-          Compiler associates variables with registers

 i) MIPS contains 32 general registers numbered 0-31(register =$).

 ii) Registers $0 always contains value 0, and is the register numbered $0.

iii) Registers reserved for assembler and operating system are $at($1), $k0($26) and $k1($27) and should not be used by user programs or assembler.

iv) Registers $a0-$a3($4-$7) are use to pass the first four arguments to routines

v) Registers $v0 and $v1 ($2 and $3) are used to return values from function.

vi) Registers ($t0- $t9) which is ($8-$15,$24,$25) are caller saved registers that are used to hold temporary values. (Beginners are advised to use these registers in their program.)
- Registers $s0-$s7($16-$23) are called saved registers that holds values preserved across call functions such as a value entered by user.
- Register $gp ($28) is a global pointer pointing to the middle of a 64k block of memory in the static data segment. (gp = global pointer)
-Register $sp ($29) is the stack pointer, pointing to the last location of the stack.  (sp= stack pointer)
- Register $fp($30) is the frame pointer.
-Register $ra($31) contains return address written by instructions. 



Example of register operand
}  C code:
            a = (b + c) - (d + e);
            a is $s0 ,b,c,d,e is $s1,$s2,$s3,$s4.
}  Compiled MIPS code using register:
add $t0, $s1, $s2
add $t1, $s3, $s4
sub $s0, $t0, $t1

Register vs memory
-          Registers are faster to access than memory
-          Registers are memories located within the Central Processing Unit (CPU)
-          They are few in number (there are rarely more than 64 registers) and also small in size, typically a register is less than 64 bits in size.

Examples of such registers include:
-          Program Counter/Instruction Pointer Register (PC/IP)
-          Instruction Register (IR)
-          ALU Input & Output Registers
-          Condition Code (Status/Flags) Register
-          Stack Pointer Register (SP)
-           
-          Registers are read from RAM while memory read from disk unit such as Magnetic Hard Disks, Floppy Disks (V. Slow), Magneto-Optical CD Roms/Disks, DVDs.

-          Memory  are much slower than Register and Main memory, the access-time to data on disk is typically between 5 and 15 milliseconds (5 × 10-3 sec), although disks can typically transfer hundreds or thousands of bytes in one go.

Memory Organization
}  Viewed as a large single-dimension array with access by address
}  A memory address is an index into the memory array
}  Byte addressing means that the index points to a byte of memory, and that the unit of memory accessed by a load/store is a byte


}  Bytes are load/store units, but most data items use larger words
For MIPS, a word is 32 bits or 4 bytes.


Memory Operand
-                                -Main memory used for composite data
o   Arrays, structures, dynamic data

-                                -To apply arithmetic operations
o   Load values from memory into registers
o   Store result from register to memory
-                             
                                 - Memory is byte addressed
o   Each address identifies an 8-bit byte
-                            
-               -Words are aligned in memory
o   Address must be a multiple of 4

-                              -A direct memory operand specifies the data at a given address.
-                             The instruction acts on the contents of the address, not the address itself.
-                            
                             -  Except when size is implied by another operand, you must specify the size of a direct memory 
                operand so the instruction accesses the correct amount of memory.

           Example of memory operand

-                C code:
a = b + C[8];
o   a in $s1, b in $s2, base address of C in $s3

-                Compiled MIPS code:
o   Index 8 requires offset of 32
§  4 bytes per word

lw  $t0, 32($s3)              # load word
add $s1, $s2, $t0
 

                 Example 2

-          C code:
B[12] = a + B[8];
o   a in $s1, base address of B in $s3

-          Compiled MIPS code:
o   Index 8 requires offset of 32

lw   $t0, 32($s3)    # load word
add $t0, $s1, $t0
sw  $t0, 48($s3)    # store word
Immediate Operands
         -          Constant data specified in an instruction
 a = b + 4
- a,b in $s1,$s2
 addi $s1, $s2, 4
         -          No subtract immediate instruction
         -          Just use a negative constant
a = b - 1
addi $s1, $s2, -1

         -          Small constants are common
         -          Immediate operand avoids a load instruction
         -           If the source operand is immediate, the destination operand must be either a register or direct memory
                to provide a place to store the result of the operation.

  Constant Zero
        -          MIPS register 0 ($zero) is the constant 0 cannot be overwritten
        -          Useful for common operations
        -          E.g., move between registers
add $t2, $s1, $zero 

Written by Soo Pheng Kian



Instructions

        1.      A machine is capable of doing many chores related to manipulating  and displaying data given to     them.The machine does it by taking instructions from us.However,machine does not understand the language of human.Instead they use machine language to interpret what they supposed to do.Since machine language consists of binary digits only,it is hard to give commands to the machine.Therefore,the closest way to instruct a machine is by using assembly language.This language is then converted into machine language by assembler.

       2.      The assembly language is more sophisticated and restrictive in terms of their usage unlike high level language.

       3.      The most famous language is the MIPS instructions architecture which inspired most instructions architecture since 1980`s.

      4.      The instructions is shown below:






-           5.     In MIPS,the register usage is as follows:
    $zero :value 0 ($0)
    $v0, $v1: result values ($2 and 3)
    $a0 – $a3: arguments ($ 4 – 7)
    $t0 – $t7: temporaries ($ 8-15)
    $s0 – $s7: saved ($ 16-23)
    $t8 – $t9: temporaries ($24-25)
    $gp: global pointer for static data ($ 28)
    $sp: stack pointer ($ 29)
    $fp: frame pointer ($ 30)
    $ra: return address ($ 31)

        6.      In MIPS,there are three types of instructions format



Written by Mohd Safar

   ADDRESSING
         1.      Register Addressing
         2.      Immediate Addressing
         3.      PC-Relative Addressing
         4.      Base Addressing
   5 .Pseudo-direct Addressing

 1.Register Addressing

 -Register Addressing is used in the jr(jump register) instruction.Because an address in a MIPS CPU is also  32 bits.

The typical call is :
                              jr $rs 

  where $rs is replaced by any register.  

  -PC <- R[s]  means the PC (program counter) is updated with the contents of register s. Recall that a jump or branch is updated by modifying the contents of the program counter.

-Register addressing gives you the ability to generate any address in memory.An address exception occurs if the two low bits are not 00.

-Example :

add   $t0 ,   $t1 ,   $t2


                Where      $t0 = rd
                                $t1 = rs
                                $t2 = rt
                Thus         $t0 = $t1 + $t2


2. Immediate Addressing

-In Immediate Addressing, the operand is stored as part of the instruction. The immediate operand, which is stored along with the instruction, resides in the code segment -- not in the data segment. This addressing mode is also faster to execute an instruction because the operand is read with the instruction from memory.

-The instructions will be executed faster because it does not involve memory access but the size of operand is limited to 16 bits.


-Example :
addi   $t1 ,   $t1 ,   1

           
  Where   $t1 = rd
                $t1 = r1
                  1   = immediate value
  Thus       $t1 = $t1 + $1 

3. PC-Relative Addressing
- PC-Relative Addressing is used in the beq and bne (branch equal,branch not equal) instructions.

-Branch instructions are used primarily to implement loops or if-else statements. When you jump to a statement in an if-else or loop,you typically jump to a nearby instruction from the one you jumped from. The instruction you jumped from must have been the one at PC,thus it makes sense to jump relative to the PC.

-The effective address determines the branch target and also is the sum of the Program Counter and offset value in the instruction.

-Example :

beqz   $t0 ,   strEnd

Where   $t0 = rs
              100 = offset
Thus if ($t1 == 0) goto PC + 4 + (4*2)

4. Base Addressing

-Base Addressing is a data structure that is very important in computer programming is the record, called a structure in the C programming language. It is a collection of variables treated as a unit.

-Base Addressing is also known as indirect addressing ,where a register act as a pointer to an operand located at the memory location.

-The register is called base that may point to a structure or some other collection of data, and we need to load a value at a constant offset from the beginning of the structure. Because each MIPS instruction fits in one word, the size of the constant is limited to 16 bits.

-Example :

lw   $t1 ,   4 ( $t2 )
Where   $t1 = rs
              $t2 = base (memory address)
                 4 = offset value
Thus       $t1 = Memory [$t2 + 4]

5. Pseudo-direct Addressing

- Pseudo-direct Addressing is specifically used for J-type instruction,j and jal. The instruction format is 6 bits of opcode and 26 bits for the immediate value (target).

-The instruction address is the 26 bits constant contained within the instruction concatenated with the upper 4 bits of the Program Counter (PC).

-Example :

j     label


Bits 31 - 28
Bits 27 - 2
Bits 1 &0
PC :
Immediate :
Shift :
0111

01 0100 0101 0000 1011 1101 0110
00

00
Effective
Address
0111
01 0100 0101 0000 1011 1101 0110
00



Written by Ng Wui Sheng













No comments:

Post a Comment