You are on page 1of 22

Embedded Systems Design – 4

(ICE – NITT)

ARM7 – Assembly Language Programming

Dr. N. Mathivanan
ARM Development Tools
• Development tools include:
IDE, Assembler, Compiler, Debugger, Simulator, JTAG Debug Probe
(hardware), Development board (hardware).

• Open source development tools:


IDE: Eclipse IDE
Compiler: GCC Compiler for ARM
Debugger: GNU Debugger
Simulator: Insight Debugger

Dr. N. Mathivanan
• Proprietary tools:
o IAR Workbench for ARM:
 Toolchain includes IDE, Compiler, Debugger, Simulator)
 Evaluation / Kickstart version available for free download
 Provides IAR PowerPac RTOS for ARM
o Keil for ARM
 Toolchain - mVision IDE, Compiler (armcc), Debugger, Simulator
 Debugger accurately simulates on-chip peripherals (I2C, UART,……)
 Evaluation version available for free download
 Provides RTX RTOS for ARM

• Proprietary RTOS
o VxWorks from Windriver, Threadx from Express Logic

• Free Open Source RTOS


o mclinux, freeRTOS
Dr. N. Mathivanan
C Language vs. Assembly Language

Dr. N. Mathivanan
• Assembler
o Translates ALPs (source codes) to MLPs (machine codes)
o Supports assembler directives, pseudo instructions, macros
o Features:
 Allows to assign names to mem locations variables, I/O
devices, subroutines
 Converts data & address between various number systems
 Performs some arithmetic as part of assembly process
 Inform loader to load program & data at desired part of mem

• Keil MDK mVision IDE


o Supports coding, compiling, linking, testing of ALPs

Dr. N. Mathivanan
Getting Started with Keil mVision
• Launch Keil mVision
• Select new project, enter project file name
• Select legacy Target ARM7 (little endian)
• Open new source code file with name ‘filename.s’
• Enter source code program, save, and add to project
• From Project menu build target files
• Verify ‘Build Output’ window for ‘filename.axf’ – 0 error, 0 warning
• If ‘error’ is not 0, make corrections, rebuild till ‘error’ is 0
• Start ‘debug’ session
• Keep Register, Disassembly, Program and Memory Windows visible
• Execute program in steps using Step-in or run using Run debug menu
• Verify changes in registers and memory locations while stepping thro
Dr. N. Mathivanan
Keil mVision – Debug Screen

Dr. N. Mathivanan
• General format of a source code line in ALP
{label} {opcode|directive|pseudo instr {operand}}{;comment}

Label:
Represents address of mem loc. holding instruction or data
Represents program-relative, reg. relative or absolute address
Should start at first column of the line.

Opcode, directive, pseudo instruction:


Should not start on first column
Opcode – mnemonics defined for an instruction by ARM
Directives – Not coded, assigns mem loc., defines symbols, etc.
Pseudo instrs. – translated to combinations of AL instructions

Operands: regs, nos., labels, strings, expressions


Comment: begins with ‘;’, assembler ignores comments
Dr. N. Mathivanan
• Examples of ALP source code lines
value1 DCD 0x0000FFFF ;label, dir, data
AREA C1, CODE, READONLY ;dir its attributes

Dr. N. Mathivanan
ARM7 ALP Programming - Problems
1. Split a two-digit hexadecimal into two high and low order nibbles
and store them in a 16-bit variable. Store the low order nibble into
bottom byte and high order nibble into upper byte of the variable.
2. Write ALP to find larger of two 16-bit numbers.
3. Write ALP to convert an 8-digit hexadecimal number into ASCII
string.
4. Write a program to get a seven segment binary code for a decimal
number (0-9) using a look-up table.
5. Write a program to rearrange the numbers in descending order by
bubble sort
6. Write a program to move a null terminated string from one part of
memory to another part (move bytes of a block).
Dr. N. Mathivanan
Solutions
Problem – 1 : Program to split a two-digit hexadecimal number into two
nibbles and to place them at upper and bottom bytes of a 16-bit
variable.
Listing:
AREA myprogram1, CODE, READONLY
ENTRY ; first instruction to execute
start
LDRB r1,num ; load value to be split
LDRB r2,mask ; get mask to divide into nibbles
MOV r3,r1,LSR #04 ; separate high order nibble
MOV r3,r3,LSL #08 ; bring high order nibble to upper byte
AND r1,r1,r2 ; separate low order nibble
ADD r1,r1,r3 ; place the low order nibble in bottom byte
STRH r1,resul ; store result
here B here ; stay in infinite loop
stop
AREA mydata10, DATA, READWRITE
num DCB 0x5B ; number to split
mask DCB 0x000F ; mask to separate nibbles
resul DCW 0x0000 ; space to store result
END Dr. N. Mathivanan
Problem-2: Write assembly language program to find larger of two
numbers.
Source Code Listing:
AREA myprogram2, CODE, READWRITE
ENTRY
start
LDRH r1,num1 ; get first number into r1 reg.
LDRH r2,num2 ; get second number into r2 reg.
CMP r1,r2 ; compare the two numbers
BHI large ; if r1 large, branch to large
MOV r1,r2 ; else bring large into r1
large STRH r1,result ; save large at result location
here B here ; stay in infinite loop
stop
AREA mydata2, DATA, READWRITE
num1 DCW 0x1234 ; first number
num2 DCW 0x5678 ; second number
result DCW 0x0000 ; large number
END
Dr. N. Mathivanan
Problem-3: Write a program to convert a 32-bit hexadecimal number into ASCII string.
Source Code Listing:
AREA myprogram3, CODE, READWRITE
ENTRY
mask EQU 0x0000000F
start
LDR r1,digi ; get the digit
MOV r4,#8 ; initialize counter
MOV r5,#28 ; control right shift
LDR r6,=result ; initialize result pointer
digi2str
MOV r3,r1 ; copy the digit
MOV r3,r3, LSR r5 ; right shift correct no. of bits
SUB r5,r5,#4 ; reduce the bits shifted
AND r3,r3,#mask ; mask out all but bottom nibble
CMP r3,0x0A ; is the number < 10D
BLT add0 ; then branch
ADD r3,r3,#(‘A’-‘0’-0x0A) ; add offset for ‘A’ to ‘F’
add0 ADD r3,r3,#‘0’ ; convert to ASCII
STR r3,[r6],#04 ;
SUBS r4,r4,#1 ;
BNE digi2str ;
MOV r3,#0x0D ; add ‘CR’ character
STRB r3,[r6],#4 ;
here B here ; Dr. N. Mathivanan
AREA mydata12, DATA, READWRITE
digi DCD 0xABCDEFAB
result DCB 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
END

