• Disassembly of 9S12 op codes • Writing an assembly language program • Huang Sections 2.4, 2.5, 2.6 o Disassembly of 9S12 op codes o Use flow charts to lay out structure of program o Use common flow structures if-then if-then-else do-while while o Do not use spaghetti code o Plan structure of data in memory o Plan overall structure of program o Work down to more detailed program structure o Implement structure with instructions o Optimize program to make use of instruction efficiencies o Do not sacrifice clarity for efficiency Writing Assembly Language ProgramsUse Flowcharts to Help Plan Program StructureFlow chart symbols:IF-THEN Flow Structure if (C) { A; }EXAMPLE: if (A<10) { var = 5; } CMPA #10 BLT L1 BRA L2L1: LDAB #5 STAB varL2: next instructionOR: CMPA #10 BGE L2 LDAB #5 STAB varL2: next instructionIF-THEN-ELSE Flow Structure if (C) { A; } else { B; }if(A < 10) { var = 5; } else { var = 0; } CMPA #10 BLT L1 CLR VAR BRA L2L1: LDAB #5 STAB varL2: next instructionDO WHILE Flow Structure do { A; } while ( C );EXAMPLE: i = 0; do { table[i]=table[i]/2; i=i+1; } while (i <= LEN); LDX #table CLRAL1: ASR 1,X+ INCA CMPA #LEN BLE L1WHILE Flow Structure while ( C ) { A; } EXAMPLE: i = 0; while( i <= LEN) { table[i]=table[i]*2; i=i+1; } LDX #table CLRAL1: CMPA #LEN BLT L2 BRA L3L2: ASL 1,X+ INCA BRA L1L3: next instructionUse Good Structure When Writing Programs — Do Not UseSpaghetti CodeExample Program: Divide a table of data by 2Problem: Start with a table of data. The table consists of 5 values. Each value is between 0 and 255. Create a new table whose contents are the original table divided by 2.1. Determine where code and data will go in memory.Code at $1000, data at $2000.2. Determine type of variables to use.Because data will be between 0 and 255, can use unsigned 8-bit numbers.3. Draw a picture of the data structures in memory:4. Strategy: Because we are using a table of data, we will need pointers to each table so we can keep track of which table element we are working on.Use the X and Y registers as pointers to the tables.5. Use a simple flow chart to plan structure of program.6. Need a way to determine when we reach the end of the table.One way: Use a counter (say, register A) to keep track of how manyElements we have processed.7. Add code to implement blocks:8. Write the program:; Program to divide a table by two; and store the results in memoryprog: equ $1000data: equ $2000count: equ 5org prog ; Set program counter to 0x1000ldaa #count ; Use A as counterldx #table1 ; Use X as data pointer to table1ldy #table2 ; Use Y as data pointer to table2l1: ldab 0,x ; Get entry from table1lsrb ; Divide by two (unsigned)stab 0,y ; Save in table2inx ; Increment table1 pointeriny ; Increment table2 pointerdeca ; Decrement counterbne l1 ; Counter != 0 => more entries to divideswi ; Doneorg datatable1: dc.b $07,$c2,$3a,$68,$F3table2: ds.b count9. Advanced: Optimize program to make use of instructions set efficiencies:; Program to divide a table by two; and store the results in memoryprog: equ $1000data: equ $2000count: equ 5org prog ; Set program counter to 0x1000ldaa #count ; Use B as counterldx #table1 ; Use X as data pointer to table1ldy #table2 ; Use Y as data pointer to table2l1: ldab 1,x+ ; Get entry from table1; then inc pointerlsrb ; Divide by two (unsigned)stab 1,y+ ; Save in table2; then inc pointerdbne a,l1 ; Decrement counter; if not 0, more to doswi ; Doneorg datatable1: dc.b $07,$c2,$3a,$68,$F3table2: ds.b countTOP-DOWN PROGRAM DESIGN• Plan data structures in memory• Start with a large picture of the program structure• Work down to more detailed structure• Translate structure into code• Optimize for efficiencyDO NOT SACRIFICE CLARITY FOR
View Full Document