Download presentation
Presentation is loading. Please wait.
1
M68K Assembly Language Programming Bob Britton Chapter 2 The M68K Architecture Notice: Slides in this series originally, provided by B.Britton, have been modified by R.Renner to reflect Spring 2006 course content.
2
M68K Datapath Diagram
3
The Basic Functional Components Control Unit Data Registers D0 through D7 and Address Registers A0 through A7 Arithmetic and Logic Unit (ALU) Program Counter (PC) Memory Instruction Register (IR)
4
Buses & Data Selectors A bus is nothing more than a set of electrical conducting paths over which different sets of binary values are transmitted. (32 tiny wires) Every data selector with two input buses must have a single control wire connected to it. The control unit sends a control signal of zero or one to select which input bus should be routed to the output of the data selector.
5
Registers The M68K architecture has a register file containing 16 registers. Each register has a capacity to hold a 32-bit value. The range of signed decimal values that can be represented in binary with 32 bits is - 2,147,483,648 (-2 31 ) to +2,147,483,647 (2 31 -1). A 32-bit register is constructed from 32 flip-flops (a digital circuit containing feedback)
6
ALU The ALU is a digital logic circuit designed to perform 32- bit binary integer addition & subtraction, as well as 32-bit binary logical operations such as AND, OR, NOT and XOR. Which operation the ALU performs depends upon the operation code in the instruction that has been fetched from memory. (Combinational Circuit) Multiplication, and division are accomplished by performing a sequence of 16 additions/subtractions.
7
Example Logical AND instruction AND.B D0, D7 00011101(D0) 00001110(D7) 00001100(D7 new)
8
Example Logical OR instruction OR.B D0, D7 00011101 00001110 00011111
9
Example of Binary Addition ADD.B D0, D7 Decimal Binary 2900011101 (D0) 1400001110 (D7) Sum= 4300101011 (D7 new)
10
Program Counter (Pointer) The Program Counter (PC) is a register that is initialized by the operating system to the address of the first instruction of your program in memory. Notice that the address in the program counter is routed to the address input of the memory via a bus.
11
Memory Memory can be thought of as a large array of locations where binary information is stored and from which binary information can be fetched. The M68K defines three data types: –A byte refers to an 8-bit quantity –A word refers to a 16-bit quantity (two bytes) –A long-word refers to a 32-bit quantity (four bytes) Each location in memory has a unique 32-bit address, so memory addresses range from 0 to 4,294,967,295 (2 32 -1).
12
Fetch Execute Cycle 1.Instruction Fetch Phase – An instruction is fetched from memory at the location specified by the Program counter. The instruction is loaded into the Instruction Register. The Program Counter is incremented to point to the next sequential instruction. 2.Operand Fetch Phase – Fields within the instruction contain binary codes that specify which registers should be accessed and routed to the ALU. The code for size will control how many bits are sent to the ALU. 3.Execute Phase – The ALU performs the operation which is specified by the opcode in the instruction. 4.Write Back Phase – The result of the operation is placed back into the same register where Source#2 came from. Go to step 1.
13
Instruction Register The Instruction Register (IR) holds a copy of the most recently fetched instruction. The M68K architecture has many different instruction formats, at the hardware encoding level, but for the assembly language programmer most instructions have the following format: Opcode.SizeSource#1, Source#2/Destination *Comment Notice that the destination for the computed result, be it a register or a memory location, is the same location as the second operand. In other words, the original value is wiped out. (this trade-off was used to conserve memory space for programs)
14
Example of M68K instruction ADD.L (A2), D5 * Memory[A2] + D5 D5 opcode mnemonic is: ADD The size of the values to be added together can be 8-bits (a byte) “.B”, 16-bits (a word) “.W”, or 32-bits (a long-word) “.L”, as in this example. In this example Source#1 is a 32-bit value in memory at a location pointed to by register A2. Here we are assuming that an address has been placed in address register A2 by some previous instruction. This is an example of an addressing mode call “address register indirect”. In this example, the second operand field specifies that the present contents of data register “D5” will be added to the value accessed from memory (Source#1) and the result of the addition will be placed back into data register D5. The last field contains a comment. The asterisk at the beginning of the comment field is optional. In this example, the pseudo-code equivalent of this assembly language instruction is provided to aid the reader in understanding the assembly language code. Comments are used to document a program.
15
Instruction Set Arithmetic instructions to add, subtract, multiply and divide Instructions to MOVE a copy of information from a source to a destination Logical bit-wise instructions AND, OR, EOR (exclusive-or) and NOT A set of conditional branch instruction BCC to implement control structures Instructions to branch to a subroutine BSR, and return from a subroutine RTS The instruction LEA to Load an Effective Address into an address register Instructions CMP and CMPA that are used to compare two values for less than equal to or greater than A TRAP instruction that is used to request an input or output (I/O) service from the operating system, such as to display values in their decimal representation or to print a string of characters; as well as a system service that will take input
16
The Load Effective Address Instruction Given that there is a place in memory that corresponds to the label “Message”, we can specify that when the program runs we want register A0 to be loaded with the binary address in memory of the first character of the string that we have labeled “Message”. LEAMessage, A0* Address of “Message” A0
17
Assembler Directives The following is an example of the define constant bytes DC.B directive that specifies that we want to initialize memory with the string of ASCII characters “Hello World” terminated with the ASCII null character zero, referenced with the label “Message”. Message DC.B‘Hello World’, 0
18
Addressing Modes The six addressing modes we will initially discuss are: Dn Data register direct: Operand in Dn An Address register direct: Operand in An (An) Address Register Indirect: An is a pointer to a memory location (An)+ Address register indirect with post auto increment (d, An) Address register indirect with displacement #n Immediate -- Constant operand stored as an extension of the instruction
19
Appendix A Quick Reference to Fundamental M68K Instructions Opcode Size Operands CCRSix Address Modes Operation bwl xnzvc Dn An(An) (An)+ (d,An)#n ADDbwl sor, Dn ***** sor sor sor sor sorsor (sor + Dn) Dn Dn, des ***** des --- des des des--- (Dn + des) des ADDAwl sor, An ------- sor sor sor sor sorsor (sor + An) An ANDbwl sor, Dn -**00 sor --- sor sor sorsor (sor AND Dn) Dn Dn, des -**00 des --- des des des--- (Dn AND ds) ds Bccbw label ------- --- --- --- --- ------ If cc true then (PC + d) PC BSRbw label ------- --- --- --- --- ------ PC - (SP); ( PC+d) PC CLR des -0100 des --- des des des--- 0 Dn CMPbwl sor, Dn -**** sor sor sor sor sorsor Set CCR with (sor - Dn) CMPAwl sor, An -**** sor sor sor sor sorsor Set CCR with (sor - An) DIVS w sor, Dn -***0 sor --- sor sor sorsor 32-bit Dn / 16-bit sor Dn (r:q) EORbwl Dn, des -**00 des --- des des des--- (Dn XOR des) des LEA l sor, An ------ --- --- sor sor sor--- “sor” address An MOVE*bwl sor, des -**00 s*d sor s*d s*d s*dsor sor des MOVEA wl sor, An ------ sor sor sor sor sorsor sor An * For the MOVE instruction the notation “s*d” means that the addressing mode corresponding for this column can be used in both the source and destination. No other M68K instruction has such broad addressing flexibility. Most other instructions require that either the source or the destination be specified as a register.
20
Appendix A (Continued) Quick Reference to Fundamental M68K Instructions Opcode Size Operands CCRSix Address Modes Operation bwl xnzvc Dn An(An) (An)+ (d,An)#n MULSw sor, Dn -**00 sor --- sor sor sorsor 16-bitDn x 16-bit sor Dn NOTbwl des -**00 des --- des des des--- !!!!!!!!!!( des) des ORbwl sor, Dn -**00 sor --- sor sor sorsor (sor OR Dn) Dn Dn, des -**00 des --- des des des--- (Dn OR des) des RTS ----- --- --- --- --- ------ (SP)+ PC SUBbwl sor, Dn ***** sor --- sor sor sorsor (sor - Dn) Dn Dn, des ***** des --- des des des--- (Dn - des) des SUBAwl sor, An ------- sor sor sor sor sorsor (sor - An) An TRAP#n ------- --- --- --- --- ------ Request a system service TSTbwl des -**00 des --- des des des--- N and Z Set to reflect des condition The following is a list of the more useful BRANCH instructions. The branch instructions use information in the Condition Code Register (CCR). In most cases the instruction immediately preceding the branch instruction will be the compare (CMP) instruction, or an arithmetic or logic instruction that affects the CCR. MnemonicNameMnemonicName BRAUnconditional BranchBVSBranch if Overflow is Set BGTBranch if Greater ThanBVC Branch if Overflow is Clear BGEBranch if Greater or EqualBNEBranch if Not Equal BLTBranch if Less ThanBEQBranch if Equal BLEBranch if Less or EqualBPLBranch if Plus (Positive) BCSBranch if Carry SetBMIBranch if Minus (Negative) BCCBranch if Carry Clear
21
EXAMPLE Let us suppose we want to subtract the 16-bit contents of register D1 from the 16-bit contents of register D2, placing result in the lower half of register D3, without modifying either source operand. Since in this case we do not want the contents of register D2 modified, it is necessary to first move a copy of the value in register D2 into register D3: move.wD2, D3* D2 D3 sub.w D1, D3* D3 - D1 D3 Note: The assembler is case insensitive. Note: Source#1 is subtracted from Source#2.
22
Multiplication The mnemonic for the signed multiply instruction is “MULS”. This instruction is only defined for 16-bit (word) operands. It multiplies two 16-bit binary values and produces a 32-bit product which is stored in the destination register. The Source#2/Destination field can only be a register. The following instruction would access 16-bits of data from memory at the location pointed to by address register A7, multiply it by the lower 16-bits of data in register D0 and store the 32-bit product back into register D0. This is an example of address register indirect addressing to get Source#1 from a memory location. MULS(A5), D0* D0 x Memory[A5] D0
23
Division The mnemonic for the signed divide instruction is “DIVS”. With this instruction there is no size field to specify. The dividend (Source#2) is always taken as 32-bits, and the divisor is always the lower 16-bits from Source#1. The resulting 16-bit remainder and 16-bit quotient will be put into the destination register. The following divide instruction divides the 32-bit binary value in register D3 by the 16-bit value in register D6. The quotient is stored in the lower 16-bits of register D3 and the remainder is stored in the upper 16-bits of register D3. DIVSD6, D3 * 32-bit D3 / 16-bit D6 (Remainder : Quotient) D3 Note: Source#1 is the 16-bit divisor With the divide instruction two possible run time error conditions that could occur: (1) Overflow – which causes the “V” bit in the status register to be set. (2) Division by zero – which will cause and exception to be generated and the program will terminate with control passing back to the operating system.
24
Status Register (CCR) You will notice there is a register associated with the ALU that has the letters XNZVC inside. This is the status register, and each letter has the following meaning: X : Extend (similar to the carry bit) N: Negative Z: Zero V: Overflow C: Carry When certain instructions are executed certain bits in the status register are either set to 1 (True) or cleared to zero (False) to provide information about the resulting value produced by the instruction that was just executed. You will notice in Appendix A there is a column in the table with the heading CCR (Condition Code Register) and below that we see “xnzvc”. You must refer to Appendix A to determine for sure which instructions affect which status bits. Below you see a portion of the table.
25
Branch Instructions The M68K provides instructions to implement control structures such as: “if (D6 < 0) then goto QUIT else D6 -1 D6 ” The above pseudocode states that if the binary number of register D6 is a value less than zero, in other words negative, we want to branch to a location in the program labeled “QUIT.” Otherwise (else) we want to decrement the contents of register D6. The assembly language instructions to accomplish this are: TST.L D6 * Test D6 BMIQuit * If N bit is set then branch to Quit SUB.L #1, D6 * (D6 – 1) D6
26
Immediate Mode In the previous section the instruction “SUB.L #1, D6” provides an example of immediate mode addressing. Immediate mode addressing should always be used to specify operands that are constants, because your program will run faster and use less memory space. At the assembly language level the sharp sign “#” indicates immediate mode addressing. At the machine language level the immediate binary value is stored in memory as part of the instruction. Notice in the datapath diagram there is a datapath from the right-hand portion of the instruction to the data selector that provides an operand input to the ALU. Immediate mode addressing may only be used to specify a Source#1 operand.
27
Auto Increment Addressing Mode Here we introduce address register indirect addressing with post-incrementing. This addressing mode also references memory at the location pointed to by the specified address register, but in addition the contents of the address register, which is a pointer, is then incremented by the amount specified in the size field. In other words, if the instruction is performing a long-word (4-bytes) operation then the address register is incremented by 4. If the instruction is performing a word (2- bytes) operation then the address register is incremented by 2, and if the instruction is performing a byte operation then the address register is incremented by 1. Obviously this is a very convenient addressing mode to sequentially access array elements. The following provides an example of the syntax for this addressing mode. To specify this addressing mode you simply place a plus sign (+) after the address register specification. move.b(A0)+, D6* Memory[A0] D6 ; then (A0 + 1) A0 Below we have an example of storing a long-word into memory using this post- increment addressing mode. move.lD2, (A6)+* D2 Memory[A6]; then (A6 + 4) A6
28
An Example M68000 Program START ORG$1000* Program origin in Memory CLR.WD1* Clear D1 to 0 MOVE#12, D7* Initialize loop counter to 12 LOOP ADD.WD7, D1* D1 + D7 D1 SUBQ.W#1, D7* Decrement loop counter BGT.S LOOP* If (D7 > 0) Branch to LOOP MOVEQ#3, D0* Code to print an integer is 3 TRAP#15* Print MOVE.B#9, D0* The code to halt is 9 TRAP#15* Halt simulation ENDSTART
29
Exercises 2.1 Explain the difference between a register and the ALU. 2.2 Explain the difference between assembly language and machine language. 2.3 Explain the difference between RAM Memory and the Register File. 2.4 Explain the difference between the Instruction Register and the Program Counter. 2.5 Explain the difference between a bus and a control line. 2.6 Identify a kitchen appliance that contains a control unit that issues control signals. 2.7 What is an algorithm? 2.8 Provide a step-by-step description of the sequence of operations that must take place within a M68K processor to fetch and execute the following instruction: BEQLoop 2.9 Provide a step-by-step description of the sequence of operations that must take place within a M68K processor to fetch and execute the following instruction: MOVE.L(A0), D0
30
Quick Reference to Fundamental I/O Tasks D0=Description (2)Read in a string of characters from the keyboard. The string is terminated by a carrage return (CR) character. Store the characters into an array in memory starting at the location specified by the address in register A1. The number of characters read in, including the “CR” character, will be returned in register D1.W. A maximum of 80 characters can be read in. (3)Display the value in register D1.L as a decimal number, using the least amount of space on the output display, in other words, left-justified.
31
D0=Description (4)Read a decimal number from the keyboard and store the binary equivalent in register D1.L. Typing in any character other than a decimal digit will invoke the decimal to binary conversion, and all trailing characters will be ignored. (9)Terminate the program (13)Display the NULL terminated string pointed to by the address in register A1, with a carrage return (CR) and a line feed (LF). The maximum number of characters that will be displayed is 255, which includes the CR and LF. (14)Display the NULL terminated string pointed to by the address in register A1, without a carrage return (CR) and a line feed (LF). The maximum number of characters that will be displayed is 255. Quick Reference to Fundamental I/O Tasks
32
ASCII Codes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.