chapter 2 hcs12 assembly programming

Download Chapter 2 HCS12 Assembly Programming

Post on 15-Jan-2016

161 views

Category:

Documents

2 download

Embed Size (px)

DESCRIPTION

Chapter 2 HCS12 Assembly Programming. Three Sections of a HCS12/MC9S12 Assembly Program. Assembler directives Defines data and symbol Reserves and initializes memory locations Sets assembler and linking condition Specifies output format Specifies the end of a program - PowerPoint PPT Presentation

TRANSCRIPT

  • Chapter 2

    HCS12 Assembly Programming

  • Three Sections of a HCS12/MC9S12 Assembly ProgramAssembler directivesDefines data and symbolReserves and initializes memory locationsSets assembler and linking conditionSpecifies output formatSpecifies the end of a programAssembly language instructionsHCS12/MC9S12 instructionsCommentsExplains the function of a single or a group of instructions

  • Fields of a HCS12 InstructionLabel fieldOptionalStarts with a letter and followed by letters, digits, or special symbols (_ or .)Can start from any column if ended with :Must start from column 1 if not ended with :Operation fieldContains the mnemonic of a machine instruction or an assembler directiveSeparated from the label by at least one spaceOperand fieldFollows the operation field and is separated from the operation field by at least one spaceContains operands for instructions or arguments for assembler directivesComment fieldAny line starts with an * or ; is a commentSeparated from the operand and operation field for at least one spaceOptional

  • Identify the Four Fields of an InstructionExample

    loopADDA #$40; add 40 to accumulator A

    (1) loop is a label(2) ADDA is an instruction mnemonic(3) #$40 is the operand(4) add #$40 to accumulator A is a comment

    movb0,X,0,Y; memory to memory copy

    (1) no label field(b) movb is an instruction mnemonic(c) 0,X,0,Y is the operand field(d) ; memory to memory copy is a comment

  • Assembler DirectivesENDEnds a program to be processed by an assemblerAny statement following the END directive is ignored.ORGThe assembler uses a location counter to keep track of the memory location where the next machine code byte should be placed.This directive sets a new value for the location counter of the assembler.The sequenceORG $1000LDAB #$FFplaces the opcode byte for the instruction LDAB #$FF at location $1000.

  • dc.b (define constant byte)db (define byte)fcb (form constant byte)-These three directives define the value of a byte or bytes that will be placed at a given location. -These directives are often preceded by the org directive.-For example,org $800arraydc.b $11,$22,$33,$44

    dc.w (define constant word)dw (define word)fdb (form double bytes)-Define the value of a word or words that will be placed at a given location.-The value can be specified by an expression.-For example,vec_tabdc.w$1234, abc-20

  • fcc (form constant character)Used to define a string of characters (a message)The first character (and the last character) is used as the delimiter. The last character must be the same as the first character.The delimiter must not appear in the string.The space character cannot be used as the delimiter.Each character is represented by its ASCII code.Examplemsgfcc Please enter 1, 2 or 3:

  • fill (fill memory)-This directive allows the user to fill a certain number of memory locations with a given value.-The syntax is fill value,count-Examplespace_line fill $20,40ds (define storage)rmb (reserve memory byte)ds.b (define storage bytes)-Each of these directives reserves a number of bytes given as the arguments to the directive.-Examplebufferds 100reserves 100 bytes

  • ds.w (define storage word)rmw (reserve memory word)-Each of these directives increments the location counter by the value indicated in the number-of-words argument multiplied by two.-Exampledbufds.w 20reserves 40 bytes starting from the current location counterequ (equate)-This directive assigns a value to a label.-Using this directive makes ones program more readable.-Examplesarr_cnt equ 100oc_cnt equ 50

  • loc This directive increments and produces an internal counter used in conjunction with the backward tick mark (`). By using the loc directive and the ` mark, one can write program segments like the following example, without thinking up new labels:loclocldaa #2ldaa #2loop`decasame asloop001decabne loop`bne loop001loclocloop`brclr 0,x,$55,loop`loop002brclr 0,x,$55,loop002

  • Macro A name assigned to a group of instructions Use macro and endm to define a macro. Example of macro

    sumOf3macroarg1,arg2,arg3ldaaarg1addaarg2addaarg3endm

    Invoke a defined macro: write down the name and the arguments of the macrosumOf3$1000,$1001,$1002is replaced by ldaa$1000adda$1001adda$1002

  • Software Development ProcessProblem definition: Identify what should be done.Develop the algorithm. Algorithm is the overall plan for solving the problem at hand.An algorithm is often expressed in the following format:Step 1Step 2Another way to express overall plan is to use flowchart.Programming. Convert the algorithm or flowchart into programs.Program testingProgram maintenance

  • Symbols of Flowchart

  • Programs to Do Simple Arithmetic (1 of 5)Example 2.4 Write a program to add the values of memory locations at $1000, $1001, and $1002, and save the result at $1100. Solution:Step 1 A m[$1000]Step 2A A + m[$1001]Step 3A A + m[$1002]Step 4$802 Aorg$1500ldaa$1000adda$1501adda$1002staa$1100end

  • Example 2.4 Write a program to subtract the contents of the memory location at $1005 from the sum of the memory locations at $1000 and $1002, and store the difference at $1100.Solution: org $1500ldaa $1000adda $1002suba $1005staa $1000endPrograms to Do Simple Arithmetic (2 of 5)

  • Example 2.6 Write a program to add two 16-bit numbers that are stored at $1000-$1001 and $1002-$1003 and store the sum at $1100-$1101.Solution:org $1500ldd $1000addd$1002std $1100end

    The Carry Flag- bit 0 of the CCR register- set to 1 when the addition operation produces a carry 1- set to 1 when the subtraction operation produces a borrow 1- enables the user to implement multi-precision arithmeticPrograms to Do Simple Arithmetic (3 of 5)

  • Example 2.7 Write a program to add two 4-byte numbers that are stored at $1000-$1003 and $1004-$1007, and store the sum at $1010-$1013.Solution: Addition starts from the LSB and proceeds toward MSB.

    org$1500ldd$1002; add and save the least significant two bytesaddd$1006;std$1012;

    ldaa$1001; add and save the second most significant bytesadca$1005;staa$1011;ldaa$1000; add and save the most significant bytesadca$1004;staa$1010;endPrograms to Do Simple Arithmetic (4 of 5)

  • Example 2.8 Write a program to subtract the hex number stored at $1004-$1007 from the the hex number stored at $1000-$1003 and save the result at $1100-$1103.Solution: The subtraction starts from the LSBs and proceeds toward the MSBs.org$1500ldd$1002; subtract and save the least significant two bytessubd$1006;std$1102;

    ldaa$1001; subtract and save the difference of the second to mostsbca$1005; significant bytesstaa$1001;

    ldaa$1000; subtract and save the difference of the most significant sbca$1004; bytesstaa$1100;endPrograms to Do Simple Arithmetic (5 of 5)

  • BCD Numbers and AdditionEach digit is encoded by 4 bits.Two digits are packed into one byteThe addition of two BCD numbers is performed by binary addition and an adjust operation using the DAA instruction.The instruction DAA can be applied after the instructions ADDA, ADCA, and ABA.Simplifies I/O conversionFor example, the instruction sequenceLDAA$1000ADDA$1001DAASTAA$1002adds the BCD numbers stored at $1000 and $1001 and saves the sum at $1002.

  • Multiplication and Division (1 of 2)

  • Example 2.10 Write an instruction sequence to multiply the 16-bit numbers stored at $1000-$1001 and $1002-$1003 and store the product at $1100-$1103.Solution:ldd$1000ldy$1002emulsty$1100std$1102Example 2.11 Write an instruction sequence to divide the 16-bit number stored at $1020-$1021 into the 16-bit number stored at $1005-$1006 and store the quotient and remainder at $1100 and $1102, respectively.Solution:ldd$1005ldx$1020idivstx$1100; store the quotientstd$1102 ; store the remainderMultiplication and Division (2 of 2)

  • Illustration of 32-bit by 32-bit MultiplicationTwo 32-bit numbers M and N are divided into two 16-bit halvesM = MHMLN = NHNL

  • Example 2.12 Write a program to multiply two unsigned 32-bit numbers stored at M~M+3 and N~N+3, respectively and store the product at P~P+7.Solution:org$1000Mds.b4Nds.b4Pds.b8org$1500lddM+2ldyN+2emul; compute MLNLstyP+4stdP+6lddMldyNemul; compute MHNHstyPstdP+2lddMldyN+2emul; compute MHNL

  • ; add MHNL to memory locations P+2~P+5adddP+4stdP+4tfrY,DadcbP+3stabP+3adcaP+2staaP+2; propagate carry to the most significant byteldaaP+1adca#0; add carry to the location at P+1staaP+1;ldaaP; add carry to the location at Padca#0;staaP;; compute MLNHlddM+2ldyNemul

  • ; add MLNH to memory locations P+2 ~ P+5adddP+4stdP+4tfrY,DadcbP+3stabP+3adcaP+2staaP+2; propagate carry to the most significant byteclraadcaP+1staaP+1ldaaPadca#0staaPend

  • Example 2.13 Write a program to convert the 16-bit number stored at $1000-$1001 to BCD format and store the result at $1010-$1014. Convert each BCD digit into its ASCII code and store it in one byte.Solution: -A binary number can be converted to BCD format by using repeated division by 10.-The largest 16-bit binary number is 65535 which has five decimal digits.-The first division by 10 generates the least significant digit, the second division by 10 obtains the second least significant digit, and so on.org$1000datadc.w12345; data to be testedorg$1010resultds.b5; reserve bytes to store the result

    org$1500ldddataldy#resultldx#10idivaddb#$30; convert the digit into ASCII codestab4,Y; save the least significant digitxgdxldx#10

  • idivadcb#$30stab3,Y; save the second to least significant digitxgdxldx#10idivaddb#$30stab2,Y; save the middle digitxgdxldx#10idivaddb#$30stab1,Y; save the second most significant digitxgdxaddb#$