Assembly

Document Sample
Assembly Powered By Docstoc
					Machine & Assembly
    Language
       Machine Language

 Computer languages cannot be read directly
  by the computer – they are not in binary.
 All commands need to be translated into
  binary instructions called machine language.
 Each type of CPU has its own machine
  language.
 Von Neumann Architecture

Memory
                    CPU

             Control      Registers
              Unit


              ALU
Slightly More Complete Picture
Memory                   CPU

                 Control       Registers
                  Unit


                  ALU




             Secondary
              Storage
   Von Neumann Architecture

 Program and Data
  are both stored in
  memory.              Memory
                                       CPU
 Fetch, Decode,
  Execute cycle…                Control      Registers
                                 Unit


                                 ALU
       Machine Language

 We will look at a simulated CPU provided by
  the author of our book…
 This machine language has only 12 different
  instructions
 Each instruction is 16 bits long.
 Data values are also limited to 16 bits.
        Sample Instruction

          Instruction ID   Register #   Memory Location


LOAD contents of memory 100000010 RR MMMMM
location into register  example:
                        R0 = Mem[3]
                        100000010 00 00011
          Machine Language

LOAD contents of memory 100000010 RR MMMMM
location into register   ex: R0 = Mem[3]
                        100000010 00 00011
STORE contents of       100000100 RR MMMMM
register into memory     ex: Mem[4] = R0
location                100000100 00 00100
MOVE contents of one    100100010000 RR RR
register into another    ex: R0 = R1
register                100100010000 00 01
           Machine Language

ADD contents of 2 registers,   1010000100 RR RR RR
store result in third.          ex: R0 = R1 + R2
                               1010000100 00 01 10
SUBTRACT contents of 2         1010001000 RR RR RR
registers, store result into    ex: R0 = R1 – R2
third                          1010001000 00 01 10
Halt the program               1111111111111111
Sample Machine Language
        Program
   Add the contents of register 1 to the
    contents of register 2 and store it in
    register 0:

   1010000100 00 01 10
   1111111111111111
Sample Machine Language
        Program
   Add the contents of memory location
    1 to the contents of register 2 and
    store it in register 0:

   100000010 01 00001
   1010000100 00 01 10
   1111111111111111
       Assembly Language
 Set of mnemonic names for the instructions in a
  particular computer's machine language.
 Works on registers and memory locations in the
  computer.
 Translated into binary instructions.
       Assembly Language

Load contents of           LOAD [REG] [MEM]
memory location into       ex: R0 = Mem[3]
register                   LOAD R0 3
Store contents of          STORE [REG] [MEM]
register into memory       ex: Mem[4] = R0
location                   STORE 4 R0
Move contents of           MOVE [REG1] [REG2]
second register into first ie: R1 = R2
register                   MOVE R1 R2
       Assembly Language

Add contents of 2          ADD [REG] [REG] [REG]
registers, store result in ex: R0 = R1 + R2
third.                     ADD R0 R1 R2
Subtract contents of 2     SUB [REG] [REG] [REG]
registers, store result    ex: R0 = R1 – R2
into third                 SUB R0 R1 R2
Halt the program           HALT
Sample Assembly Language

    Add the contents of register 1 to the
     contents of register 2 and store it in
     register 0:
    ADD R0 R1 R2
    HALT
Sample Machine Language

   Add the contents of memory location
    1 to the contents of register 2 and
    store it in register 0:
   LOAD R1 1
   ADD R0 R1 R2
   HALT
Sample Program
   LOAD R2 5
   ADD R2 R2 R2
   LOAD R1 3
   ADD R3 R1 R2
   HALT

   What does this program do?
                  Exercise
 What would be the         STORE [MEM] [REG]
  assembly                  LOAD [REG] [MEM]
  instructions to swap      MOVE [REG] [REG]
                            ADD [REG] [REG] [REG]
  the contents of           SUB [REG] [REG] [REG]
  registers 1 & 2?          HALT
 Exercise Solution

STORE 1 R1
MOVE R1 R2
LOAD R2 1
HALT
                  Exercise
 What would be the      STORE [MEM] [REG]
  assembly instructions
                         LOAD [REG] [MEM]
  to do the following    MOVE [REG] [REG]
  computation:           ADD [REG] [REG] [REG]
                         SUB [REG] [REG] [REG]
R0 = Mem[7] + R2 - R3    HALT
  Exercise Solution

SUB R2 R2 R3
LOAD R1 7
ADD R0 R1 R2
HALT



(we want R0 = Mem[7] + R2 - R3)
 Some More Instructions…

 We are missing some crucial functionality…
 ??
 Some More Instructions…
 We are missing some
  crucial functionality…
 Loops!

Branch to a location in    BRANCH [MEM]
memory
Branch if the ALU result BZERO [MEM]
is zero.
Branch if the ALU result BNEG [MEM]
is negative.
A More Complex Example

R0   3        0   ADD R3 R2 R3

R1   1
              1   SUB R0 R0 R1

R2   Number
              2   BZERO 4
R3   0
              3   BRANCH 0

              4   MOVE R2 R3

              5   HALT
                      In Python
 The same program in Python would be the following:
       z=0
       x=3
       while x != 0:
                z += y
                x = x -1
       y=z

 Or the following:
       y = y*3
       Compilers, Interpreters,
           Assemblers
 Because it is not fun to program in
  Assembly, we have “high level”
  programming languages.
  • Python
  • Alice
  •                         , Ada, lisp, Ruby, Smalltalk, C#, …
      C, C++, Java, Fortran, Cobol, Pascal, C++, M

  • Compiler/Interpreter translates from the high-
    level language to machine language.
                 Program Translation


                              ADD R3 R2 R3               1010000100000110
z=0
x=3                           SUB R0 R0 R1               1010001000000110
while x != 0:                 BZERO 4                    0000001000000100
            z += y            BRANCH 0                   0000000100000000
            x = x -1                                     1001000100001011
                              MOVE R2 R3
y=z                                                      1111111111111111
                              HALT




                   Compiler                  Assembler

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:12/18/2011
language:English
pages:27