Download Assembly Language Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assembly Language Programming
Assembly Language Programming is a method of creating instructions that are the
symbolic equivalent of machine code. Thus, an assembler is a program that accepts the
source code (.s19) as an input file and produces the 1s and 0s that are to be placed in the
memory of the computer - this is called the machine code. There is a one to one
correspondence between the assembly instructions and the machine code in memory.
Wide View
An assembly language program is divided into four sections that contain the main
program components:
Assembler directives:
o These instructions are provided by the user.
o They define data and symbols, allocate data storage locations, specify
output format
o Assembler directives do not produce machine code
Assembly language instructions: The 68HC11 instructions
Comments: Explain a line code or a group of lines
END directive: Last statement – ignored also in assembler
Assembly Source Code Fields:
Each line of the 68HC11 (in general) comprises of four distinct fields- some of
the fields may be empty. The order of the fields is:
1- Label Field
a. symbols or identifiers
b. optional
c. when used provides symbolic memory reference such as a branch
instruction address
d. labels are also used to define constants
e. Rules: (USUALLY)
i. start with an alphabetic character or period or underscore in the
FIRST Column
ii. may contain digits and other characters like the “_” , “.” , ”$”
iii. limitations on the number of characters (1 to 15)
iv. case sensitive
v. may end up with a “:”
Example:
Label:
_TEST
_Test
$TEST
NO
TEST$
TEST$DATA
Test_Data
1_TEST
Test_DATA NO
TEST DATA NO
NO
2- Operation Filed
a. The op-code field contains mnemonics for the operation or an assembler
directive.
b. It must be proceeded by at least one white space from the left margin or a
label
c. The assembler is INSENSITIVE to the cases of the mnemonics
3- Operand Filed
a. The operand field (if present) follows the operation field by at least one
space.
b. The operand field may contain operands for instructions or arguments for
assembler directives.
Examples:
Operation
Operand
Field
Field
ADDA
#$10
ABC
EQU
0
LOOP: BNE
HERE
4- Comment Filed
a. The comment field is added for documentation and is ignored by the
assembler
b. * at start (first column)
c. ; as an in-line comment
♣
♣
♣
♣
♣ ♣
Before we continue, let us put it all together in a simple example:
Main ( )
{
int i,j,k
i=10;
j=20;
k=i+j;
}
/* these are 8-bit inter variables
Write the equivalent assembly code:
* Declare the data storage
ORG $0200
i
RMB 1
; variable i
j
RMB 1
;variable j
k
RMB 1
;variable k
1
2
3
4
5
* Program section
start ORG $2000
LDAA #10
STAA i
LDAA #20
STAA j
ADDA i
STAA k
END
6
7
8
9
10
11
12
13
14
;starting address of the program
;initialize i to 10
;======
;initialize j to 20
;= = = = = = = =
;i+j
;store value in k location
Before continuing further, let us do the following
1- Write the above program in Motpad and save it as .asm file (example1.asm)
2- generate program listing .l file
cas11 –l example1.asm (note it is –L not –ONE)
Here is the generated program listing
CalU 68HC11 Cross Assembler, ver. 2.04
3/04/2007 16:35:12
Page 1
1
0200
2
0200 (0001)
0201 (0001)
0202 (0001)
2000
2000
2002
2005
2007
200A
200D
2010
86
B7
86
B7
BB
B7
0A
02
14
02
02
02
00
01
00
02
3
4
5
6
7
8
9
10
11
12
13
14
15
-----------15 Lines
0 Errors
0 Warnings
DISCUSS the above program.
* Declare the data storage
ORG $0200
i
j
k
RMB
RMB
RMB
1
1
1
; variable i
;variable j
;variable k
* Program section
start ORG $2000 ;start add. of the prog.
LDAA #10
;initialize i to 10
STAA i
; = = = = = =
LDAA #20
;initialize j to 20
STAA j
;= = = = = = = =
ADDA i
;i+j
STAA k
;store value in k location
END
Now, let us discuss some directives:
Directives:
Assembler directives are important part of an assembler program. They can define the
program’s location in memory, they allow us to define symbols and contents of memory
locations.
Let us visit some of the directives:
ORG sets the value of the location counter and thus it tells the assembler
where to put the next byte it generates after the ORG directive. The syntax is as follows:
ORG < expression>
Example:
ORG $2000
LDAB #$EE
Will place the opcode byte for the instruction LDAB #$EE at location $2000.
RMB (Reserve Memory Byte) reserves a block of memory whose size is specified by the
number that follows the directive. The syntax is:
<label> RMB <expression>
Example:
BUFFER RMB 80
allocates 80 decimal bytes for some data. The programmer can refer to it using
the label BUFFER.
Example:
Suppose we want to load the first byte in BUFFER into accumulator A we do:
LDAA BUFFER
To load the 8th byte we do:
LDAA BUFFER + 7
Also, we can use the directive ORG to specify where the block of memory be
reserved:
ORG $100
BUFFER
RMB 80
this will cause a block of 80 bytes be reserved at the starting address of $100.
EQU (Equate a symbol to a value)
Examples:
zero
TRUE
EQU 0
EQU 1
syntax:
<label>
EQU <expression>
A
EQU 2
B
EQU 4*A
-
BSZ (Block Storage of zeros) causes the assembler to allocate a block of
bytes and assign each a value of zero. The number of bytes is specified in
the expression.
Syntax:
<label>
BSZ
<expression>
Example:
ABC
EQU 60
BUFFER
BSZ ABC
allocates and sets to zero a storage buffer of 60
-
FCB (Form Constant Byte) this directive will put a byte in memory for
each argument of the directive. The syntax is:
<label>
FCB <expression1>,<expression2>, ….
Example:
ORG $1000
XYZ FCB $10,$18,$25,$40
This will initialize the contents of memory locations $1000, $1001, $1002, and $1003 to
the values: $10, $18, $25, and $40.
-
LETTERS
Form Constant Character: FCC
o will generate the code bytes for the letters in the argument of the
directives, using ASCII code. ALL letters to be coded and stored are
enclosed in double quotes. The format is:
o <label> FCC “<string>” ; comments
o Example:
FCC “DEF” ; this is an example
This will generate the following values in memory:
$44, $45, $ 46
Also, it will let the assembler know that the label LETTERS refers to the address of the
first letter.
-
SPACE
Define Constant Block Directive DCB
o will reserve an area of memory and initializes each byte to the same
constant value
o The format of this directive is:
o <label> DCB <length>,<value>
o Example:
DCB
80,$20
A line of 80 space characters is generated. The label SPACE refers to the first space
character. The value $20, is the ASCII code to the space character.
Sometimes, DCB is not supported by most freeware (ours does not support it)
Let us write some programs:
Challenge -1 (Due Date to be announced)
Write a program that will multiply 16 bit numbers stored at locations {M, M+1} and {N,
N+1} and store the product at memory locations { P, P+1, P+2, P+3} Where P is the
highest memory byte (H) and P+3 is the lowest memory byte (L).
Here is a graphical illustration of the process:
8-bit
+
Address
Upper
Byte
P
MSB
8-bit
Upper
Byte
Upper
Byte
Lower
Byte
P+1
8-bit
Upper
Byte
Lower
Byte
Lower
Byte
8-bit
Lower
Byte
partial product: MLNL
Partial Product: MHNL
Partial Product: ML NH
Partial Product: MHNH
P+2
P+3
LSB
Final Product: M N
Test your solution for the given case:
M: = $24A1 at location $200, $201
N = $41B7 at location $202, $203
P, the result should be at locations: $20A, $20B, $20C, $20D
use assembly, Sim11 and MotPad.