21cs43 Module 5
21cs43 Module 5
21CS43- Module-5
Computer
   p     Organization
           g z        and Assemblyy Languages
                                       g g
Yung-Yu Chuang
Memory syst em
• Memory is a linear array of                                     0x00000000    00
  byt es addressed from 0 t o                                     0x00000001    10
  232-1                                                                         20
                                                                  0x00000002
• Word,
  W d h    half-word,
             lf    d bbytt e                                      0x00000003    30
• Lit t le-endian                                                 0x00000004
                                                                                FF
                                                                                FF
                                                                  0x00000005
                                                                                FF
                                                                  0x00000006
                                                                                00
                                                                  0 FFFFFFFD
                                                                  0xFFFFFFFD
                                                                                00
                                                                  0xFFFFFFFE    00
                                                                  0xFFFFFFFF
Byt e ordering
• Big Endian
   – Least significant byt e has                                         0x00000000   00
     highest address                                                     0x00000001   10
   Word address 0x00000000                                                            20
                                                                         0 00000002
                                                                         0x00000002
   Value: 00102030                                                                    30
                                                                         0x00000003
• Lit t le Endian                                                                     FF
   – Least significant byt e has                                         0x00000004
                                                                                      FF
     lowest address                                                      0x00000005
   Word address 0x00000000                                                            FF
                                                                         0x00000006
   Value: 30201000
                                                                                      00
                                                                         0xFFFFFFFD
                                                                                      00
                                                                         0xFFFFFFFE   00
                                                                         0xFFFFFFFF
                    Downloaded by san abbigeri (sangeetaabbigeri1652010@gmail.com)
                                          lOMoARcPSD|27739474
                                                                0x00000000    00
                                                                0x00000001    10
 R0    R1       R2                     R3                                     20
                                                                0x00000002
 R4    R5       R6                     R7                       0x00000003    30
                                                                              FF
 R8    R9      R10                    R11                       0 00000004
                                                                0x00000004
                                                                              FF
 R12   R13     R14                     PC                       0x00000005
                                                                              FF
                                                                0x00000006
                                                                              00
                                                                0xFFFFFFFD
                                                                              00
                                                                0xFFFFFFFE
                                                                0             00
                                                                0xFFFFFFFF
             Downloaded by san abbigeri (sangeetaabbigeri1652010@gmail.com)
                                               lOMoARcPSD|27739474
Dat a processing
• They are move, arit hmet ic, logical, comparison
  and mult iply inst ruct ions.
                          ions
• Most dat a processing inst ruct ions can process
  one off t h
            heir
              i operands
                       d using
                             i thhe bbarrell shift
                                              hift er.
• General rules:
   – All operands are 32-bit , coming
     from regist ers or lit erals.
   – The result , if any, is 32-bit and
     placed in a regist er (wit h t he
     exceptt iion for
                  f llong
                        g multlt iiply
                                    l
     which produces a 64-bit result )
   – 3-address
     3 address format
Regist er movement
                                                                    immediat e,regist er,shift
• MOV     R0, R2                        @ R0 = R2
• MVN     R0,
           0 R22                        @ R0
                                           0 = ~R22
move negatt ed
             d
Addressing modes
• Regist er operands
  ADD   R0
        R0, R1
            R1, R2
• Immediat e operands
               a lit eral; most can be represent ed
                           by (0..255)x22n 0<n<12
  ADD   R3
        R3, R3
            R3, #1    @ R3:=R3+1
  AND   R8, R7, #0xff @ R8=R7[7:0]
  C                       regist er                                            0
 MOV  R0, R2, LSL #2 @ R0:=R2<<2
                      @ R2 unchanged
                                 g
 Example: 0…0 0011 0000
 Before R2=0x00000030
        R2 0x00000030
 After R0=0x000000C0
        R2=0x00000030
   0                      regist er                                            C
 MOV  R0, R2, LSR #2 @ R0:=R2>>2
                      @ R2 unchanged
                                 g
 Example: 0…0 0011 0000
 Before R2=0x00000030
        R2 0x00000030
 After R0=0x0000000C
        R2=0x00000030
           MSB            regist er                                            C
 MOV  R0, R2, ASR #2 @ R0:=R2>>2
                      @ R2 unchanged
                                 g
 Example: 1010 0…0 0011 0000
 Before R2=0xA0000030
        R2 0xA0000030
 After R0=0xE800000C
        R2=0xA0000030
