Title: CEG2400 Microcomputer Systems
1CEG2400 - Microcomputer Systems
- Lecture 3 ARM Assembly Language and usage
2Part 1Introduction
- Learn how to read a program
- Directives
- Subroutine calls and stack
- Reference 1 ARM system-on-chip architecture by
Steven Furben 2nd ed. Pearson
3Position of assembly language
assembler
assembly language e.g. Mov, ldr
Object code 1001011001101.. 0101011110001.. No
specific location
compilation
High-level language C or C etc e.g. If, then
else
linker
Executable Machine code 1001011001101.. 010101111
0001.. With specific location
4Tools
- Compiler translates a high level language such as
C or C into assembly language - Assembler turns assembly language into object
code - Allows programmer to use mnemonics, symbolic
labels, error checking, pseudo-instructions etc - Object code not directly executable as it may
call other object code in different files and/or
libraries - Linker turns object code into an executable,
assigning addresses for jumps, code/data areas etc
5Power up / reset (graphical)
example
Reset/ Power up
ARM MCU
- Assembly programming
- After power up the assembly program from location
0 will be executed - First instruction (4 bytes ) is goto reset
address
Program memory
Goto Reset address
6Power up / reset (text) Reset Code starting from
location 0
- AREA RESET, CODE, READONLY
- ARM
- Vectors LDR PC, Reset_Addr load PC reset
addr. - LDR PC, Undef_Addr
-
- DCDDefine Constant Data (same as DCW)
- Reset_Addr DCD Reset_Handler
-
- EXPORT Reset_Handler
- Reset_Handler
- initialise everything here
7Assembly Programming T1.s
- The following is a simple example which
illustrates some of the core constituents of an
ARM assembler module Lr (link reg.)R14,
PC(progarm counter)R15
label
comment
opcode
operands
8General Layout of an Assembly Program
- The general form of lines in an assembler module
is - label opcode operands comment
- e.g.
- start MOV r0,15 put 15 into reg. r0
9Some rules
- Each field must be separated by one or more
ltwhitespacegt (such as a space or a tab). - Actual instructions never start in the first
column, since they must be preceded by
whitespace, even if there is no label. - All three sections are optional and the assembler
will also accept blank lines to improve the
clarity of the code.
10Description of Module T1.s
- The main routine of the program (labelled start )
loads the values 15 and 20 into registers 0 and
1. - It calls the subroutine firstfunc by using a
branch with link instruction (BL branch address).
BL copies the address of the next instruction
into r14 (lrlink register), then fills PCbranch
address (go to branch address). - The subroutine adds together the two parameters
it has received and places the result back into
r0. - It then returns by simply restoring the program
counter to the address which was stored in the
link register (r14) on entry. - Upon return from the subroutine, the main program
simply terminates using software interrupt (SWI)
11. This instructs the program to exit cleanly
and return control to the debugger.
11Directives
- They are not instructions but help the computer
to assemble the program - E.g.
- AREA, ENTRY END Assembly Directives
12AREA, ENTRY END Assembly Directives
- Directives are instructions to the assembler
program, NOT to the microprocessors - AREA Directive - specifies chunks of data or code
that are manipulated by the linker. - A complete application will consist of one or
more areas. The example above consists of a
single area which contains code and is marked as
being read-only. A single CODE area is the
minimum required to produce an application. - ENTRY Directive - marks the first instruction to
be executed within an application - An application can contain only a single entry
point and so in a multi-source-module
application, only a single module will contain an
ENTRY directive. - END directive - marks the end of the module
13How to write a better assembly program?
- i) Use subroutines
- ii) Use nested Subroutines
14(i) Subroutines
- Subroutines allow you to modularize your code so
that they are more reusable. - The general structure of a subroutine in a
program is
15Subroutine (con't)
- BL subroutine_name (Branch-and-Link) is the
instruction to jump to subroutine. It performs
the following operations - 1) It saves the PC value (which points to the
next instruction) in r14. This holds the return
address. - 2) It loads PC with the address of the
subroutine. This effective performs a branch. - BL always uses r14 to store the return address.
r14 is called the link register. - Return from subroutine is simple - just put r14
back into PC (r15).
16ARM Registers
32-bit
17(ii) Nested Subroutines
- Since the return address is held in register r14,
you should not call a further subroutine without
first saving r14. - It is also a good software engineering practice
that a subroutine does not change any register
values except when passing results back to the
calling program. - This is the principle of information hiding try
to hide what the subroutine does from the calling
program. - How do you achieve these two goals? Use a stack
to - Preserve r14
- Save, then retrieve, the values of registers used
inside subroutine
18Preserve data inside subroutine with STACK
- Stack (Pointed by r13) for subroutine
- BL stores return address at link reg. r14
Main program
Subroutine sub1
BL stores return address at link reg. r14
Using r0-r2 e.g. r0value1
push r0-r2,r14 into stack
BL sub1
You may use r0-r2 freely here e.g. r0value2
r0-r2 values are preserved not changed by
subroutine i.e. r0value1
pop r0-r2,r14 into stack Mov r14 into PC will
enable it to go back to the main program.
r0value1
19Details of how to preserve data inside a
subroutine using STACK
BL SUB1 .. SUB1 STMED r13!, r0-r2, r14
push work link registers .
stmedstore multiple empty descending BL SUB2
jump to a nested subroutine LDMED r13!,
r0-r2, r14 pop work link
registers MOV pc, r14 return to calling
program
20STMEDstore multiple empty descending
- Memory Address descending and pointing at an
empty space
21Question 3.1
Push stack (save registers)
Pop stack (restore registers)
- If SP (r13) is 00340080H before executing
STMEDr13!, r0-r2,r14 - Where are r0,r1,r2,r14 stored, and what is r13
after STMEDr13!, r0-r2,r14 is executed? - What is the value of SP(r13) after LDMED
r13!,r0-r2,r14 is executed?
22Subroutine nesting
Sub1 e.g. Average n numbers
main
Sub2 e.g. Add function
23Effect of subroutine nestingafter sub2 return to
main, SP should remain the same
- SUB1 calls another subroutine SUB2. Assuming
that SUB2 also saves its link register (r14) and
its working registers on the stack, a snap-shot
of the stack will look like-
main
High address Low address
Sub1
Sub2
24ASCII table
Columns MSB 4-bit (nibble)
Rows LSB 4-bit (nibble)
E.g. 41Hex A 6BHex k
25send only ascii numbers to screen for
displayHexOut ,e.g.output hex 1F230E2Ah to
screen 1
Subroutine HexOut - Output 32-bit word as 8 hex
numbers as ASCII characters Input
parameters r1 contains the 32-bit word (8 hex
numbers) to output Return parameters none 1
HexOut STMFD r13!, r0-r2, r14 save working
registers on stack 2 MOV r2, 8 r2 has nibble
(4-bit digit) , loop 8 tines 3 Loop MOV r0, r1,
LSR 28 get top nibble by shifting right 28
bits 4 CMP r0, 9 if nibble lt 9,
then 5 ADDLE r0, r0, "0" convert to ASCII
numeric char 030h 6 ADDGT r0, r0, "A"-10
else convert to ASCII, A-1041h-1037h 7 BL Writ
eC print character 8 MOV r1, r1, LSL 4
shift left 4 bits to get to next
nibble 9 SUBS r2, r2, 1 decrement nibble
count 10 BNE Loop if more, do next
nibble 11 LDMFD r13!, r0-r2, pc retrieve
working registers from stack 12 and
return to calling program 13 END
26Details
- Line 3, R11F230E2Ah, so R000000001h
- For the first digit 1h
- At line 4, 1h is less than 9, so line 5 is
executed - ADDLE r0, r0, "0 "030h
- R01h30h31h character 1 in ASCII
- For the second digit F
- Fh15 is more than 9, so line 6 is executed,
r0Fh37h46h character F in ASCII
27Part 2Assembly and hardware --Interfacing is
about connecting the computer to the outside world
- e.g. serial interface--UART
28Serial interface (universal asynchronous
receiver transmitter UART)
- RS232 standard and application, e.g.
RS232 standard 3 wires 10 V1 -10V0
RS232 port (UART)
RS232 port (UART)
29universal asynchronous receiver transmitter UART
- RS232 is a serial communication standard
- Since it is asynchronous, no clock is needed,
only 3 wires are needed for the simplest RS232
connection (GND, tx, rx)
Logic 1 Logic 0
30Hello world example
- Manual (http//www.nxp.com/acrobat_download/userma
nuals/UM_LPC21XX_LPC22XX_2.pdf) - Application Note http//www.nxp.com/acrobat_downlo
ad/applicationnotes/AN10369_1.pdf - from course/tutorial web page
- astartup.s
- ahello.s
31UART
32UART Hardware Interface
- All that is needed is a MAX232 chip which
translates the 3.3V NXP levels to the /-10V
RS232 levels (VCC5V)
33ARM system support for UART Memory Map
- Software interface involves appropriately
programming registers in the memory map to
manipulate the hardware - i.e. from
- 0xE000 C000
34Like office for mail delivery
- UART Office address from floor
- 0xE000 C000 to
- 0xE000 C030
Memory address space
35Remind you thatARM has
- 32-bit memory addresses (0x0000 0000 to 0xFFFF
FFFF)
36UART Register address map 0xE000 C000 to 0xE000
C030
37UART
38Define Uart Registers in assembly (in lpc21xx.h
generated by uvision3 of the Keil assembler tool)
- UART0 registers
- U0RBR EQU 0xE000C000
- U0THR EQU 0xE000C000
- U0IER EQU 0xE000C004
- U0IIR EQU 0xE000C008
- U0FCR EQU 0xE000C008
- U0LCR EQU 0xE000C00C
- U0LSR EQU 0xE000C014
- U0SCR EQU 0xE000C01C
- U0DLL EQU 0xE000C000
- U0DLM EQU 0xE000C004
- PINSEL0 EQU 0xE002C000
39EQU Pseudo instruction
- X EQU 2
- This is an assembler directive that is used to
give a value to a label name. In this example it
assigns X the value 2. Thus when X is used
elsewhere in the code, the value 2 will be
substituted (similar to using a define to set up
a constant in C).
40UART
- Initialize the UART Hardware
- Set data into UART registers
UART registers 0xE000 C000 to 0xE000 C030
41Init uartprogram segment
step4
- iuart0
- MOV R1, 0x5
- LDR R0, PINSEL0
- STR R1, R0
- MOV R1, 0x7
- LDR R0, U0FCR
- STRB R1,R0
- MOV R1, 0x83
- LDR R0, U0LCR
- STRB R1,R0
-
- MOV R1, 0x0f
- LDR R0, U0DLL
- STRB R1,R0
-
step1
step2
step3
What do all of these values do?
42Software to initialize the UART in the LPC21xx
chipset
- Step 1
- configure pin function select reg. i.e. pin
19TXD - Step 2
- configure FIFO control reg.
- Step 3
- configure line control reg. (start, stop bits)
- Step 4
- Configure baud rate
43Step1 pin configuration0x05
- Configure the pin for serial interfaces
- Arm7 pins are multi functions, depending on
initialization. - E.g. configure pin-symbol p0.0 (pin19) to be
the serial transmission pin TXD - You must set Bit 10 of address 0xE002 C00001.
- Exercise how to configure RXD? (01)
- How to fill 0xE002 C000 ? (05)
44PINSEL0--pin function select reg. (0XE002 C000)
45Init uartprogram segment
step4
- iuart0
- MOV R1, 0x5
- LDR R0, PINSEL0
- STR R1, R0
- MOV R1, 0x7
- LDR R0, U0FCR
- STRB R1,R0
- MOV R1, 0x83
- LDR R0, U0LCR
- STRB R1,R0
-
- MOV R1, 0x0f
- LDR R0, U0DLL
- STRB R1,R0
-
step1
step2
step3
What do all of these values do?
46Step2U0FCR--FIFO control set reg0x07
47Step3U0LCRline control set reg0x83
48Step4U0DLLset baud rate0x0F
- U0DLL Fpclk / (16 Baudrate)
- If we want 57600 baud (Fpclk13.824MHz) then
U0DLL150x0f (U0DLMU0DLL) - Check datasheet of how it should be set.
49Important interface conceptHandshaking
50basic concept of sending a character using the
serial port
- Sending procedure is slower than your program,
need to ask permission to send - Writec()
Sending rate (Baud rate) is slower than your
program can gen. data True even for usb2
receiver
Send-buffer
Is transmitter Buffer empty ?
no
yes
Send data
51Read buffer status U0LSR(0xE000 C014)
52U0LSRline status at U0LSR(0xE000 C014) (if
bit6TEMT (transmitter empty)1 you can send next
data
Bit7 Bit0
53Write character in R0 subroutinemake sure
TEMT(bit6)1 before write
- writec
- LDR R1, U0LSR (line status reg)
- LDRB R1, R1 get line status
- TST R1, 0x40 TEMT0?Z1
- BEQ writec if Z1,loop back wait
- LDR R1, U0THR (transmit reg)
- STRB R0, R1 send out
- BX R14 return from subroutine
54Summary
- Studied serial interface in ARM
- Studied handshaking in interfacing
55Appendix Answer of Question 3.1
Push stack (save registers)
Pop stack (restore registers)
- If SP (r13) is 00340080H before executing
STMEDr13!, r0-r2,r14. - Where are r0,r1,r2,r14 stored, and what is r13
after STMEDr13!, r0-r2,r14 is executed? - Ans
- r14 is at 00340080H,
- r2 is at 00340080H-4 00340087cH
- r1 is at 00340080H-4-4 003400878H
- r0 is at 00340080H-4-4-4 003400874H
- R13 is at 00340080H-4-4-4-4 003400870H
- What is the value of SP(r13) after LDMED
r13!,r0-r2,r14 is executed? - Ans SP 003400870H