Docstoc

04-Assembly Programming

Document Sample
04-Assembly Programming Powered By Docstoc
					Chap 4 - Assembly Programming                                                                      CPE202/CSC201




                                       Introduction to Assembly Language
                   © W.B.Goh (2005)
                                                  Programming
                           1. Introduction
                                      - Why and when to use assembly programming
                                      - Characteristics and format of assembly programs
                           2. Assembly Directive
                                      - Representing constant values
                                      - H8S assembler directives
                                      - Worked examples – data allocation in memory
                           3. Executable Instructions
                                      - H8S instruction format
                                      - Interpreting the H8S instruction set reference chart
                           4. Assembly Program Development
                                      - Stages in assembly program development
                                      - Assembly program development tools
                                                                                               1




                   © W.B.Goh (2005)
                                          Why Use Assembly Language?
                                      Very efficient codes can be created:
                                      More compact program size.
                                      e.g. – embedded devices may not have much built-in
                                      memory but require more and more functionalities.
                                      Codes with faster execution speed.
                                      e.g. – algorithms for processing signals in mobile phone
                                      can be computationally demanding and need to operate in
                                      real-time.
                                      Reduce unnecessary programming overheads.
                                      e.g. – high-level language may perform data type checking,
                                      it improves code robustness at the expense of efficiency.
                                                                                               2




(c) A/P Goh Wooi Boon - 2008                                                                                   1
Chap 4 - Assembly Programming                                                                       CPE202/CSC201




                                       When to Use Assembly Language?
                   © W.B.Goh (2005)



                                      Critical parts of the operating system’s software.
                                      e.g. – kernel of system that are constantly being executed.

                                      Input/Output intensive codes.
                                      e.g. – device drivers and “loopy” segments of code that
                                      processes streaming data (e.g. audio signal enhancement).

                                      Time-critical codes.
                                      e.g. – code that detect incoming sensor signals and has to
                                      response quickly (e.g. car braking system).




                                                                                              3




                                        Characteristics of Assembly Programs
                   © W.B.Goh (2005)




                                      An assembly language programs is made up of
                                      two types of statements.
                                      Executable instructions.
                                      – one of the valid instruction of the processor.
                                      – they are executed when the program runs.

                                      Assembler directives.
                                      – they tell assembler about the desired characteristics of
                                        the program.
                                      – they are processed during program assembly and
                                        influences the way the program is loaded into memory.
                                                                                              4




(c) A/P Goh Wooi Boon - 2008                                                                                    2
Chap 4 - Assembly Programming                                                                               CPE202/CSC201




                   © W.B.Goh (2005)
                                        H8S Assembly Program Example
                                      Executable instructions                Assembler Directive
                         DSeg           .equ       H’FF6000       ;equate DSeg to hex FF6000
                         Fri            .equ       5              ;equate Fri to 5

                                        .section myData,DATA,LOCATE=DSeg
                         Day            .data.b Fri        ;initialize byte-sized memory to Fri
                         Mth            .res.b 12          ;allocated 12 bytes of memory
                                        .align  2          ;align to next word boundary
                         Week           .data.w 7          ;initialize word-sized memory to 7

                                        .section myCode,CODE,LOCATE=H’001000
                         Main:          mov.b   R0L, R1L ;move byte in register R0L to R1L
                                        mov.w   @Week,E1 ;move word at memory variable Week to E1
                                        mov.l   ER2,@Mth ;move longword in ER2 to memory var. Mth
                                        .end
                                                                                                        5




                   © W.B.Goh (2005)
                                          Assembly Language Format
                                      The typical assembly language program format
                                      divides into four columns.

                         Label          Opcode      Operand            Comments


                                        .section    myData,DATA,LOCATE=DSeg
                         Day            .data.b     Fri          ;initialize byte-sized memory to Fri
                         Mth            .res.b      12           ;allocated 12 bytes of memory
                                        .align      2            ;align to next word boundary
                         Week           .data.w     7            ;initialize word-sized memory to 7

                                        .section    myCode,CODE,LOCATE=H’001000
                         Main:          mov.b       R0L, R1L     ;move byte in register R0L to R1L
                                        mov.w       @Week,E1     ;move word at memory variable Week to E1
                                        mov.l       ER2,@Mth     ;move longword in ER2 to mem var. Mth
                                        .end                                                            6