Dr. N. Mathivanan
Problem-4: Program to find a seven segment binary code for a decimal number
(0-9) using a look-up table.
Source code Listing:
AREA myprogram, CODE, READWRITE
ENTRY
start
LDR r0,=table ; starting address of table into r0 register
LDRB r2,num ; get number into r2 register
CMP r2,#09 ; check validity, i.e. is r2 < 9?
BHI here ; if not, terminate
ADD r0,r0,r2 ; point r0 to appropriate code
LDRB r1,[r0] ; get code
STRB r1,bincode ; store result
here B here
stop
MOV r0,#0x18 ; termination
LDR r1,=0x20026
SVC #0x123456
Dr. N. Mathivanan
AREA mydata, DATA, READWRITE
table DCB 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F,0x6F ; table of codes for numbers ‘0’ to ‘9’
num DCB 0x05 ; number to which code is to be determined
bincodeDCB 0x00 ; store the result
END

Dr. N. Mathivanan
Problem-5: Program to rearrange numbers in descending order by
bubble sort
Listing:
AREA myprogram, CODE, READWRITE
ENTRY
start
LDR r6,=array ; start address of array in r6
LDRB r0,[r6] ; length of array in r0
MOV r8,r6 ; array pointer – r8
sort
ADD r7,r6,r0 ; get address of last element
MOVS r1,#0 ; zero flag for change ?
ADD r8,r8,#1 ; move 1 byte up the list on each iteration
next
LDRB r2,[r7],#-1 ; get the first byte
LDRB r3,[r7] ; get the second byte
CMP r2,r3 ; compare them
BCC noswap ; branch if r2 ? r3

Dr. N. Mathivanan
STRB r2,[r7],#1 ; otherwise swap bytes
STRB r3,[r7] ; flag as changes made
ADD r1,r1,#1 ;
SUB r7,r7,#1 ; decrement address to check
noswap
CMP r7,r8 ; checked all bytes?
BHI next ; if not, do inner loop
CMP r1,#0 ; did all changes made?
BNE sort ; if so, check outer loop
here B here
stop
MOV r0,#0x18 ; termination
LDR r1,=0x20026
SVC #0x123456

AREA mydata, DATA, READWRITE


array DCB 6
DCB 0x2A,0x5B,0x60,0x3F,0xD1,0x19
END
Dr. N. Mathivanan
Problem-6: Program to move a null terminated string from one part of memory to another
part (move bytes of a block).
Listing:
AREA myprogram, CODE, READWRITE
ENTRY
start
LDR r1,=srcstr ; base address of source
LDR r0,=desstr ; base address of destination
BL strcpy ; call string copy function
here B here
stop
MOV r0,#0x18 ; termination
LDR r1,=0x20026
SVC #0x123456
strcpy
LDRB r2,[r1],#1 ; get a character and update address
STRB r2,[r0],#1 ; copy the character and update the address
CMP r2,#0 ; check for end of string
BNE strcpy ; keep copying till end of string
BX lr ; return
Dr. N. Mathivanan
AREA mydata, DATA, READWRITE
srcstr DCB "Source String",0
desstr DCB "Destination String", 0
END

Dr. N. Mathivanan
ALP Programming – More Problems
1. Compute S = 1 + 2 + 3 + …………… + n.
2. Add, subtract two 64-bit numbers.
3. Find the smaller of two numbers
4. Get the factorial of a number from a factorial look-up table.
5. Compute sum of square of two numbers and place the result in a
memory location.

6. Count the number of negative elements, positive elements and


zeros in an array of signed words.

7. Find the length of a carriage return / NULL terminated string.


8. Add even / odd parity to a series of characters.
9. Compare two counted strings for equality.
10. Truncate decimal string to integer string.
Dr. N. Mathivanan
11. Convert a hexadecimal string to ASCII string and vice-versa

12. Add two two-digit packed BCD numbers and store three-digit
packed result.

13. Divide a 32-bit binary number by a 16-bit binary number and store
the result.

14. Rearrange the numbers ascending order using bubble sort

Dr. N. Mathivanan

You might also like