Assembly Language Programming



Download 37.61 Kb.
Date07.08.2017
Size37.61 Kb.
#28107
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:

    • These instructions are provided by the user.

    • They define data and symbols, allocate data storage locations, specify output format

    • 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

    1. symbols or identifiers

    2. optional

    3. when used provides symbolic memory reference such as a branch instruction address

    4. labels are also used to define constants

    5. Rules: (USUALLY)

      1. start with an alphabetic character or period or underscore in the FIRST Column

      2. may contain digits and other characters like the “_” , “.” , ”$”

      3. limitations on the number of characters (1 to 15)

      4. case sensitive

      5. may end up with a “:”

Example:

Label:


_TEST

_Test


$TEST NO

TEST$


TEST$DATA

Test_Data

1_TEST NO

Test_DATA NO

TEST DATA NO



  1. Operation Filed

    1. The op-code field contains mnemonics for the operation or an assembler directive.

    2. It must be proceeded by at least one white space from the left margin or a label

    3. The assembler is INSENSITIVE to the cases of the mnemonics

  2. Operand Filed

    1. The operand field (if present) follows the operation field by at least one space.

    2. 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




  1. Comment Filed

    1. The comment field is added for documentation and is ignored by the assembler

    2. * at start (first column)

    3. ; as an in-line comment

♣ ♣ ♣ ♣ ♣ ♣


Before we continue, let us put it all together in a simple example:

Main ( )


{

int i,j,k /* these are 8-bit inter variables

i=10;

j=20;


k=i+j;

}

Write the equivalent assembly code:




* Declare the data storage 1

ORG $0200 2

i RMB 1 ; variable i 3

j RMB 1 ;variable j 4

k RMB 1 ;variable k 5
* Program section 6

start ORG $2000 ;starting address of the program 7

LDAA #10 ;initialize i to 10 8

STAA i ; = = = = = = 9

LDAA #20 ;initialize j to 20 10

STAA j ;= = = = = = = = 11

ADDA i ;i+j 12

STAA k ;store value in k location 13

END 14

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 * Declare the data storage

0200 2 ORG $0200

0200 (0001) 3 i RMB 1 ; variable i

0201 (0001) 4 j RMB 1 ;variable j

0202 (0001) 5 k RMB 1 ;variable k

6

7 * Program section



2000 8 start ORG $2000 ;start add. of the prog.

2000 86 0A 9 LDAA #10 ;initialize i to 10

2002 B7 02 00 10 STAA i ; = = = = = =

2005 86 14 11 LDAA #20 ;initialize j to 20

2007 B7 02 01 12 STAA j ;= = = = = = = =

200A BB 02 00 13 ADDA i ;i+j

200D B7 02 02 14 STAA k ;store value in k location

2010 15 END


------------

15 Lines


0 Errors

0 Warnings


DISCUSS the above program.

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:

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 EQU 0

TRUE EQU 1


syntax:



Download 37.61 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page