Home | Projects | Notes > Computer Architecture & Organization > ARM Register Set

ARM has 16 programmer-visible registers and a Current Program Status Register, CPSR.
xxxxxxxxxx81r0 - r12 : General-purpose registers2 (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 pointer5r14 : Link register which stores a subroutine return address6 (This is what the BL instruction uses to store the current PC.)7r15 : Program counter which is accessible by the programmer8CPSR : a.k.a. CCR, holds the processor status and control informationMore 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
xxxxxxxxxx41N : Negative or less than flag2Z : Zero flag3C : Carry or borrow or extended flag4V : Overflow flag[27- 9] Reserved
[ 8- 0] System Control Bits
xxxxxxxxxx121IF : Enables or disables IRQ or FIQ interrupts2T : Thumb mode3Mode :4 M[4:0] Mode ARM - Visible State Registers5 ------ ---------- --------------------------------------------6 b10000 User r0-r14, PC, CPSR7 b10001 FIQ r0-r7, r8_fiq-r14_fiq, PC CPSR, SPSR_fiq8 b10010 IRQ r0-r12, r13_irq, r14_irq, PC CPSR, SPSR_irq9 b10011 Supervisor r0-r12, r13_svc, r14_svc, PC CPSR, SPSR_svc10 b10111 Abort r0-r12, r13_abt, r14_abt, PC, CPSR, SPSR_abt11 b11011 Undefined r0-r12, r13_und, r14_und, PC, CPSR, SPSR_und12 b11111 System r0-r14, PC, CPSRSo 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.)
xxxxxxxxxx91ADD r1, r2, r3 @ Three operands2 @ RTL: [r1] ← [r2] + [r3]3 @4 @ At times, one of these three can be left out.5 @ ADD r1, r26 @ 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.
xxxxxxxxxx361push { /* list of registers*/ } @ Save register values on the stack2 e.g., push {r0, r1, r2, r3}3 push {r0-r15}4 push {r2, r0}5pop { /* list of registers*/ } @ Restore original values into the regs6 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 r221 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 values35 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!
xxxxxxxxxx1031@------------------------------------------------------------------------2@ File Name : lab04.s3@ Description : ARM assembly program to calculate factorials.4@ Author : Kyungjae Lee5@ UAH Email : kl0079@uah.edu6@ Course : CS309-01 Spring 20227@------------------------------------------------------------------------8
9.text10
11.global main12
13main:14
15@ Prompt the user to enter a number.16
17 ldr r0, =askForNumber18 bl printf19
20@ Read in the number.21
22 ldr r0, =formatSpecifier23 ldr r1, =intInput24
25 bl scanf26 ldr r1, =intInput27 ldr r1, [r1]28
29@ If the input value is not 1-12, terminate the program.30
31 cmp r1, #132 blt myexit33
34 cmp r1, #1235 bgt myexit36
37@ Confirm the user of the entered number if the number is valid.38
39 ldr r0, =confirmNumber40 bl printf41
42 ldr r1, =intInput @ r1-r3 value changes when printf is invoked43 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, r659
60 mov r1, r4 @ Pass r4 value to printf via r161 mov r2, r5 @ Pass r5 value to printf via r262
63 ldr r0, =calcOutput64 bl printf @ Print the intermediate/final calc result.65
66 mov r6, r5 @ Update r4, r5, r6 for next calculation67 add r4, r4, #168 add r5, r5, #169
70 b loop71
72myexit:73 mov r7, #0x01 @ SVC call to exit74 svc 0 @ Make the system call.75
76.data77
78.balign 479askForNumber: .asciz "Enter a number (1-12): "80
81.balign 482confirmNumber: .asciz "You entered %d!\n "83
84.balign 485calcOutput: .asciz "%d\t%d\n "86
87.balign 488formatSpecifier: .asciz "%d"89
90.balign 491intInput: .word 092
93.global printf94@ 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 printf99@ function these register values are likely to be changed. It is a good100@ practice to use other registers than these for the manipulation purposes.101
102.global scanf103@ Similar applies to the global scanf function.Output
xxxxxxxxxx141Enter a number (1-12): 122You entered 12!3 1 14 2 25 3 66 4 247 5 1208 6 7209 7 504010 8 4032011 9 36288012 10 362880013 11 3991680014 12 479001600