Professional Documents
Culture Documents
Chapter3 Microprocessorandmicrocontrollerprogramming
Chapter3 Microprocessorandmicrocontrollerprogramming
AND MICROCONTROLLER
Data Representation
Instruction Set
Chapter Outline
Chapter 3 – Microprocessor and Writing Assembly and C Program
Microcontroller Programming
ARM Development flow
© C. Uttraphan
Data Representation
© C. Uttraphan
Instruction Set
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
MOVT <Rd>,<#value>
R0 0 x 00001234
R1 0 x 00000000
Destination register R2 0 x 00000000
Immediate value
(16-bit) R3 0 x 00000000
Example: ·
R15 0 x 00000000
MOVT R0, #0xABCD
© C. Uttraphan
Instruction set – Moving data within the processor
MOVT <Rd>,<#value>
R0 0 x ABCD1234
R1 0 x 00000000
Destination register R2 0 x 00000000
Immediate value
(16-bit) R3 0 x 00000000
Example: ·
R15 0 x 00000000
MOVT R0, #0xABCD
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Moving data within the processor
© C. Uttraphan
Instruction set – Memory Accesses
• The ARM processors use the load and store instruction (Load and
Store architecture) to access memory instead of using the move
instructions as in CISC processors. This means data manipulations
must be done in the registers.
• For example, to add the data in two (or more) memory locations,
the data must be loaded into the register, perform the addition
process and store back into the memory.
• In this section, we will also examine the addressing modes that
can be used in order to access the memory.
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data from a register into a memory location and reading data from
memory location to a register
• ARM processors use indirect addressing mode to store the data from the
register to the memory (or load from memory to register) where the address
of the memory location that we want to store/load the data must first be
loaded into the register. This register will act as a pointer to the target memory
location.
• The instruction format:
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data from register R1 into a memory location
0x20000000 Source operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data from register R1 into a memory location
0x20000000 Source operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data from register R1 (haft word) into a memory
location 0x20000000
Source operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data from register R1 (byte) into a memory location
0x20000000
Source operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data from register R1 (byte) into a memory location
0x20000000
Source operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading data from memory location 0x20000000 into register R2
destination operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading data from memory location 0x20000000 into register R2
destination operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading half word data from memory location 0x20000000 into
register R2
destination operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading half word data from memory location 0x20000000 into
register R2
destination operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading one byte data from memory location 0x20000000 into
register R2
destination operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading one byte data from memory location 0x20000000 into
register R2
destination operand
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading/storing data using offset addressing mode
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data using offset addressing mode
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data using offset addressing mode
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading/storing data using post-indexed addressing mode
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data using post-indexed addressing mode
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data using post-indexed addressing mode
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Reading/storing data using pre-indexed addressing mode
© C. Uttraphan
Instruction set – Memory Accesses (Cont..)
Storing data using pre-indexed addressing mode
© C. Uttraphan
Instruction set – Logic and Arithmetic
• Instructions that are specifically used for setting and clearing flags, such TST or
CMP.
• Instructions that are told to set the flags by appending an “S” to the mnemonic.
For example, MOVS would perform a MOV operation and set the flags afterward,
since the S bit is set in the instruction. We can do this with all of the ALU
instructions, so we control whether or not to update the flags.
• A direct write to the Program Status Register, where you explicitly set or clear
flags
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
The ADD instruction (without suffix “S”) does not update the
flags!
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
ADDS R0, R1
R0 0 x 00000004 R0 0 x 00000006
R1 0 x 00000002 R1 0 x 00000002
before after
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
R0 0 x 00000004 R0 0 x 00000104
R1 0 x 00000002 R1 0 x 00000002
before after
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
The SUB instruction (without suffix “S”) does not update the
flags!
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
SUBS R0, R1
R0 0 x 00000004 R0 0 x 00000002
R1 0 x 00000002 R1 0 x 00000002
before after
Condition flags: N = 0, Z = 0, C = 1, V = 0
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Instruction for Multiplication
(1) MUL <Rd>, <Rn>, <Rm> - Multiply, 32-bit result
- Can be 2 or 3 operands
R0 0 x 00000000 R0 0 x FFFFFF00
R1 0 x 00000000 R1 0 x 0000007F
R2 0 x 7FFFFFFF R2 0 x 7FFFFFFF
R3 0 x 00000100 R3 0 x 00000100
before after
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Instruction for Division
(1) SDIV <Rd>, <Rn>, <Rm> - performs a signed integer
division of the value in Rn by
the value in Rm
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Instruction for Division
SDIV R0, R1
R0 0 x FFFFFFF6 R0 0 x FFFFFFFB
R1 0 x 00000002 R1 0 x 00000002
R2 0 x 00000000 R2 0 x 00000000
R3 0 x 00000000 R3 0 x 00000000
before after
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Instruction for Division
UDIV R0, R1
R0 0 x 0000000A R0 0 x 00000005
R1 0 x 00000002 R1 0 x 00000002
R2 0 x 00000000 R2 0 x 00000000
R3 0 x 00000000 R3 0 x 00000000
before after
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Logic Instructions (AND, OR, XOR)
(1) AND <Rd>, <Rm>, Op2
(2) ORR <Rd>, <Rm>, Op2
(3) EOR <Rd>, <Rm>, Op2
(4) BIC <Rd>, <Rm>, Op2
(5) ORN <Rd>, <Rm>, Op2
• The AND, EOR, and ORR instructions perform bitwise AND, Exclusive OR,
and OR operations on the values in Rm and Op2 (Can be a register or an
immediate value).
• The BIC instruction performs an AND operation on the bits in Rm with
the complements of the corresponding bits in the value of Op2.
• The ORN instruction performs an OR operation on the bits in Rm with
the complements of the corresponding bits in the value of Op2.
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Instruction for Logic operations
R0 0 x 00000000 R0 0 x 00001200
R1 0 x 00001234 R1 0 x 00001234
R2 0 x 0000FF00 R2 0 x 0000FF00
R3 0 x 00000000 R3 0 x 00000000
before after
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Instruction for Logic operations
R0 0 x 00000000 R0 0 x 00000034
R1 0 x 00001234 R1 0 x 00001234
R2 0 x 0000FF00 R2 0 x 0000FF00
R3 0 x 00000000 R3 0 x 00000000
before after
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Shift and Rotate instructions
(1) ASR <Rd>, <Rm>, Op2 - Arithmetic shift right
(2) LSL <Rd>, <Rm>, Op2 - Logical shift left
(3) LSR <Rd>, <Rm>, Op2 - Logical shift right
(4) ROR <Rd>, <Rm>, Op2 - Rotate right
(5) RRX <Rd>, <Rm> - Rotate right with extend
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Shift and Rotate instructions
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Shift and Rotate instructions
R0 0 x 00000000 R0 0 x 000000F0
R1 0 x 0000000F R1 0 x 0000000F
R2 0 x 00000000 R2 0 x 00000000
R3 0 x 00000000 R3 0 x 00000000
before after
© C. Uttraphan
Instruction set – Logic and Arithmetic (Cont..)
Shift and Rotate instructions
R0 0 x 00000000 R0 0 x FF050000
R1 0 x F050000F R1 0 x F050000F
R2 0 x 00000004 R2 0 x 00000004
R3 0 x 00000000 R3 0 x 00000000
before after
© C. Uttraphan
Program flow control
© C. Uttraphan
Program flow control
Review
© C. Uttraphan
Program flow control
© C. Uttraphan
Writing Assembly Program
Assembly directive
• Assembly directives are instructions to the assembler for creating areas of code, aligning data, marking
the end of your code, and so forth. These are unlike processor instructions, which tell the processor to
add two numbers or jump somewhere in your code, since they never turn into actual machine
instructions.
Directive Uses
AREA Defines a block of code or data
EQU Equates a symbol to a numeric constant
ENTRY Declares an entry point to the program
DCB, DCW, DCD Allocates memory and specifies initial runtime contents
ALIGN Aligns data or code to a particular memory boundary
END Designates the end of a source file
EXPORT declares a symbol that can be used by the linker to resolve
symbol references in separate object and library files.
© C. Uttraphan
Writing Assembly Program
Assembly program example
00000100 0 x 10
00000101 0 x 11
MyData DCB 0x10, 0x11, 0x12, 0x13 00000102 0 x 12
00000103 0 x 13
00000104 0 x 00
00000100 0 x 48
MyData DCB “HELLO” 00000101 0 x 45
00000102 0 x 4C
00000103 0 x 4C
00000104 0 x 4F
© C. Uttraphan
Writing Assembly Program
00000100 0 x 11
00000101 0 x 10
MyData DCW 0x1011, 0x1213 00000102 0 x 13
00000103 0 x 12
00000104 0 x 00
00000100 0 x 13
MyData DCD 0x10111213 00000101 0 x 12
00000102 0 x 11
00000103 0 x 10
00000104 0 x 00
© C. Uttraphan
Writing Assembly Program
Load the memory address of the data into a register
C = -1
© C. Uttraphan
C codes and Assembly codes
For-loop statement
for (i=1;i<10;i++) R2 holds the memory address
A[i] = 0xF0;
MOV R0,#1
C codes
MOV R1,#0xF0
loop STRB R1,[R2],#1
i=1 ADDS R0,#1
CMP R0,#10
A[i] = 0xF0 BLT loop
no A[9] 0x109
© C. Uttraphan
C codes and Assembly codes
While statement
int A = 10
MOV R0,#10
while(A != 0)
loop SUBS R0,#1
A = A - 1;
BNE loop
C codes
Assembly codes
no
A=0? A=A-1
yes
© C. Uttraphan
Example 1
• Since any real code will have something more than just arithmetic and
control instructions, the whole notion of looping through code needs to be
addressed. We need the ability to execute a section of code multiple times,
so we’re going to start off the discussion of loops by looking at a real
problem.
• Suppose that we want to move a block of data from memory address
location 0x20000000 – 0x2000001F to a memory address location
0x20000020 – 0x2000003F
© C. Uttraphan
Example 1
20000000 0 x 12 20000020 0 x 12
20000001 0 x 13 20000021 0 x 13
20000002 0 x 14 20000022 0 x 14
20000003 0 x 15 20000023 0 x 15
2000001D 0 x A1 2000003D 0 x A1
2000001E 0 x A2 2000003E 0 x A2
2000001F 0 x A3 2000003F 0 x A3
© C. Uttraphan
Example 1
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
0x20000000
0x20000020
0x20000040
0x20000060
0x20000080
© C. Uttraphan
Example 1
Flowchart
A
start
Move data word
Mem[R1] <- R2 from R2 to mem
R0 <- 0x20000000 pointer1
Increment the
Counter, to R3 <- R3 + 1 counter
R3 <- 0 count up to 8 words
yes
Update the pointer to
R0 <- R0 + 4 point to the next word
end
A
© C. Uttraphan
Example 1
Assembly codes
© C. Uttraphan
Example 2
Suppose that we had a register
start
containing a binary value that needed to
be normalized. In other words, we need
to have the leading 1 in the most no
significant bit. MSB = 1? Shift left
yes
end
Assembly codes
© C. Uttraphan
Stack and Subroutine
Stack
• Stacks are conceptually Last In-First Out (LIFO) queues that can be used to describe systems
from the architectural level down to the hardware level.
• When referring to hardware, generally these are areas in memory that have a variable length
and a fixed starting address.
• Data is written, or pushed, onto the top of the stack, and also read, or popped, from the top
of the stack, where the processor adjusts the stack pointer before or after each operation.
• ARM processors have a stack pointer, register R13, which holds the address of either the next
empty entry or the last filled entry in the queue.
© C. Uttraphan
Stack and Subroutine
Stack
© C. Uttraphan
Stack and Subroutine
Example of push and pop data to/from stack
Student are required to run the code in Keil MDK and observe the
value in registers and stack!!!
© C. Uttraphan
Stack and Subroutine
Example of push and pop data to/from stack
Initial values in registers and stack
© C. Uttraphan
Stack and Subroutine
Example of push and pop data to/from stack
PUSH {R0}
© C. Uttraphan
Stack and Subroutine
Example of push and pop data to/from stack
PUSH {R1}
© C. Uttraphan
Stack and Subroutine
Example of push and pop data to/from stack
POP {R2}
© C. Uttraphan
Stack and Subroutine
Example of push and pop data to/from stack
POP {R2}
© C. Uttraphan
Stack and Subroutine
Subroutine
• Most large programs consist of many smaller blocks of code, or subroutines, where
functions can be called at will, such as a print routine or a complicated arithmetic function
like a logarithm.
• A large task can be described more easily this way. Subroutines also allow programmers to
write and test small blocks of code first, building on the knowledge that they’ve been
proven to work.
• Subroutine is called with the BL (branch and link) instruction. This instruction transfers the
branch target (the starting address of the subroutine) into the Program Counter and also
transfers the return address into the Link Register, R14, so that the subroutine can return
back to the calling program.
• Subroutines can also call other subroutines, but caution must be taken to ensure
information is not lost in the process.
© C. Uttraphan
Stack and Subroutine
Subroutine Example
© C. Uttraphan
Stack and Subroutine
How the microprocessor executes the subroutine
Initial values in registers and stack just before execute the BL Routine1 instruction
© C. Uttraphan
Stack and Subroutine
How the microprocessor executes the subroutine
After executing the BL Routine1 instruction
© C. Uttraphan
Stack and Subroutine
How the microprocessor executes the subroutine
© C. Uttraphan
Stack and Subroutine
How the microprocessor executes the subroutine
PC = LR – 1
© C. Uttraphan
Stack and Subroutine
• In the previous example, there is only one subroutine, hence, only one return address is
needed to be kept track.
• However, if we need to call another subroutine from a subroutine, there will be two return
addresses that need to be saved. Therefore, BX instruction cannot be used in this case.
• In nested subroutine, the stack is used to store the value of LR as shown in the next
example.
© C. Uttraphan
Stack and Subroutine
__main LDR SP,=0x20000100 ;Stack address
LDR R0,=0x12345678 ;Load value in R0
LDR R1,=0xA1A2A3A4 ;Load value in R1
BL Routine1 ;Call Routine1
MOV R0,#0x10 ;Data processing1
MOV R1,#0x10
ADDS R2,R0,R1
stop B stop ;Loop forever (required in
;hardware programming)
Routine1
PUSH {LR} ;Save the values of LR
MOV R0,#0x20 ;Data processing 2
SUBS R1,R0,#0x05
BL Routine2 ;Call Routine2
POP {PC} ;Restore the values of return
;address to PC
Routine2
MOV R0,#0x30 ;Data processing 3
SUBS R0,R0,#0x05
BX LR ;Restore the values of R0, R1, and
;return to main loop
© C. Uttraphan
Exercise
© C. Uttraphan