(c) A/P Goh Wooi Boon - 2008                                                                                            3
Chap 4 - Assembly Programming                                                                                 CPE202/CSC201




                   © W.B.Goh (2005)
                                                                   Label
                         Label Opcode                Operand           Comments

                                      Identifier or symbol that begins with an alphabet.
                                      It is used as a reference to the address of an instruction or
                                      data (address label).
                                      However, when used with the .equ directive, it takes on
                                      the value of the equated constant (value label).
                                                                   Example
                         Fri             .equ        5            ;Value label “Fri” has the value of 5
                                            :
                         Week            .data.w     7        ;Address labels “Week” and “Main” are
                                            :                 ;assigned the addresses of the memory
                         Main:           mov.b       R0L,R1L  ;locations where the word-data 7 and the
                                         mov.w       @Week,E1 ;mov.b instruction is stored respectively.
                                            :                                                             7




                   © W.B.Goh (2005)
                                                                Opcode
                         Label           Opcode Operand                Comments

                                      Contains mnemonic of executable instructions or
                                      assembler directive.
                                      Many opcode mnemonics require a suffix to
                                      indicate size of the data being handled.
                                      e.g. – the 3 valid size suffixes of the move mnemonic are
                                             mov.b, mov.w and mov.l.

                                      Some assembler directive examples:
                                         .section myData,DATA,LOCATE=DSeg
                         Day             .data.b Fri           ;initialize byte-sized memory to Fri
                         Mth             .res.b   12           ;allocated 12 bytes of memory
                                                                                                          8




(c) A/P Goh Wooi Boon - 2008                                                                                              4
Chap 4 - Assembly Programming                                                                                  CPE202/CSC201




                   © W.B.Goh (2005)
                                                                Operand
                         Label          Opcode        Operand         Comments


                                      Number of operands in this column depends on
                                      the instruction (may be 0, 1 or 2) .
                                      Two-operand example.
                                       mov.b     R0L,R1L         ;move byte in register R0L to R1L.

                                      One-operand example.
                                       neg.w     R0              ;negate word in register R0.

                                      No operand example.
                                       rts                       ;return from subroutine.
                                                                                                        9




                   © W.B.Goh (2005)
                                                            Comments
                         Label          Opcode        Operand         Comments

                                      Comments are ignored by the assembler but is
                                      essential for good program documentation.
                                      Use a “;” character before starting comments.


                         Main:          mov.b         R0L, R1L      ;move byte in register R0L to R1L
                                        mov.w         @Week,E1      ;move word at memory variable Week to E1
                                        mov.l         ER2,@Mth      ;move longword in ER2 to memory var. Mth




                                                                                                       10




(c) A/P Goh Wooi Boon - 2008                                                                                               5
Chap 4 - Assembly Programming                                                                   CPE202/CSC201




                   © W.B.Goh (2005)
                                             Representing Constants
                                      The H8S assembler uses certain notations to
                                      represent different types of value (constants).

                                      Decimal digits are written as they are.
                                      e.g. 10 or -123
                                      Hexadecimal digits are preceded with H’.
                                      e.g. H’2F or H’FF6000
                                      Binary digits are preceded with B’.
                                      e.g. B’00101111
                                      ASCII characters are enclosed in double quotes.
                                      e.g. “A” or “0”
                                                                                           11




                                                         Assembler Directives
                   © W.B.Goh (2005)
                                                               EQU
                                      The .equ directive means “equate”.

                                      Links a symbolic name (label) to a constant value.
                                      A label cannot be redefined elsewhere in program
                                      but can be referenced any number of times.
                                      The equated label is replaced by its equivalent
                                      constant value during program assembly. E.g.:

                            Fri          .equ      5
                                            :
                            Day          .data.b   Fri               Day     .data.b   5

                                                                   becomes
                                                                                           12




(c) A/P Goh Wooi Boon - 2008                                                                                6
Chap 4 - Assembly Programming                                                                                CPE202/CSC201




                                                      Assembler Directives
                   © W.B.Goh (2005)
                                                            SECTION
                                      The .section directive determines the start
                                      address in memory where the subsequent data or
                                      code is to be stored.
                                      Each section is given a unique section name.
                                      An additional DATA or CODE qualifier is used to
                                      distinguish between a data or code segment.
                                      The start address of the section is defined after
                                      the LOCATE directive. For example:
                                        .section myCode,CODE,LOCATE=H’001000
                         Main:          mov.b        R0L, R1L      ;move byte in register R0L to R1L
                                        mov.w        @Week,E1      ;move word at mem var Week to E1
                                                                                                       13




                                                      Assembler Directives
                   © W.B.Goh (2005)
                                            SECTION – worked example
                                      Show the memory map associated with the given
                                      .section directives:
                       DSeg            .equ      H’FF6000
                       CSeg            .equ      H’002000
                             .section myCode,CODE,LOCATE=CSeg
                       Main: mov.b    R0L, R1L                                      Address
                                        :                                         H’000000

                                       .section myData,DATA,LOCATE=DSeg           H’002000
                       Day             .data.b 5                                                 Code
                                          :

                                                                                  H’FF6000
                                        Memory map of code and data areas                         Data
                                      based on the given .section directives
                                                                                  H’FFFFFF             14
                                                                                                Memory map




