Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
high, assembly and machine language Describe important terminologies Determine four fields of an assembly language instruction
Assembly language
Programmer need to Programmer do not know basic architecture need to know the of CPU such as registers architecture of CPU to realize the high level language programming
Important Terminologies :
Machine code
the
Important Terminologies :
object file
The source file is converted into an object file containing the actual binary information the machine will understand by a special program called an assembler contains all the original source file text plus the additional code generated by the assembler
list file
hex file
contains printable object code in a text format Hex files (called S-record files by Motorola) are useful for downloading over serial lines.
Important Terminologies :
Important Terminologies :
Cross-assembler
Cross-assemblers
are programs written in one language, such as C, that translate source statements into a second language: the machine code of the desired processor
Pseudo-opcode
Labels (or symbols) are optional and are used to identify a particular statement so that it can be referred to from other parts of the program
MNEMONIC
The mnemonic defines the function of a particular line and it can either be one of the 68000 instruction mnemonics or it may be an assembler directive or pseudo-instruction.
OPERANDS
Operand
may be a 68000 register name, an address or data or a label referring to either an address or data When two operands are required, the first operand specifies the destination for the result of the operation and the second operand specifies the source of the data for the operation
COMMENTS
Comments
are used to describe the function of the program Any text preceded by a semi-colon(;) or asterisk (*) is ignored by the assembler but will be included as a comment in the listing of the program
Assembler Directives
Assembler directives statements give the assembler information that affects the assembly process, but do not necessarily cause object code to be generated
ORG sets the ORIGIN address this is the first memory location to be used for storage of the program
EQU used to define (or EQUATE) the value of a label DC Define Constant used to store a byte (or
Assembler Directives
DS
Define Storage used to reserve memory locations for use by the program make the program counter even
ALIGN
EVEN
TCALL
allows calls to the operating system using the 68000 TRAP instruction
groups of instruction sets of the 68000 microprocessor. Write assembly language using each group of instruction set. Use "DEBUG" program to debug assembly language programs.
Move instruction
Use
to copy/transfer data from source to destination Transfer can be done between register and memory Instruction format
MOVE.s
source, destination
W(word) L(longword)
Move instruction
Use
to copy/transfer data from source to destination Transfer can be done between register and memory Instruction format
MOVE.s
source, destination
W(word) L(longword)
Addressing Modes
Addressing Modes
ADDITION
ADD
EA, Dn (EA) + Dn Dn
allow Byte, Word and Long Word data is accessed via register D0 D7, memory, absolute data and I/O Port
SUBTRACTION
SUB
EA, Dn Dn - (EA) Dn
similar to the logic gate, logical operations involve AND, OR and NOT the content of the register and memory are compared the operation is implemented in ALU
Shift Instructions :
Arithmetic (signed) (0 to 127, -1 to -128) Logical (unsigned) (0 to 255)
Rotate Instructions :
Ordinary Rotate Rotate through X Flag
types of addressing modes. Code assembly language instructions using each addressing mode.
Data register direct Address register direct Absolute short Absolute long Register indirect Post-increment register indirect Pre-decrement register indirect Register indirect with offset Register indirect with index and offset PC-relative with offset PC-relative with index and offset Immediate Immediate quick Implied register
Description
data register (D0-D7) is the source or destination of data since .B is appended to MOVE, only the low byte of the destination data register affected MOVE.B is D0,D3 INSTRUCTION
MEMORY REGISTER
Example
ADDRESS
BEFORE
CONTENTS
NAME
D0 D3 D0 D3
CONTENTS
1020 4FFF 1034 F88A
1020 4FFF 1034 F8FF
AFTER
Description
address register (A0-A7) is the destination of data only word or longword operands may be specified A word operand is sign-extended to fit the register
Example
REGISTER CONTENTS
A0 A3
A0 A3
AFTER
Absolute short
Description
source or destination is a memory location whose address is specified in one extension word of the instruction
bits 16-23 of the full address are obtained by sign-extension of the 16-bit short address
Example
The source is immediate, destination is absolute short address Since operation is .w, source is sign-extended to two bytes
INSTRUCTION
MOVE.W #$1E, $800
BEFORE AFTER
Absolute long
Description
source or destination is a memory location whose address is specified in two extension word of the instruction
bits 16-23 of the full address are obtained by sign-extension of the 16-bit short address
Example
Note
Often, the distinction between abs-short and abs-long is transparent to the programmer due to the use of labels
INSTRUCTION
MOVE.B #$1E, $8F00
Register indirect
Description
An address register contains the address of the address of the source or destination operand The instruction moves a longword stored in D0 to the memory location specified by he address in A0
MOVE. L D0, (A0)
Example
INSTRUCTION
BEFORE
AFTER
MEMORY ADDRESS CONTENTS 001000 55 001001 02 001002 3F 001003 00 001000 10 001001 43 001002 83 001003 4F
A0 D0
00001000 1043834F
Description
Indicated by a + sign after (Ai) after reading or writing data the address register is incremented by the number of bytes transferred Byte : [Ai] [Ai] + 1 Word : [Ai] [Ai] + 2 Longword : [Ai] [Ai] + 4
INSTRUCTION
MOVE. W (A5)+, D0
BEFORE
AFTER
MEMORY ADDRESS CONTENTS 001000 45 001001 67 001002 89 001003 AB 001000 45 001001 67 001002 89 001003 AB
A5 D0
00001000 0000FFFF
A5 D0
00001002 00004567
Description
Indicated by a - sign before (Ai) before reading or writing data the address register is decremented by the number of bytes transferred Byte : [Ai] [Ai] - 1 Word : [Ai] [Ai] - 2 Longword : [Ai] [Ai] - 4
INSTRUCTION
MOVE. W D0,-(A7)
BEFORE
AFTER
MEMORY ADDRESS CONTENTS 001000 10 001001 12 001002 83 001003 47 001000 01 001001 43 001002 83 001003 47
A7 D0
00001002 00000143
A7 D0
00001002 00000143
Description
A variation of register indirect that includes a 16-bit signed offset (displacement) as an extension word in the instruction The sign-extended offset is added to the address register to form the effective address of the source or destination
Example
Effective address is 6 plus address register Value stored in the address register does not change
INSTRUCTION
MOVE. W 6 (A0),D0
BEFORE AFTER
Description
Another variation of register indirect. An index is used as well as an 8-bit signed offset The effective address is formed by adding the sign-extended offset, the contents of the index register and the contents of the address register
Example
<ae>=$10+$100A+$2=$101C
INSTRUCTION
MOVEA $10(A0,D0.L),A1
AFTER
Description
a 16-bit offset is added to PC to form effective address only source operand can be addressed this way this mode provides position-independent code assembler computes offset by subtracting PC from label
3A3A 0FFE ABCD 1 2 3 4 5 ORG MOVE.W ORG COUNT END $1000 COUNT (PC), D5 $2000 DC.W $ABCD $0FFE = $2000-$1000$2
INSTRUCTION
MOVE.W COUNT(PC),D5
BEFORE AFTER
Description
an 8-bit signed offset plus and index register are used to compute the address relative to the PC
00001000 00001000 00001004 00001008 0000100A 0000100E 00001010 00001016 303C 0005 6100 0004 4E75 103B 0004 4E75 000104091019 1 2 3 4 5 6 7 8 ORG MOVE.W BSR RTS MOVE.B RTS DC.B END $1000 #5,D0 SQUARE TABLE (PC,D0.W),D0 0,1,4,9,16,25
SQUARE TABLE
INSTRUCTION
MOVE.B TABLE(PC,D0.W),D0
Immediate
Description
Immediate address uses two extension words to hold the source operand Data may be expressed in :
decimal (& prefix or none) hexadecimal ($ prefix) octal (@ prefix) binary (% prefix) ASCII (string within )
MOVE.B #$1FFFF,D0
INSTRUCTION
Immediate quick
Description
Immediate quick addressing is an optimized case of immediate addressing whose binary code fits in one word (including the operand) Immediate operand is sign-extended to fit the 32-bit destination Available with the following instructions
INSTRUCTION
MOVEQ (operand must be a 8-bit signed integer) ADDQ (operand must be in the range 1 to 8) SUBQ (operand must be in the range 1 to 8)
MOVEQ #$1F,D0
the operation of stacks and queues according to LIFO and FIFO concept.
One of the registers in the MC68000 microprocessor is status register. It is a 16 bits register. Each bit has it own function with some of the bits unused. The status register has 16 bits and is divided into the system byte and user byte. The user byte contains five condition flags. The remaining 3 bits in the user byte are not used and remain zero. The condition flags contain information on the result of the last processor operation.
Ex2 :
Ex3 :
Example :
If initial data for Status Register (SR) = $A71F and D2 = 6677 8899, what are the end data in SR and D2 after ADDI.B #$5E,D2 instruction been executed. Bits how SR T 0 S 0 of 0 flags I2 I1 obtained I0 0 0 0 X N Z V Show each bit
Initial End A71F ? 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 ? 1 ? 1 ? 1 ?
C 1 ?
the operation of stacks and queues according to LIFO and FIFO concept.
What is STACK ?
Stack is a memory-based structures a STACK is an area in memory reserved for reading and writing special data items such as return addresses and register values. Ex :
i) move.l D3,-(A7) Entire contents of D3 are written into the stack area pointed to by A7. A7 is automatically decremented by 4 during execution. Item previously pushed onto the stack
Cont
Ex :
ii) move.l (A7)+,D3 Stack memory is read out into D3 and A7 is automatically incremented by 4 during execution. items popped off the stack (read out of memor)
STACK Characteristic
One characteristic of a stack is that the last item pushed is always the first item popped. Stack are commonly referred to as a LIFO (last in, first out) structure.
LIFO ILLUSTRATION
A stack of trays. The last tray to be placed on top is also the first to be taken off the top.
QUEUE
Stack is a memory-based structures In a QUEQU, the first item loaded is the first item to be removed. Queues are referred to as FIFO (first in, first out).
FIFO ILLUSTRATION
Persons standing in line. Persons leave the queue in the order they arrive.
Stack Pointer
Stack Pointer A stack pointer is used to point to the last stack location used.
Cont
PUSH (input) Data PUSH into stack (Stack SAVE) as data are pushed into the stack, the stack grows towards the lower address POP (output) Data POP from stack moving towards higher address
Examples :
Question 1 : SOLUTION :
SP Address Stack
SP-2
SP-1
0000 2FFE
0000 2FFF
12
34
SP 0000 3000 X location 0000 2FFEH contains 12H location 0000 2FFFH contains 34H Stack Pointer = 0000 2FFEH
Examples :
Question 1 :
Stack Pointer (A7)=0000 4200H, D0 = 1122 3344 move.w (SP)+,D0 Has the following contents SP Address Stack
0000 41FE 0000 41FF 0000 4200 0000 4201 0000 4202 9A 78 56 34 12 SP SP + 1 0000 41FE 0000 41FF 0000 4200 0000 4201 0000 4202 9A 78 56 34 12
SOLUTION :
SP
Address
Stack
Exercise :
Question 1 : ANSWER
Exercise :
Question 2 :
Stack Pointer (A7)=0000 1000H
D0 = AABB CCDD move.l (SP)+,D0 Has the following contents
ANSWER
SP
Address
0000 0FFF 0000 1000
Stack
DE BC
0000 1001
0000 1002 0000 1003 0000 1004
9A
78 56 34
the flowchart for a simple problem using branching and subroutines. Apply assembly-level structured programming techniques using branching and subroutines.
Concept of Programming
Definition of problem Logical design Programming Test run and Analysis the program Documentation of the program
Steps :
1. Definition of problem Input data of two numbers. These two numbers are added, and the product is stored in a memory location.
2. Logical design
START Data1 D0 Data2 D1
D0 + D1 D1
D1 Memory
END
Steps :
3. Programming ORG $7000 MOVE.B #12,D0 MOVE.B #34,D1 ADD.B D0,D1 MOVE.B D1,$7050
END
Steps :
4. Test run and Analysis the program
ORG $7000
$7000
This is a assembler directive to store the opcodes of this program in the memory started at address $7000. MOVE.B #12,D0 ; decimal data 12 Before After stored in D0; end program execution. D0 XXXXXXXX XXXXXX0C From instruction list, the operation of this instruction:
Since: 1210 = 0CH; thus, D0 = $xxxxxx0C MOVE.B #34,D1 >> #34 D1 ADD.B D0,D1 D0 + D1 D1 Since: 34 10 = 22H; thus, D1 = $xxxxxx22 ; Before D0 XXXXXX0C D1 XXXXXX22 From instruction list, the operation of this instruction: ADD.B EA,Dn >> (EA) + Dn Dn MOVE.B D1,$7050 ; product of addition stored in memory at location $7050 ADD.B D0,D1 >> D0 + D1 of this D1 From instruction list, the operation instruction: D0 + D1 = $0C + $22 = $2E MOVE.B SRC,DST >> (SRC EA) DST EA D1 = $xxxxxx2E END ; end program execution. MOVE.B D1,$7050 >> D1 M($7050) $2E M($7050) Before D0 XXXXXXXX After XXXXXX0C After XXXXXX0C XXXXXX2E
Steps :
5. Documentation of the program
ORG $7000 $7000 ; The program initiled at address MOVE.B #12,D0 ; decimal data 12 stored in D0 MOVE.B #34,D1 ; decimal data 34 stored in D1 ADD.B D0,D1 ; D0 + D1 D1 MOVE.B D1,$7050 ; product of addition stored in memory at location $7050 END ; end program execution.
Flowchart
A flowchart is really a shorthand technique for describing the processes within a system Flowcharts can also be used to describe computer programs
Branch Instruction
JSR
RTE RTR RTS
JSR $11010
RTE RTR RTS
Bcc instruction is for branching based on a condition BRA - instruction is for the branching always BSR - instruction is for the branching to a subroutine DBcc - instruction is for testing a condition, decrementing and branching JMP instruction is for jumping JSR istruction is for jumping to a subroutine
RTE instruction is for returning from an exception RTS instruction is for returning from a subroutine RTR - instruction is for returning from a subroutine and restoring condition codes
Construct of Sequential
Construct of Loop
Subroutines
The subroutine concept is an approach to simplify a very long or complex program. There are several instructions in a program tend to be repeated to perform the similar function, or with a little alteration. Thus it is more practical to group these few instructions into a sub-program, and to be called whenever necessary by calling its name or label.
Subroutines cont
This sub-program is normally referred as subroutine in M68000. In other words, a subroutine is a special segment of program that can be called for execution from any point in a program.
Subroutine concept
Ex :
Ex :
Subroutines Instructions
RTS instruction restores the program counter from the point at which it left the main program
Subroutines Instructions
instruction has a similar function to JSR except that the address of the subroutine to be called is specified by a relative address (offset) rather than an absolute address. Like the BRA instruction, BSR is restricted to 8-bit or 16-bit offsets