CHAPTER 4 : ASSEMBLY LANGUAGE
PROGRAMMING
Subtopic
4.1 Addressing mode and program flow
4.2 intermediate, direct and Indirect addressing
4.3 Conditional and unconditional branch
4.4 direct jumps and calls
4.5 Return from subroutines
4.6 Interrupts
4.1 Addressing mode and program flow
3
Addressing Modes
• The term 'addressing mode' refers to the mechanism
which will use to specify the operands. An operand
can be specified as the part of the instruction, or the
reference of the memory location which had given.
• An operand could also be an address of CPU register.
Addressing modes can also be defined by the
following criteria which are as follows:
4
• Addressing modes shows the way where exactly the required object is present.
• In this condition, object may be an instruction or data.
• The output of the addressing mode is Effective Address [EA], where EA is the
actual address where exactly the object is present in the computer.
• There is a different convention which is used to indicate the various addressing
modes some of them follow here:
– #/I ------- Immediate addressing mode.
– Reg.Name -------Register addressing mode.
– [ ] ------------Direct addressing mode.
– @/ ( ) ----------Indirect addressing mode.
– Index Reg Name ------Indexed addressing mode.
– +/- -------------- Auto indexed addressing mode.
Classification of Addressing Modes
According to specify the operand in different ways, the
addressing modes are classified in two ways that are:
• Sequential control flows addressing mode which is
focused on data.
• Transfer of control flow addressing mode which is
focused on instruction.
1) Sequential Control Flow Addressing Mode
These modes are concentrating on the location of data
in a computer system. Basically, data is present in the
computer in two possible places that are register and
memory. Therefore these modes are further divided
into two kinds:
• Register based addressing mode
• Memory based addressing mode
2) Transfer of control flow Addressing Mode
These modes are concentrating on the location of instruction on the
computer. Two different AM are employed under this category to
indicate the instruction address that is:
• PC Relative Addressing Mode
• Base Register Addressing Mode
These AM are associated with the help of the transfer of control
instruction. So during the execution of these instructions, the EA
(Effective Address) is calculated. Then the calculated EA is placed into
the PC so that in the next fetch cycle CPU reads the instruction from
the target location.
4.2 Intermediate, direct and Indirect addressing
10
8051 Addressing Modes
• The CPU can access data in various ways, which are called
addressing modes
i. Immediate
ii. Register
iii. Direct
iv. Register indirect
v. External Direct
11
i. Immediate Addressing Mode
• The source operand is a constant.
• The immediate data must be preceded by the pound or hatch sign, “#”
• Can load information into any registers, including 16-bit DPTR register
– DPTR can also be accessed as two 8-bit registers, the high byte DPH and low byte DPL
12
ii. Register Addressing Mode
• Use registers to hold the data to be manipulated.
• The source and destination registers must match in size.
MOV DPTR,A will give an error
• The movement of data between Rn registers is not allowed
MOV R4,R7 is invalid
13
iii. Direct Addressing Mode
• It is most often used the direct addressing mode to access RAM locations 30 – 7FH.
• The entire 128 bytes of RAM can be accessed.
• Contrast this with immediate addressing mode, there is no “#” sign in the operand.
14
iv. Register Indirect Addressing Mode
• A register is used as a pointer to the data.
• Only register R0 and R1 are used for this purpose.
• R2 – R7 cannot be used to hold the address of an operand located in
RAM.
• When R0 and R1 hold the addresses of RAM locations, they must be
preceded by the “@” sign.
15
v. External Direct
• External Memory is accessed.
• There are only two commands that use External Direct
addressing mode:
– MOVX A, @DPTR
MOVX @DPTR, A
• DPTR must first be loaded with the address of external
memory.
16
4.3 Conditional and unconditional branch
17
Conditional Branch
In this example, there are only two
branches. The first branch, which
selects a loan offer from United Loan,
is executed if a case condition
containing an XPath Boolean
expression is met. Otherwise, the
second branch, which selects the Star
Loan loan offer, is executed.
(IF-Else statement, Nested, Switch Statement)
Unconditional Branch
The goto statement changes the normal sequence of
the program execution by transferring control to other
part of the program. It is usually not used because C is
completely structured language and most probably, the
break and continue statements are used.
(Go To, Switch, Loop, Continue Statement)
4.4 direct jumps and calls
20
MOV Instruction
• MOV destination, source ; copy source to destination.
• MOV A,#55H ;load value 55H into reg. A
MOV R0,A ;copy contents of A into R0
;(now A=R0=55H)
MOV R1,A ;copy contents of A into R1
;(now A=R0=R1=55H)
MOV R2,A ;copy contents of A into R2
;(now A=R0=R1=R2=55H)
MOV R3,#95H ;load value 95H into R3
;(now R3=95H)
MOV A,R3 ;copy contents of R3 into A
;now A=R3=95H
21
ADD Instruction
• ADD A, source ;ADD the source operand to the
accumulator
• MOV A, #25H ;load 25H into A
MOV R2,#34H ;load 34H into R2
ADD A,R2 ;add R2 to accumulator
;(A = A + R2) 22
Structure of Assembly Language
ORG 0H ;start (origin) at location 0
MOV R5,#25H ;load 25H into R5
MOV R7,#34H ;load 34H into R7
MOV A,#0 ;load 0 into A
ADD A,R5 ;add contents of R5 to A
;now A = A + R5
ADD A,R7 ;add contents of R7 to A
;now A = A + R7
ADD A,#12H ;add to A value 12H
;now A = A + 12H
HERE: SJMP HERE ;stay in this loop
END ;end of asm source file
23
Multiplication of Unsigned Numbers
MUL AB ; A B, place 16-bit result in B and A
MOV A,#25H ;load 25H to reg. A
MOV B,#65H ;load 65H in reg. B
MUL AB ;25H * 65H = E99 where B = 0EH and A =
99H
Multiplication can be performed done exactly as with
decimal numbers, except that you have only two digits
(0 and 1).
24
Division of Unsigned Numbers
DIV AB ; divide A by B
• MOV A,#95H ;load 95 into A
• MOV B,#10H ;load 10 into B
• DIV AB ;now A = 09 (quotient) and B = 05 (remainder)
Unsigned Integers are just like integers (whole numbers) but
have the property that they don't have a + or - sign associated
with them. Thus they are always non-negative (zero or positive).
25
Unconditional Jump Instructions
• In an unconditional jump, the jump is
always taken. The jump instruction tells
the CPU to execute code at a different
memory address. This is similar to the
goto statement in the C programming
language.
26
Call Instructions
The CALL instruction interrupts the flow of a program by passing
control to an internal or external subroutine. An internal subroutine is
part of the calling program. An external subroutine is another
program.
• LCALL (long call): 3-byte instruction
– 2-byte address
– Target address within 64K-byte range
• ACALL (absolute call): 2-byte instruction
– 11-bit address
– Target address within 2K-byte range
27
4.5 Return from subroutines
28
Subroutine basics
• The basic form of a subroutine is:
sub [optional_return_type] subroutine_name [ ( optional_argument_type1
optional_argument_name1, ... ) ] begin [statements] [return statement if returning value] end;
• For any subroutine that returns a value, the final line of code
in the subroutine must be a return statement that specifies a
value of the type the subroutine is supposed to return.
4.6 Interrupts
30
The 8051 features two main types of interrupts, i.e. Hardware
interrupts and software interrupts.
The hardware interrupts are triggered by external signal such
as peripheral events or external devices.
The microcontroller can be configured to respond to specific
events, allowing for efficient event-driven programming.
Programming using interrupts is a technique based on
an automatic mechanism in the hardware of the
microcontroller, which allows a device to provide
service to internal or external devices, only at the
moment it is required.
Based on the figures above, when
the interrupt signal is generated,
the microcontroller finishes first
execution of the instruction that is
currently being processed, then
stores on the stack memory the
address of the next instruction
code and later calls the "interrupt
service subroutine",