3D1-Microprocessor Systems 1 Lecture 4: Introduction to Assembly Language Programming We have already looked at the architecture of a computer based on the Von Neumann model. We then presented the programmer’s of the Motorola 68000 and the organisation of memory. We are now looking at some of the concepts that underlie an assembly language. Learning Outcomes: On completion of this lecture, you will be able Define the four fields in a assembly language program; Distinguish assembler directives and executable statements; Distinguish decimal, hexadecimal, binary and ASCII data representation; Understand the listing file produced by an assembler. 4.1 Introduction to the Assembler An assembly language is a language that controls the primitive operations on binary data within a computer. An assembly language program is made of two types of statements: executable instructions and assembler directives. An executable instruction is one of the processor’s valid instructions and is translated into the appropriate machine code form by the assembler. Assembler directives cannot be translated into machine code; they indicate the assembler specific information about the program and its environment. They link symbolic names to actual values (EQU), allocate storage for data in memory (DS), set up pre-defined constants (DC) and control the assembly process (ORG, END) 4.1.1 Simple “Hello World” and binary count-down Let’s look at an example of an assembly language program that prints a zero-terminated string on simulates a binary counter using an array of 8 LEDs: delay equ start equ 500 *500ms delay between counts %11111111 *starts count down from 255 *Display to LCD org lea $4000 msg,a0 trap #7 *Program origin *a0 points to the starting memory location of zero*terminated string to display *displays the zero-terminated string pointed by a0 *on the top line of the LCD Display *Countdown using LED loop msg move.w move.b move.w trap dbra #start,d1 d1,$30000 #delay,d0 #$d d1,loop *d1 <- $FF *LED array <- [d1] *d0 <- $1F4 *Delay by the number of milliseconds specified in d0 *Decrement d1 and goto to loop if result greater * than zero *returns control of the CPU back to the Robot’s OS trap #0 dc.b end 'Hello World',$0 *zero-terminated string 4-1 3D1-Microprocessor Systems 1 4.1.2 Program organisation The assembly language program is written in four columns (or fields): an optional label that must begin in column 1; an up-code instruction field; an field for the associated operands; and an optional comment field used by the programmer to document a program. A word beginning in the left-most column is a programmer-defined label and is used by the assembler as a reference to that line. E.g.: delay, start, loop and msg. A line beginning with an asterisk in its first column is a comment and is entirely ignored by the assembler. Consider the line: loop l “loop” is a label that refers this line. to move.b d1,$30000 “move.b d1,$30000” is the actual instruction to be assembled. *LED array <- [d1] This is the comment field and is entirely ignored by the assembler. The assembler requires at least one space to separate the label and comment field from the actual instructions. 4.1.3 Decimal, Hexadecimal, binary and ASCII representation A number without a prefix in an assembly language program is treated as a decimal value. e.g.: delay equ 500. Prefixing a number with a $ symbol indicates a hexadecimal value. e.g.: org $4000 Prefixing a number with a % indicates a binary value. e.g.: start equ %11111111 Enclosing a text string in single quotes indicates a sequence of ASCII characters. eg.: msg dc.b 'Hello World'. 4.1.4 Assembler listing file The program is assembled producing a listing file. The following listing file contains the original assembly language and the machine code (in hexadecimal) produced by the assembler. 4-2 3D1-Microprocessor Systems 1 4.2 Main assembler directives We now describe the five major assembler directives. 4.2.4 EQU The equate directive links a name to a value, making the program easier to read. For example, it might be better to equate the name “delay” to $1f4 and use this name in the program, rather than writing $1f4 and leaving the reader to figure out that it is the number of milliseconds to wait between consecutive counts. The use of names makes the code more meaningful to the programmer and facilitates future modifications and updates. Most 68000 assemblers permit the programmer to employ an expression as valid name. 4.2.5 DC DC means define a constant and is qualified by .B, .W or .L, depending on the size of the constant (8, 16 or 32 bits). The constant defined by this directive is loaded in memory at the current location. E.g.: the assembler directive DC.B ‘Hello World’,$0 loads the bytes $48, $65, $6C, $6C, $6F, $20, $57, $67, $72, $6C, $64 and $00 into memory starting at address $401E. Constants can be stored in consecutive locations by separating each one with a comma. 4.2.6 DS The define storage directive, DS, reserves storage locations in memory. It has a similar effect to DC, but no information is stored in memory. DC is used to set up constant values that are to be loaded into memory before the program is executed, whereas DS reserves memory space for variables that are generated by the program at runtime. DS is also qualified by .B, .W or .L and takes the same types of operands as DC. A label in the label field can be used to refer to the first address of the defined storage. The label references the lowest address of the define storage value. 4.2.7 ORG The origin assembler directive sets up the value of the location counter that keeps track of where the next item is to be located in the target processor’s memory. The operand following ORG is the absolute value of the origin. ORG can be located at any point in a program. 4.2.8 END The end directive indicates the assembler that the end of a program has been reached and that there are no further instructions or directives to be assembled. 4-3 3D1-Microprocessor Systems 1 4.3 Conclusion At this stage, we have some familiarity with the organisation of a 68000 assembly language program. We saw that programs are composed of assembler directives and executables instructions and we described five of the most important directives: EQU, DC, DS, ORG and END. Executable instructions are composed of an up-code and operands. We will later explain the main mechanisms indicating the address of operands to the assemblers. REFERENCES A. Clements; Introduction to Computer Architecture and Assembly language, In: 68000 Family Assembly Language; pp. 61-75; PWS Publishing Company; 1994. Dr. Mike Brady, Microprocessor Systems 1, dept. of Computer Science, Trinity College Dublin: http://www.tcd.ie/Engineering/Courses/BAI/JS_Subjects/3D1/. Look on the Web at http://www.mee.tcd.ie/~assambc/3D1. 4-4