Rot at e right
regist er
  C                       regist er                                            C
 MOV   R0, R2, RRX    @ R0:=R2 rotate
                      @ R2 unchanged
                                 g
 Example: 0…0 0011 0001
 Before R2=0x00000031,
        R2 0x00000031, C=1
                        C 1
 After R0=0x80000018, C=1
        R2=0x00000031
                                                                                         destination register
                                                                                         first operand register
                                                                                         set condition codes
                                                                                         arithmetic/logic function
25 11 8 7 0
                         1                                                                       # t
                                                                                                 #rot           8 bit iimmediate
                                                                                                                8-bit      di t
                                             immediate alignment
                                                                                            11              7    6   5 4   3        0
#shift Sh 0 Rm
Rs 0 Sh 1 Rm
Arit hmet ic
• Add and subt ract ion
Arit hmet ic
•   ADD    R0,   R1,         R2                                     @   R0        =   R1+R2
•   ADC    R0
           R0,   R1
                 R1,         R2                                     @   R0        =   R1+R2+C
                                                                                      R1 R2 C
•   SUB    R0,   R1,         R2                                     @   R0        =   R1-R2
•   SBC    R0,   R1,         R2                                     @   R0        =   R1-R2-!C
•   RSB    R0,   R1,         R2                                     @   R0        =   R2-R1
                                                                                      R2 R1
•   RSC    R0,   R1,         R2                                     @   R0        =   R2-R1-!C
-11                                 -128
                                     128 127                                                 0
            -5                                                                          3
Arit hmet ic
Arit hmet ic
Logical
Logical
•   AND   R0,    R1,           R2                                @     R0   =   R1   and   R2
•   ORR   R0
          R0,    R1
                 R1,           R2                                @     R0   =   R1   or    R2
