Home | Projects | Notes > Computer Architecture & Organization > ARM Register Set
ARM has 16 programmer-visible registers and a Current Program Status Register, CPSR.
xxxxxxxxxx
81r0 - r12 : General-purpose registers
2 (r0 - r11 are completely interchangeable.
3 r12, a.k.a. scratch register, is used for Intra-Procedure-call.)
4r13 : Reserved for the programmer to use it as the stack pointer
5r14 : Link register which stores a subroutine return address
6 (This is what the BL instruction uses to store the current PC.)
7r15 : Program counter which is accessible by the programmer
8CPSR : a.k.a. CCR, holds the processor status and control information
More on Current Program Status Register (CPSR) (For more details on "condition flags setting", see Introduction to the Stored Program Machine & ARM.)
[31-28] Condition Codes
xxxxxxxxxx
41N : Negative or less than flag
2Z : Zero flag
3C : Carry or borrow or extended flag
4V : Overflow flag
[27- 9] Reserved
[ 8- 0] System Control Bits
xxxxxxxxxx
121IF : Enables or disables IRQ or FIQ interrupts
2T : Thumb mode
3Mode :
4 M[4:0] Mode ARM - Visible State Registers
5 ------ ---------- --------------------------------------------
6 b10000 User r0-r14, PC, CPSR
7 b10001 FIQ r0-r7, r8_fiq-r14_fiq, PC CPSR, SPSR_fiq
8 b10010 IRQ r0-r12, r13_irq, r14_irq, PC CPSR, SPSR_irq
9 b10011 Supervisor r0-r12, r13_svc, r14_svc, PC CPSR, SPSR_svc
10 b10111 Abort r0-r12, r13_abt, r14_abt, PC, CPSR, SPSR_abt
11 b11011 Undefined r0-r12, r13_und, r14_und, PC, CPSR, SPSR_und
12 b11111 System r0-r14, PC, CPSR
So far, we have discussed only these 17 registers that are visible to us in the user mode. In reality, however, ARM has total 32 registers and the rest become visible as you switch to the different modes.
ARM designers decided to maintain another set of registers to make context switching more efficient by reducing the push/pop of all the registers to/from the stack.
ARM has some four operand instructions. This is possible due to only having 16 registers so only 4 bits are needed to specify the register. (Out of 32 bits, 16 bits are still available for the instruction and addressing modes after using 16 bits to specify four operand registers.)
xxxxxxxxxx
91ADD r1, r2, r3 @ Three operands
2 @ RTL: [r1] ← [r2] + [r3]
3 @
4 @ At times, one of these three can be left out.
5 @ ADD r1, r2
6 @ RTL: [r1] ← [r2]
7
8MLA r1, r2, r3, r4 @ Four operands (MLA: Multiply Accumulate)
9 @ RTL: [r1] ← [r2] * [r3] + [r4]
Not all supports four operands. Refer to the "ARM Instruction Set" manual.
When a call is made to routines like printf, scanf, etc. the values in registers r0 - r3
and r12
will be used by the called routine. Whatever values you had stored there will be overwritten. So, save the register values on the stack if this wipe-out is expected.
xxxxxxxxxx
361push { /* list of registers*/ } @ Save register values on the stack
2 e.g., push {r0, r1, r2, r3}
3 push {r0-r15}
4 push {r2, r0}
5pop { /* list of registers*/ } @ Restore original values into the regs
6 e.g., pop {r0, r1, r2, r3}
7 pop {r0-r15}
8 pop {r2, r0}
9
10[!] Note: The order of the list of registers when pushing does not matter
11 since each register is mapped to the bits in the data transfer
12 instruction.
13 However, the order in pop should match that of push's.
14 Otherwise, the restored value can be mixed up.
15 (ARM assembler won't report this as an error.)
16
17 e.g., CORRECT!
18 push {r2, r3, r4}
19 pop {r2, r3, r4} @ Move top of the stack into r4 first,
20 @ then r3, then r2
21 WRONG!
22 push {r2, r3, r4}
23 pop {r3} @
24 pop {r4} @ This mixes up the resotred values!
25 pop {r2} @
26
27
28[!] Note: The push puts the data on the stack from the lowest number register
29 to the highest number register. The pop pulls them off the stack
30 from the highest number register to the lowest number.
31
32[!] Note: Be careful NOT to,
33 "push {r2, r3, r4}" then try and restore by
34 "pop {r3}, pop {r4}, pop {r2}" since this will switch values
35 between the registers.
36 You should be using "pop {r2, r3, r4}"
Depending on the type of call, sometimes other registers may be used as well. Be aware of that!
Comments on .global printf
in the following program supports this. Refresh yourself!
xxxxxxxxxx
1031@------------------------------------------------------------------------
2@ File Name : lab04.s
3@ Description : ARM assembly program to calculate factorials.
4@ Author : Kyungjae Lee
5@ UAH Email : kl0079@uah.edu
6@ Course : CS309-01 Spring 2022
7@------------------------------------------------------------------------
8
9.text
10
11.global main
12
13main:
14
15@ Prompt the user to enter a number.
16
17 ldr r0, =askForNumber
18 bl printf
19
20@ Read in the number.
21
22 ldr r0, =formatSpecifier
23 ldr r1, =intInput
24
25 bl scanf
26 ldr r1, =intInput
27 ldr r1, [r1]
28
29@ If the input value is not 1-12, terminate the program.
30
31 cmp r1, #1
32 blt myexit
33
34 cmp r1, #12
35 bgt myexit
36
37@ Confirm the user of the entered number if the number is valid.
38
39 ldr r0, =confirmNumber
40 bl printf
41
42 ldr r1, =intInput @ r1-r3 value changes when printf is invoked
43 ldr r1, [r1] @ so load the input valu again!
44
45@ Manipulate the registers to calculate factorials and print them.
46
47 mov r8, r1 @ r8 will be used to hold copy of the user input.
48 mov r4, #1 @ Initialize r4 which will be used as 1st col num.
49 mov r5, #1 @ Initialize r5 which will be used as 2nd col num.
50 mov r6, #1 @ r6 will be used to hold r5's previous value.
51
52@ Loop to calculate and print the factorials.
53
54loop:
55 cmp r4, r8 @ break if r4 gets bigger than the user input.
56 bgt myexit @ terminate the program.
57
58 mul r5, r4, r6
59
60 mov r1, r4 @ Pass r4 value to printf via r1
61 mov r2, r5 @ Pass r5 value to printf via r2
62
63 ldr r0, =calcOutput
64 bl printf @ Print the intermediate/final calc result.
65
66 mov r6, r5 @ Update r4, r5, r6 for next calculation
67 add r4, r4, #1
68 add r5, r5, #1
69
70 b loop
71
72myexit:
73 mov r7, #0x01 @ SVC call to exit
74 svc 0 @ Make the system call.
75
76.data
77
78.balign 4
79askForNumber: .asciz "Enter a number (1-12): "
80
81.balign 4
82confirmNumber: .asciz "You entered %d!\n "
83
84.balign 4
85calcOutput: .asciz "%d\t%d\n "
86
87.balign 4
88formatSpecifier: .asciz "%d"
89
90.balign 4
91intInput: .word 0
92
93.global printf
94@ printf("This is printf function, %d %d %d\n", r1, r2 ,r3)
95@ -----------------------------------
96@ Address of this whole string is stored in 'r0'
97@
98@ r0-r3 are used as arguments to printf function. When you invoke printf
99@ function these register values are likely to be changed. It is a good
100@ practice to use other registers than these for the manipulation purposes.
101
102.global scanf
103@ Similar applies to the global scanf function.
Output
xxxxxxxxxx
141Enter a number (1-12): 12
2You entered 12!
3 1 1
4 2 2
5 3 6
6 4 24
7 5 120
8 6 720
9 7 5040
10 8 40320
11 9 362880
12 10 3628800
13 11 39916800
14 12 479001600