(c) A/P Goh Wooi Boon - 2008                                                                                             7
Chap 4 - Assembly Programming                                                                        CPE202/CSC201




                                                      Assembler Directives
                   © W.B.Goh (2005)
                                                             DATA
                                      The .data directive defines a data constant in
                                      memory.
                                      It must be qualified with a size suffix (.b, .w or .l).
                                      The number of bytes allocated to the constant in
                                      memory is dependent on the suffix used (e.g. .b
                                      allocates 1 byte, .w allocates 2 bytes).
                                      The constant is loaded into the associated
                                      memory addresses when the program is
                                      downloaded into the processor’s memory.


                                                                                                15




                                                      Assembler Directives
                   © W.B.Goh (2005)
                                             DATA – worked example
                                      Show the memory map associated with the given
                                      sequence of .data directives:

                                         .section   myData,DATA,LOCATE=H’FF6000
                           Data1         .data.w    H’1234
                                         .data.b    0,10
                           Data3         .data.l    4          Data1 H’FF6000   H’12
                                                                     H’FF6001   H’34
                                                                        H’FF6002    H’00
                                                                        H’FF6003    H’0A
                                                               Data3 H’FF6004       H’00
                                                                        H’FF6005    H’00
                                                                        H’FF6006    H’00
                                           Memory map after constants   H’FF6007    H’04
                                               are downloaded                                   16
                                                                                   Byte-sized




(c) A/P Goh Wooi Boon - 2008                                                                                     8
Chap 4 - Assembly Programming                                                                                                CPE202/CSC201




                                                             Assembler Directives
                   © W.B.Goh (2005)
                                                                          RES
                                      The .res directive allocates storage space for
                                      memory variables.
                                      It must be qualified with a size suffix (.b, .w or .l).
                                      Unlike the .data directive, no values are loaded
                                      into these memory locations.
                                      The operand defines the number of the specified
                                      data size to be allocated. Large numbers of
                                      elements can be easily allocated for arrays.
                                        Array      .res.b        80         ;array of 80 bytes in memory


                                                                                                                   17




                                                             Assembler Directives
                   © W.B.Goh (2005)
                                                                       ALIGN
                                      The .align directive forces the next data
                                      allocation to a specified memory boundary.
                                      The operand determines the specified memory
                                      boundary for alignment and can take the value of
                                      1, 2, 4 or 8.
                                      This directive is used to force word and longword
                                      data to be stored starting at even addresses.
                                      Byte1       .data.b          1         If address of Byte1 is H’001000,
                                                  .align           2         the starting address for Word2
                                      Word2       .data.w          5         using .align 2 it will be H’001002,
                                                                             using .align 4 it will be H’001004,

                                      Note: If address of Byte1 is H’001005, the starting address for Word2 using .align 2
                                      it will be H’001006, using .align 4 it will be H’001008,                       18




(c) A/P Goh Wooi Boon - 2008                                                                                                             9
Chap 4 - Assembly Programming                                                                          CPE202/CSC201




                                                      Assembler Directives
                   © W.B.Goh (2005)
                                        DATA and RES – worked example
                                      Show the memory map associated with the given
                                      sequence of .data and .res directives:
                           Dseg          .equ       H’FF6000
                                                              No memory space allocated for .equ
                           Fri           .equ       5
                                         .section   myData,DATA,LOCATE=DSeg
                           Day           .data.b    Fri                        Mth = H’FF6001
                           Mth           .res.b     12
                                         .align     2        Day H’FF6000 H’05      1

                           Week          .data.w    7            H’FF6002    2      3

                                                                   H’FF6004    4       5

                                                                   H’FF6006    6       7
                                          Memory map after
                                          constants are            H’FF6008    8       9

                                          downloaded               H’FF600A    10      11   .align 2
                                                                   H’FF600C    12

                                                             Week H’FF600E     H’0007          19
                                                                               Word-sized




                                                      Assembler Directives
                   © W.B.Goh (2005)
                                                                END
                                      The .end directive stops further assembly of
                                      directives and executable instructions.
                                      Any statements after this directive will be ignored
                                      by the assembler.
                                          .section myCode,CODE,LOCATE=CSeg
                              Main:       mov.b    R0L, R1L
                                          .end
                                          mov.w      @Week,E1
                                          mov.l      ER2,@Mth                       Not assembled


                                      Note: the .end directive does not stop program execution,
                                      it only stops the assembly process.
                                                                                               20




