Title: Assembly Language
1Assembly Language
- The inputs to a microprocessor to form its
program are termed instructions and the set of
instructions that a microprocessor recognizes is
termed its instruction set.
2Assembly to Binary Code
- Writing instruction in binary code is a tedious
and difficult task. - Instructions can be written in a mnemonic form
termed assembly language and then translated into
machine code by a computer program called an
assembler.
3Instructions
- Each instruction must contain two parts
- Operation - The code must detail the operation
required, i.e. opcode. - Operand - The operation will require some data to
operate on and its destination after processing
they are known as the operands. - In principle all instructions require two
operands, one to define the source of data prior
to processing and one to define the destination
for the processed data
4Instruction ..
- For example, in assembly language an instruction
might be LD A,BThe opcode used is LD to
specify the operation is to load the operand in
register B to accumulator A.
5Instruction ..
- LD A,B
- LD - Operation code for load
- A Destination
- B - Source
6Instructions ..
- Another example of an instruction isJMP
2000Hwhich has the operation jump with the
destination being address 2000H
7Operations
- In general, instructions can be classified as
falling into four main groups1. Data
Transfer2. Arithmetic3. Logical4. Program
Control
8Data Transfer
- Load/MoveThis instruction reads the contents of
a specified memory location and copies it to a
specified register location, e.g. Before
instructionData in memory location 0010After
instructionData still in memory location
0010Data from 0010 in accumulator
9Data Transfer ..
- Store This instruction copies the current
contents of a specified register into a specified
memory location, e.g.Before instructionData
in accumulatorAfter instructionData still in
accumulatorData copied to memory location 0010
10Arithmetic
- ADDThis instruction adds the contents of a
specified memory location to the data in some
register, e.g.Before instructionAccumulator
with data 0001Memory location with data
0010After instructionAccumulator with data
0011
11Arithmetic
- SUBTRACTThis instruction subtracts the content
of a specified memory location from data in some
register. - DECREMENTThis instruction subtracts 1 from the
contents of a specified memory location
(accumulator)
12Arithmetic
- INCREMENTThis instructions adds 1 to the
contents of a specified location - COMPAREThis instruction indicates whether the
contents of a register are greater than, less
than or the same as the contents of a specified
memory location. The result appears in the status
register as flags. If the two are equal we can
have the zero flag set to 1 and the carry flag to
0.If greater the zero flag and the carry flag
are both 0If less than the zero flag is 0 and
the carry flag is 1
13Logical
- ANDThis instruction carries out the logical AND
operation bit by bit with the contents of a
specified memory location and the data in some
register, e.g.Before instructionAccumulator
with data 0011Memory location with data
1001After instructionAccumulator with data
0001
14AND ..
- One of the main uses of the AND instruction is to
force bits in the accumulator to become 0,
e.g.If accumulator content 1010 1111 the ANDing
this with mask data 1111 0000 gives the result
1010 0000 . Every 0 in the mask data has forced
logic 0s into the accumulator. (Note Use 1111
1101 used to switch certain bit 2 to 0, while
all the other bits remain)
15OR
- Like the AND operation the OR operation can be
used for bit masking. E.g.If we have 0011 1000
in the accumulator then ORing it with 1111 0000
results in 1111 1000. Whenever a logic 1 appears
in the mask data then the corresponding bit in
the accumulator becomes 1 a logic 0 in the mask
data does not change the data in the accumulator.
16EXCLUSIVE-OR
- The Exclusive-OR function gives a 1 if either of
the inputs is 1 bit it gives 1 0 if both are
1.If we have 0110 0101 in the accumulator and
XOR it with 1111 0000 then the result is 1001
0101. A logic 1 in the mask data will invert a
bit in the accumulator, but a logic 0 will not
change the data.
17Logical Shift (Left or Right)
- Logical shift instructions involve moving the
pattern of bits in the register one place to the
left or right by moving a 0 into the LSB of the
number and the overflow bit into the carry.For
example logical shift rightBefore
instructionAccumulator with data 0011After
instructionAccumulator with data 0001Status
register indicates Carry 1
18Arithmetic Shift (left or right)
- Arithmetic shift instructions involve moving the
pattern of bits in the register one place to the
left or right but they preserve the sign bit at
the left end of the number. The overflow goes
into the carryFor example Arithmetic shift
rightBefore instructionAccumulator with data
1011After instructionAccumulator with data
1001Status register indicates Carry 1
19Arithmetic Shift Left
- For example Arithmetic shift leftBefore
instructionAccumulator with data 1011After
instructionAccumulator with data 1110Status
register indicates Carry 0
20Rotate (Left or Right)
- Rotate instructions involve moving the pattern of
bits in the register one place to the left or
right through the carry the bit that spills out
is written back into the other end, e.g. for a
rotate right Before instructionAccumulator
with data 0011After instructionAccumulator
with data 1001
21Program Control
- JUMPThis instruction changes the sequence in
which the program steps are carried out.
Normally, the program counter causes the program
to be carried out sequentially, one instruction
after another, in the strict numerical sequence
in which the instructions are written. However,
the jump instruction causes the program counter
to jump to some other specified location in the
program.
22Program Control
- BRANCHThis is a conditional instruction which
might be branch if zero or branch if plus. This
branch instruction is followed if the right
conditions occur. - ENDThis instruction stops all further
microprocessor activity.
23Program Control
- NO OPERATIONThis instruction does nothing except
use an instruction cycle and increment the
program counter. It can therefore be used to
provide short delays.
24Numerical Values
- Numerical data may be binary, octal, hex or
decimal. - Generally in the absence of any indicator the
assembler assumes the number is
decimal.Intelnumerical values must be
preceded by to indicate a number and B for
binary, O or Q for octal, H or h for hex and D or
nothing for decimal.
25Numerical Values ..
- Motorola/RockwellA number is indicated by the
prefix A binary number is preceded by or
followed by B An octal number is preceded by _at_
or followed by O A hex number is preceded by
or followed by H.
26Addressing
- When a mnemonic, such as LD, is used to specify
an instruction it will be followed by additional
information to specify the source and destination
of the data required by the instruction. - There are several different methods that are used
for specifying data location, i.e. addressing - Different microprocessors have different
addressing modes.
27Addressing
- The Motorola 68HC11 has the six addressing modes
of immediate, direct, extended, indexed, inherent
and relative. - The Intel 8051 has the five modes of immediate,
direct, register, indirect and indexed. - The PIC microcontroller has the three modes of
immediate, direct and indirect and indirect mode
allowing indexing.
28Immediate Addressing
- The data immediately following the mnemonic is
the value to be operated on. - This type of instruction with immediate
addressing is used with the loading of a
predetermined value into a register or memory
location, e.g.Z80 CodeLD A, 25Hmeans
load the A register with the number 25, the H
indicating that it is a hex number
29Immediate Addressing ..
- Motorola/Rockwell CodeLDA B 25means load
the number 25 into accumulator B. The signifies
immediate mode and a number, the that the
number is in hexadecimal - Intel CodeMOV A,25Hmeans move the number 25
to the accumulator A. The indicates a number
and the H indicates a hex number.
30Immediate Addressing ..
- PIC Codemovlw H25to load the number 25
into the working register w, the H indicating it
is a hex number.
31Direct Addressing
- Also called absolute, extended or zero-page
addressing. - With this form of addressing the data byte that
follows the opcode directly gives an address that
defines the location of the data to be used in
the instruction. For example - Z80 codeLD A,(0400H)means load the
accumulator with the data at address 0400
32Direct Addressing ..
- Motorola codeLDAA 25means load the
accumulator with the contents of memory location
0025, the 00 is assumed. - Rockwell codeLDA 25
- Intel codeMOV A, 20H
- PIC codemovwf Reg1to copy the contents of
Reg1 into the working register, the address of
Reg1 having been previously defined.
33Inherent Addressing
- Also called Implied addressing
- With this mode of addressing, the address is
implied in the instruction. For
exampleZ80/Motorola/Intel codeCLR Ameans
clear accumulator A.PIC codeclrwmeans clear
the working register.
34Register Addressing
- With this form of addressing, the operand is
specified as the contents of one of the internal
registers. For exampleZ80 codeADD A,Bto
add register B to the accumulator.Intel
codeADD R7,Ato add the contents of the
accumulator to register R7.
35Indirect Addressing
- This form of addressing means that the data is to
be found in a memory location whose address is
given by the instruction. For exampleZ80
codeLD A,(HL)we might first load the HL
register pair with the address of the location of
data then use LD A,(HL) to load the accumulator
with the data found in the memory location given
by HL register pair.
36Index Addressing
- Indexed addressing means that the data is in a
memory location whose address is held in an index
register. The first byte of the instruction
contains the opcode and the second byte contains
the offset the offset is added to the contents
of the index register to determine the address of
the operand.
37Index Addressing ..
- For example(Motorola code)LDA A FF, Xthis
means load accumulator A the data at the address
given by adding the content of the index register
and FF.
38Relative Addressing
- This is used with branch instructions. The opcode
is followed with a byte called the relative
address. This indicates the displacement in
address that has to be added to the program
counter if the branch occurs. For
example,(Motorola code)BEQ F1indicates if
the data is equal to zero then the next address
in the program is F1 further on. The relative
address of F1 is added to the address of the next
instruction.
39Separate Addressing Mode
- Source and destination operands may each have
their own separate addressing modes and so an
instruction might involve two different
addressing modes. For example,(Z80 code)ADD
A,(HL)uses indirect addressing to specify the
data source and register addressing to specify
the location of the data to which the source data
must be added.
40Separate Addressing Mode ..
- (Intel code)MOV A, 7EHhere the data source
is specified using immediate addressing and
register addressing to specify where the data is
to be moved.Note number in Intel
41Motorola Addressing Modes
- Load accumulator A with data F0LDAA F0
(Immediate) - Load accumulator A with data at address 0050LDAA
50 (Direct) - Load accumulator with data at the address given
by the index register plus CFLDAA CF, X
(Indexed) - Add the hexadecimal value 16 to the
accumulatorADDA 16 (Immediate)
42Motorola Addressing Modes ..
- Branches to the address indicated by the label
THERE if equal, i.e. the Z bit in the CCR
register is 1BEQ THERE (Relative) - Clear accumulator ACLR (Inherent)
- Clear the address given by the index register
plus 10, i.e. store all 0s at that addressCLR
10, X (Indexed)
43Examples of 8051 Assembly Codes
- Load accumulator A with the data contained in
memory address 22HMOV A, 22H - Add the contents of register 5 to accumulator
AADD A, R5 - Subtract hex 30 from the accumulatorSUBB A,
30 - Set the carry bit to 0CLR C
44Examples of 8051 Assembly Codes
- Increment register R2INC R2
- Logical AND the contents of accumulator with the
data at address 25HANL A, 25H - Change al the 0s to 1s and all the 1s to 0s in
the accumulator, i.e. obtain the complementCPL
A - Jump to address 0200 if the accumulator is not
zeroJNZ 0200H
458051 Instruction Sequence
- Example 1 Add the hexadecimal numbers 1234 and
4142 and store the result in register R6 and
R7.This requires the low byte of the sum to be
added, with no carry, and the result stored in
R6. Then the high byte is added is added, with
any carry set by the first addition, and the
result stored in register R7.
468051 Instruction Sequence ..
- MOV A, 34H move 34H into accumulator
- ADD A, 42H add 42H, with no carry to A
- MOV R6, A store result in R6
- MOV A, 12H move 12H into accumulator
- ADDC A, 41H add 41H, with carry to A
- MOV R7, A store result in R7
- END
47More 8051 Instruction Sequence
- Example 2 Subtract the number stored at address
22H from the number stored at address 23H and
stored the result at address 24HSolutionThe
accumulator is where the 8051 accumulates the
results of arithmetic operation. Thus we have to
copy data to the accumulator before we can carry
out the arithmetic. We must clear carry flag
since this acts as the borrow bit
48Solution for Example 2
- MOV A, 22H move 22H into accumulator
- CLC C clear the carry flag
- SUBB A, 23H subtract with borrow
- MOV 24H, A move result to 24H
- END
49Interpreting Instruction Codes
- MOV A, direct
- Description Move direct byte to A
- Byte 2
- Hex Code 74 (Decimal 116)
- Mnemonic MOV
- Operands A, direct
- Operation/Explanation (A) ? data
- Encoding 0 1 1 1 0 1 0 0 (Decimal 116)
50Interpreting Instruction Codes ..
- MOV Rn, A
- Description Move Accumulator to Register
- Byte 1
- Hex Code F8 to FF
- Mnemonic MOV
- Operands Rn, A
- Operation/Explanation (Rn) ? (A)
- Encoding 1 1 1 1 1 r r r
51Interpreting Instruction Codes ..
- ADDC A, direct
- Description Add with carry from direct to A
- Byte 2
- Hex Code 35 (decimal53)
- Mnemonic ADDC
- Operands A, direct
- Operation/Explanation (A) ? (direct) (C) (A)
- Encoding 0 0 1 1 0 1 0 1 direct address