Modules Register name Abbr Register's function Current instruction register CIR Stores the current instruction while it is being decoded and executed Index register IX Stores a value; only used for indexed addressing Memory address register MAR Stores the address of a memory location which is about to have a value read from or written to Memory data register (memory buffer (MBR) register) MDR Stores data that has just been read from memory or is just about to be written to memory Program counter PC Stores the address of where the next instruction is to be read from Status register SR Contains individual bits that are either set or cleared
Programming languages Programming languages can be classified into 3 High level programming languages Assembly Language Low level - Machine Code/ Language The lowest level of programming languages is machine language. This is the language the computer can understand as is in the form of zeroes and Ones. Programming in machine language is very difficult.
Programming languages Machine language is necessary in cases where one wants to write instructions which directly control the processor Assembly language is the next level. It uses mnemonics which are easier than machine code but still difficult to understand as compared to high level programming languages. Assembly language translates on a one to one basis with machine code. Meaning one line of assembly language will convert to one line of machine code.
Machine code instructions We need to start with a few facts. • The only language that the CPU recognises is machine code. • Machine code consists of a sequence of instructions. • An instruction contains an opcode . • An instruction may not have an operand but up to three operands are possible. • Different processors have different instruction sets associated with them. • Different processors will have comparable instructions for the same operations, but the coding of the instructions will be different.
Machine code instructions For a particular processor, the following must be defined for each individual machine code instruction : • the total number of bits or bytes for the whole instruction • the number of bits that define the opcode • the number of operands that are defined in the remaining bits • whether the opcode occupies the most significant or the least significant bits.
Machine code instructions
Assembly language Assembly language consists of 2 parts a mnemonic (a symbolic abbreviation) for the opcode a character representation for the operand. A program written in assembly language has to be translated into machine code before it can be executed by the processor. The translation program is called an 'assembler',
Assembler language Special features which can be included in a assembly language programme include: comments symbolic names for constants helps the programmer labels for addresses macros subroutines directives system calls.
Programming languages Comments are removed by the assembly during the translation process. Symbolic names for constants and labels for addresses are converted into binary equivalent by the assembler Macros are (small programs / subroutines) a set of instructions which are used more than once in a program
Programming languages Directives and system calls are instructions to the assembler as to how it should construct the final executable machine code. They can involve directing how memory should be used or defining files or procedures that will be used. They do not have to be converted into binary code. directive is an instruction to the assembler as to how it should construct the final executable machine code. This might be to direct how memory should be used or to define files or procedures that will be used.
Symbolic, relative and absolute addressing
Addressing modes
T wo-pass assembler For any assembler there are a number of things that have to be done with the assembly language code before any translation can be done. Some examples are: • removal of comments • replacement of a macro name used in an instruction by the list of instructions that constitute the macro definition • removal and storage of directives to be acted upon later.
T wo-pass assembler A two-pass assembler is designed to handle programs written using the symbolic addressing mode . This program contains forward references. Some of the instructions have a symbolic address for the operand where the location of the address is not known at that stage of the program. A two-pass assembler is needed so that in the first pass the location of the addresses for forward references can be identified. To achieve this during the first pass the assembler uses a symbol table. The code is read line by line. When a symbolic address is met for the first time its name is entered into the symbol table. Alongside the name a corresponding address has to be added as soon as that can be identified.
T wo-pass assembler
Addressing modes T he assembler has to count the instructions as it reads the code. Then when it encounters a label it can enter the offset value into the symbol table. In this example the first entry made in the offset column is the +7 for STRPLP . For the second pass the Assembler uses the symbol table and a lookup table that contains the binary code for each opcode. This table would have an entry for every opcode in the set defined for the processor.
The result from the second pass
Addressing modes When an instruction requires a value to be loaded into a register there are different ways of identifying the value. These different ways are described as the 'addressing modes’. For our simple processor, two bits of the opcode in a machine code instruction would be used to define the addressing mode. This allows four different modes.
Addressing modes Addressing mode Operand Immediate The value to be used in the instruction LDM #7 Direct An address which holds the value to be used in the instruction LDD 125 Indirect An address which holds the address which holds the value to be used in the instruction LDI Indexed An address to which must be added what is currently in the index register (IX) to get the address which holds the value in the instruction LDX Relative Relative addressing. Move to the address n locations from the address of the current instruction. Load the contents of this address to ACC. LDV
Assembly language instructions Assembly language instructions can be divided into 4 groups Data movement Arithmetic Jump Input / Output Data movement Data movement instructions can involve loading data into a register or storing data in memory.
Data movement Instruction opcode Instruction operand Explanation LDM #n I mmediate a dd ressing l oading n to ACC LDR #n Immediate addressing l oading n to I X LDD <address> Direct addressing, loading to ACC LDI <address> Indirect addressing, loading to ACC LDX <address> Indexed addressing, loading to ACC S T O <address> Storing the contents of ACC MOV <register> Move the contents of the accumulator to the given register (IX). The mnemonic defines the instruction type including wh i ch register i s involved and, where appropriate, the addressing mode . It is important to r ead the mnemonic care f u l ly!
Arithmetic operations Instruction opcode Instruction operand Explanation ADD <address> Add the address content to the content in the ACC INC < r egister> Add 1 t o the value s t ored in t he specified register DEC <reg ister > Subtract 1 from the value s t o r ed in the specified register SUB #n Subtract the denary number n from the ACC
Comparisons and jump operations Instruction opcode Instruction operand Explanation JMP <add ress > Jump to the address specified CMP <add ress > Compare the ACC content with the address content CMP #n Compare the ACC content with n JPE <address> Jump to the address if the result of the previous comparison was TRUE JPN <address> Jump to the address if the result of the previous comparison was FALSE A program might require an unconditional jump or might only need a jump if a condition is met. The execution of the conditional jump instruction begins by checking whether or not the flag bit has been set.
Input / Output instructions For the purpose of this course we will be covering 2 input/ output instructions and they come without an operand I N Used to store in the ACC the ASCII value of a character typed at the keyboard. OUT Used to display on the screen the character for which the ASCII code is stored in the ACC.
Shift operations There are two shift instructions available: • LSL #n where the bits in the accumulator are shift ed logically n places to the left • LSR #n where the bits are shifted to the right. In a logical shift no consideration is given as to what the binary code in the accumulator represents. Because a shift operation moves a bit from the accumulator into the carry bit
Shift operations A logical shift cannot be used for multiplication or division by two when a signed integer is stored. This is because the operation may produce a result where the sign of the number has changed. As indicated earlier, only the two logical shift s are available for the simple processor considered here. However, in more complex processors there is likely to be a cyclic shift capability. Here a bit moves off one end into the carry bit then one step later moves in at the other end. All bit values in the original code are retained. Left and right arithmetic shifts are also likely to be available. These work in a similar way to logical shifts, but are provided for the multiplication or division of a signed integer by two. The sign bit is always retained following the shift.
Bitwise logic operation
Shift operations A logical shift cannot be used for multiplication or division by two when a signed integer is stored. This is because the operation may produce a result where the sign of the number has changed. As indicated earlier, only the two logical shift s are available for the simple processor considered here. However, in more complex processors there is likely to be a cyclic shift capability. Here a bit moves off one end into the carry bit then one step later moves in at the other end. All bit values in the original code are retained. Left and right arithmetic shifts are also likely to be available. These work in a similar way to logical shift s, but are provided for the multiplication or division of a signed integer by two. The sign bit is always retained following the shift.