•   EOR   R0,    R1,           R2                                @     R0   =   R1   xor   R2
•   BIC   R0,    R1,           R2                                @     R0   =   R1   and   (~R2)
R0=0x10011010
Logical
Comparison
• These inst ruct ions do not generat e a result , but
  set condit ion code bit s (N
                            (N, Z
                                Z, C
                                   C, V) in CPSR
                                            CPSR.
  Oft en, a branch operat ion follows t o change t he
  program flow.
            flow
Comparison
  compare
• CMP R1
       R1, R2                       @ set cc on R1-R2
  compare negat ed
• CMN R1
       R1, R2                       @ set cc on R1+R2
  bit t est
• TST R1   R1, R2                   @ set cc on R1 and R2
  t est equal
• TEQ R1  R1, R2                    @ set
                                        t cc on R1 xor R2
Comparison
• Feat ures:
   – Second
     S      d operand  d can’’ t b
                                 be iimmediat
                                         di t e
   – The result regist er must be different from
     t he first operand
       y
   – Cycles   depends
                 p       on core t yp
                                   ype
   – If S bit is set , C flag is meaningless
• See t he reference manual (4.1.33)
                            (4 1 33)
• M
  Mult
     lt iiply
           l wit
              it h a constt antt can oft
                                      ft en b
                                            be more
  efficient ly implement ed using shift ed regist er
  operand
 MOV R1, #35
 MUL R2    R2, R0
               R0, R1
       or
 ADD R0, R0, R0, LSL #2 @ R0’=5xR0
 RSB R2, R0, R0, LSL #3 @ R2 =7xR0’
Branches
sub2:         …
              …
              MOV              PC
                               PC, LR
          CMP              R0, R1
          BNE              skip
          CMP              R2, R3
          BNE              skip
          ADD              R4, R4, #1
skip:     …
          CMP   R0
                R0, R1
          CMPEQ R2, R3
          ADDEQ R4
                R4, R4
                    R4, #1
Addressing modes
• Memory is addressed by a regist er and an offset .
     LDR   R0
           R0, [R1] @ mem[R1]
                         [R1]
• Three ways t o specify offset s:
   – Immediat e
      LDR R0, [R1, #4] @                                           mem[R1+4]
   – Regist
     R i er
      LDR R0, [R1, R2]   @                                         mem[R1+R2]
   – Scaled regist er    @                                         mem[R1+4*R2]
                                                                      [R1+4*R2]
      LDR R0, [R1, R2, LSL                                         #2]
Addressing modes
• Pre-index addressing ( LDR R0, [R1, #4])
  wit
    i h
      hout a writ
                i eback
                    b k
• Aut o-indexing addressing ( LDR R0, [R1, #4]!)
  Pre-index wit h writ eback
  calculat ion before accessing wit h a writ eback
• Post -index addressing ( LDR R0, [R1], #4)
  calculat
     l l t ion
           i aftft er accessing
                            i witit h a writ
                                          it eback
                                              b k
Pre-index addressing
LDR   R0, [R1, #4]                         @ R0=mem[R1+4]
                                           @ R1 unchanged
                                                 nchanged
      R1           +
                                                                              R0
      R1           +
                                                                              R0
LDR R0,[R1],
R1 R0
Comparisons
• Pre-indexed addressing
LDR   R0
      R0, [R1,
          [R1 R2]                       @ R0=mem[R1+R2]
                                        @ R1 unchanged
• Aut o-indexing
      o indexing addressing
LDR   R0, [R1, R2]! @ R0=mem[R1+R2]
                    @ R1
                      R1=R1+R2
                         R1+R2
• Post -indexed addressing
LDR   R0, [R1], R2                      @ R0=mem[R1]
                                        @ R1=R1+R2
Example
Example
Example
• Assembler t ransfer p
                      pseudo inst ruct ion int o a
  sequence of appropriat e inst ruct ions
   sub    r0 pc
          r0,    pc, #12
Applicat ion
          ADR R1, table
                           table
loop
loop:     LDR R0
              R0, [R1]
                              R1
          ADD R1, R1, #4
          @ operations on R0
          …
          ADR R1,
                , table
loop:     LDR R0, [R1], #4
          @ operations on R0
          …
suffix        meaning
  IA      increase after
  IB      increase before
  DA      decrease after
  DB      decrease before
Addressing modes
                                                                                 addr   dat a
                                                                                0x010    10
                                         R0
R1:   10                                                                        0x014   20
R2:   20                                                                        0x018   30
R3:   30                                                                        0x01C   40
R0:   0x01C                                                                     0x020   50
                                                                                0x024   60
                                                                                 addr   dat a
                                                                                0x010    10
                                         R0
R1:   20                                                                        0x014   20
R2:   30                                                                        0x018   30
R3:   40                                                                        0x01C   40
R0:   0x01C                                                                     0x020   50
                                                                                0x024   60
                                                                                 addr   dat a
                                                                                0x010    10
R1:   40                                                                        0x014   20
R2:   50                                                                        0x018   30
R3:   60                                                                        0x01C   40
R0:   0x018                                                                     0x020   50
                                         R0                                     0x024   60
                                                                                 addr   dat a
                                                                                0x010    10
R1:   30                                                                        0x014   20
R2:   40                                                                        0x018   30
R3:   50                                                                        0x01C   40
R0:   0x018                                                                     0x020   50
                                         R0                                     0x024   60
Example
Example
Example
Applicat ion
• Copy a block of memory
  – R9
    R9: address
         dd     off t h
                      he source
  – R10: address of t he dest inat ion
  – R11:
    R11 endd address
              dd        off t h
                              he source
Applicat ion
• St ack (full: point ing t o t he last used; ascending:
  grow t owards increasing memory addresses)
          mode           POP                                         =LDM PUSH =STM
    Full ascending (FA) LDMFA                                       LDMDA STMFA STMIB
   Full descending (FD) LDMFD                                       LDMIA STMFD STMDB
  Empt y ascending (EA) LDMEA                                       LDMDB STMEA STMIA
 E ty d
 Empt descending
            di ((ED)) LDMED                                         LDMIB STMED STMDA
LDMFD R13!
      R13!, {R2
            {R2-R9}
                 R9} @ used for ATPCS
… @ modify R2-R9
STMFD R13!, {R2-R9}
Example
Example
Applicat ion
Process A OS Process B
Example
Immediat e numbers
    31          28 27 26 25 24         21 20 19             16 15                        12 11                                      0
                                                                                         destination register
                                                                                         first operand register
v=n ror 2r                                                                               set condition codes
                                                                                         arithmetic/logic function
25 11 r 8 7 n 0
                         1                                                                       # t
                                                                                                 #rot           8 bit iimmediate
                                                                                                                8-bit      di t
                                             immediate alignment
                                                                                            11              7    6   5 4   3        0
#shift Sh 0 Rm
dat a processing Rs 0 Sh 1 Rm
Impossible t o use
direct addressing
encoding for
dat a t ransfer
inst ruct ions
                  Downloaded by san abbigeri (sangeetaabbigeri1652010@gmail.com)
                                                lOMoARcPSD|27739474