0% found this document useful (0 votes)
166 views8 pages

Disassembly of 9S12 Op Codes Writing An Assembly Language Program

The document discusses writing assembly language programs using a top-down approach. It recommends using flowcharts to plan the program structure and common flow structures like if-then, if-then-else, do-while, and while. The document provides an example of writing a program to divide a table of data by 2 using pointers, a counter, and optimizing the code for efficiency while maintaining clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views8 pages

Disassembly of 9S12 Op Codes Writing An Assembly Language Program

The document discusses writing assembly language programs using a top-down approach. It recommends using flowcharts to plan the program structure and common flow structures like if-then, if-then-else, do-while, and while. The document provides an example of writing a program to divide a table of data by 2 using pointers, a counter, and optimizing the code for efficiency while maintaining clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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 Programs


Use Flowcharts to Help Plan Program Structure
Flow chart symbols:

IF-THEN Flow Structure


if (C)
{
A;
}

EXAMPLE:
if (A<10)
{
var = 5;
}

CMPA
#10
BLT
L1
BRA
L2
L1: LDAB #5
STAB
var
L2: next instruction
OR:

L2:

IF-THEN-ELSE Flow Structure


if (C)
{
A;
}
else
{
B;
}

CMPA
#10
BGE
L2
LDAB
#5
STAB
var
next instruction

if(A < 10)


{
var = 5;
}
else
{
var = 0;
}

L1:
L2:

CMPA
#10
BLT
L1
CLR
VAR
BRA
L2
LDAB
#5
STAB
var
next instruction

DO WHILE Flow Structure


do
{
A;
}
while ( C );

EXAMPLE:
i = 0;
do
{
table[i]=table[i]/2;
i=i+1;
}
while (i <= LEN);

LDX
CLRA
L1: ASR
INCA
CMPA

BLE

#table
1,X+
#LEN

L1

WHILE Flow Structure


while ( C )
{
A;
}

EXAMPLE:
i = 0;
while( i <= LEN)
{
table[i]=table[i]*2;
i=i+1;
}

LDX
#table
CLRA
L1: CMPA
#LEN
BLT
L2
BRA
L3
L2: ASL
1,X+
INCA
BRA
L1
L3: next instruction

Use Good Structure When Writing Programs Do Not Use


Spaghetti Code

Example Program: Divide a table of data by 2


Problem: 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 many
Elements 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 memory
prog: equ
data: equ
count: equ

l1:

org
ldaa
ldx
ldy
ldab
lsrb
stab
inx
iny
deca

$1000
$2000
5
prog
#count
#table1
#table2
0,x
0,y

; Set program counter to 0x1000


; Use A as counter
; Use X as data pointer to table1
; Use Y as data pointer to table2
; Get entry from table1
; Divide by two (unsigned)
; Save in table2
; Increment table1 pointer
; Increment table2 pointer
; Decrement counter

bne l1
swi

; Counter != 0 => more entries to divide


; Done

org data
table1: dc.b $07,$c2,$3a,$68,$F3
table2: ds.b count
9. Advanced: Optimize program to make use of instructions set efficiencies:
; Program to divide a table by two
; and store the results in memory
prog: equ
data: equ
count: equ

l1:

$1000
$2000
5

org prog
ldaa #count
ldx
#table1
ldy
#table2
ldab 1,x+
lsrb
stab 1,y+
dbne a,l1
swi

; Set program counter to 0x1000


; Use B as counter
; Use X as data pointer to table1
; Use Y as data pointer to table2
; Get entry from table1; then inc pointer
; Divide by two (unsigned)
; Save in table2; then inc pointer
; Decrement counter; if not 0, more to do
; Done

org data
table1: dc.b $07,$c2,$3a,$68,$F3
table2: ds.b count
TOP-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 efficiency
DO NOT SACRIFICE CLARITY FOR EFFICIENCY

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy