A Level CS CH 6 9618
A Level CS CH 6 9618
Machine code - Simple instructions that are executed directly by the CPU.
As we should hopefully already know, computers can only understand binary, 1s and 0s. We are
now going to look at the simplest instructions that we can give a computer. This is called machine
code.
000000110101 =
Store 53
Assembly Language: a low-level language related to machine code where opcodes are written
as mnemonics and there is a character representation for an operand.
Assembler: a program used to translate an assembly language program into machine code.
Q: Name and explain the two parts that make up a machine code instruction:
A: opcode - the command to be executed, operand - the data or address being worked upon
Q: For a word with 4 bits for an opcode and 6 bits for an operand
Computer Science 9618 Notes Subject Teacher: Fahim Siddiq 03336581412
Q: How many different instructions could I fit into the instruction set? What is the largest number
that I could use as data?
A:
• Number of instructions:
• largest operand:
Q: For a 16 bit word with 6 bits for an opcode, How many different instructions could I fit into
the instruction set? What is the largest number that I could use as data?
A:
• Number of instructions:
• largest operand:
A: So that they can increase the number of discrete instructions that can be executed.
Q: What might be the problem with increasing the space taken up by the opcode?
A: Less space for the operand, meaning reduced range and precision in data be processed in a
single instruction.
A: More space available to increase the instruction set size and greater range and precision
available in the operand.
Addressing modes
You might notice that some instructions use a # and others don't, you might even have an inkling
as to what the difference is. Well here is the truth:
# = number
Single pass Assembler: A single pass assembler scans the program only once and creates the
equivalent binary program.
Two pass Assembler: Translate assembly code into machine code by processing it in two
passes.
What happens during the passes: In the first pass of a two-pass assembler all of these features
are either removed or acted upon. Typical actions are:
1. removal of comments
2. creation of a symbol table containing the binary codes for symbolic names and labels
3. creation of a literal table if the programmer has used constants in the program
4. expansion of macros
5. Identification of system calls and subroutines used.
If errors are not found, the second pass of the assembler generates the object code. This involves
replacing symbolic addresses with absolute addresses.
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.
Below table shows a possible format for the symbol table that would be created for the a
program.
Computer Science 9618 Notes Subject Teacher: Fahim Siddiq 03336581412
Note that the 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. Table 6.04 shows entries only for the instructions used in the simple program
we are using as an example. Note that the binary codes are just suggestions of codes that might
be used.
Provided that no errors have been identified, the output from the second pass will be a
machine code program.
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.
Logical Shift: Where bits in the accumulator are shifted to the right or to the left and zero moves
into the bit position vacated.
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 in the
status register this can be used to examine individual bits. For a left logical shift, the most
significant bit is moved to the carry bit, the remaining bits are shifted left and a zero is entered
for the least significant bit. For a right logical shift, it is the least significant bit that is moved to
the carry bit and a zero is entered for the most significant bit. If the accumulator content
represents an unsigned integer, the left shift operation is a fast way to multiply by two. However,
this only gives a correct result if the most significant bit is a zero. For an unsigned integer the
right shift represents integer division by two. For example, consider: 00110001 (denary 49) gives
if right shift ed 00011000 (denary 24)
The remainder from the division can be found in the carry bit. Again, the division will not always
give a correct result; continuing right shifts will eventually produce a zero for every bit. It should
be apparent that 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 shift s 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.
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.
Cyclic Shift: Similar to a logical shift but bits shift ed from one end reappear at the other end.
Arithmetic Shift: Uses the shift to carry out multiplication or division of a signed integer stored
in the accumulator.
Computer Science 9618 Notes Subject Teacher: Fahim Siddiq 03336581412
ACC ← [[CIR(15:0)]]
The instruction is in the CIR and only the 16-bit address needs to be examined to identify the
location of the data in memory. The contents of that location are transferred into the
accumulator.
Computer Arithmetic
In Chapter 1 we saw that computer arithmetic could lead to an incorrect answer if overflow
occurred. In Chapter 5 we saw the possible uses of the Status Register. The following worked
example illustrates how the values stored in the Status