(c) A/P Goh Wooi Boon - 2008                                                                                      10
Chap 4 - Assembly Programming                                                                        CPE202/CSC201




                                                      Executable Instructions
                   © W.B.Goh (2005)               H8S Instruction Format
                                      The H8S executable instructions (mnemonics)
                                      assembles into binary patterns (machine code)
                                      that the H8S CPU understands.
                                      Different mnemonics translates into machine
                                      codes of different sizes (1,2,3,4 or 5 words).
                                      Since machine codes are stored in the CPU’s
                                      memory, selecting shorter instructions that
                                      perform the same functional requirements can
                                      reduce the overall size of the program.


                                                                                                21




                                                      Executable Instructions
                   © W.B.Goh (2005)           H8S Instruction Examples
                                      Some instructions with their mnemonic, machine
                                      code and instructions length:
                                       Address       Machine Code                Mnemonics
                                      in memory    (shown in hex form)




                                                   Assembles into a 2-byte machine code




                                                                         Assembles into an 8-byte
                                         Words:   1     2     3    4     machine code (or 4 words)
                                                                                                22




(c) A/P Goh Wooi Boon - 2008                                                                                    11
Chap 4 - Assembly Programming                                                                                               CPE202/CSC201




                                                            Executable Instructions
                   © W.B.Goh (2005)        H8S Instruction Set Reference Chart
                                      Provides useful information on each instruction:
                                      Mnemonics (e.g. mov.b Rs,Rd)
                                      Data size operated upon (.b, .w, .l)
                                      Instruction length (no. of bytes, always even)
                                      Execution time (in number of states)
                                      Influence on CCR (I,H,N,Z,V,C)
                                      Instruction operation




                                                                                                                     23
                                                                        Sample of the H8S Instruction Set Reference Chart




                                             H8S Reference Chart - Worked example
                   © W.B.Goh (2005)                        Instruction Operation
                                      Describe the operation of the instruction given by
                                      the mnemonic mov.b R0L,R1L.




                                      Answer:
                                      It moves the byte in
                                      register R0L (Rs) into
                                      register R1L (Rd)
                                      Note: Rs = source register
                                            Rd = destination register                                                24




(c) A/P Goh Wooi Boon - 2008                                                                                                           12
Chap 4 - Assembly Programming                                                                CPE202/CSC201




                                           H8S Reference Chart - Worked example
                   © W.B.Goh (2005)     Instruction Length and Execution Time
                                      Identify the length (in bytes) and execution time of
                                      the instruction mov.b R0L,R1L.




                                      Answer:
                             •        Instruction length = 2 bytes.
                             •        Exectution time = 1 clock cycle.
                                                                                      25




                                           H8S Reference Chart - Worked example
                   © W.B.Goh (2005)                 Interpreting CCR flags
                                      How does mov.b R0L,R1L alter the CCR flags?




                                      Answer:
                                                            −   Unchanged
                            •         Always clear V.
                                                            0   Always cleared
                            •         Leaves I,H and C      ↕   Result dependent
                                      unchanged.
                            •         Sets or clears N and Z depending
                                      on byte-sized data moved.                       26




(c) A/P Goh Wooi Boon - 2008                                                                            13
Chap 4 - Assembly Programming                                                                                               CPE202/CSC201




                                          Assembly Program Development
                   © W.B.Goh (2005)

                                              Stages in developing an assembly program
                           Prog.src                      Prog.obj                Prog.abs
                                                                                                          H’FF6000
                                           Assembly                 Linking     Address Info   Download   H’FF6002
                                                                                                                     Code
                                                                                                          H’FF6004
                         mov.b                          01010                    01010
                                                                                 Code                     H’FF6006
                         mov.w                          11011                    11011                    H’FF6008
                                          Assembler                 Linker        Data         Loader     H’FF600A
                         :                              :                                                 H’FF600C   Data
                                                                                                          H’FF600E


                      Source file                     Object file             Load module                    Memory of
                                            Host PC (for software development)                              Target MCU
                                                                                                            Target MCU

                                              Assembly Program Development Tools
                                      Text editor – edit the text-based mnemonics in source file (*.src).
                                      Assembler – converts mnemonics in source file into machine code
                                      and produces an object file (*.obj).
                                      Linker – combines several object files (e.g. from libraries) into a load
                                      module that contains machine code and address information (*.abs).
                                      Loader – uses load module’s address info. to download instructions
                                      and data constants into appropriate memory areas for execution. 27




                    ©2003, Goh Wooi
                    Boon


                                       Summary – Assembly Programming
                                      Assembly programs are represented by assembler
                                      directives and executable instructions.

                                      Addresses and constant values can be represented in
                                      symbolic form (address and value labels).

                                      Unlike high-level languages, assembly-level statements
                                      Are known as mnemonics and are converted by an
                                      assembler into binary patterns understood by the CPU
                                      (machine code).
                                      address architecture of processor directly (hardware
                                      dependent).
                                      have one-to-one correspondence with machine-level
                                      instructions (machine code).
                                                                                                                       28




(c) A/P Goh Wooi Boon - 2008                                                                                                           14

				
DOCUMENT INFO