ARM Procedure Call Standard: Registers, Stack, and Function Calls

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/215

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

216 Terms

1
New cards

ARM Procedure Call Standard

A set of guidelines for using registers when calling subroutines in ARM architecture.

2
New cards

Preserved Registers

Registers that must retain their values after a subroutine call.

3
New cards

Non-Preserved Registers

Registers that can be freely modified by a subroutine without needing to restore their original values.

4
New cards

r0

Argument 1 and return value; not preserved.

5
New cards

r1

Argument 2; not preserved.

6
New cards

r2

Argument 3; not preserved.

7
New cards

r3

Argument 4; not preserved.

8
New cards

r4

General-purpose V1; preserved.

9
New cards

r5

General-purpose V2; preserved.

10
New cards

r6

General-purpose V3; preserved.

11
New cards

r7

General-purpose V4; preserved.

12
New cards

r8

General-purpose V5; preserved.

13
New cards

r9

Platform specific/V6; usage is platform-dependent.

14
New cards

r10

General-purpose V7; preserved.

15
New cards

r11

General-purpose V8; preserved.

16
New cards

r12 (IP)

Intra-procedure-call register; not preserved.

17
New cards

r13 (SP)

Stack pointer; preserved, must remain the same after a subroutine.

18
New cards

r14 (LR)

Link register; not preserved.

19
New cards

r15 (PC)

Program counter; do not directly change.

20
New cards

Callee Saved Registers

Registers that the callee must restore if modified.

21
New cards

Caller Saved Registers

Registers that the caller must save if they need to retain their values.

22
New cards

SP

Stack pointer; must be the same after a subroutine has completed.

23
New cards

LR

Link register; does not need to contain the same value after a subroutine.

24
New cards

64 bits

If return has 64 bits, then r0:r1 hold it.

25
New cards

128 bits

If the return has 128 bits, r0-r3 hold it.

26
New cards

Leaf Function

A function that does not call any other function.

27
New cards

Non-Leaf Function

A function that calls another function.

28
New cards

Function Call Tree

All functions in a program forms a function call tree. Assume no recursive function call.

29
New cards

Assembly Code for Add2

Add2 ADD r0, r0, r1 ; r0 = a + b

30
New cards

BX LR ; return

31
New cards

C Function for Add2

int Add2(int a, int b)

32
New cards

{

33
New cards

return a + b;

34
New cards

}

35
New cards

Assembly Code Implementation

Add2 ADD r2, r0, r1 ; r2 = a+b

36
New cards

MOV r0, r2 ; just realize …

37
New cards

BX LR ; return

38
New cards

Assembly Code with r4

Add2 ADD r4, r0, r1 ; r4 = a+b

39
New cards

MOV r0, r4 ; r0 = r4

40
New cards

BX LR ; return

41
New cards

Preserved Register

If one insists to change the value of a preserved register, this is what needs to be done to make the function correct.

42
New cards

Calc Function

int Calc(int a, int b)

43
New cards

{

44
New cards

return Add2(a, b) + 10;

45
New cards

}

46
New cards

Return Address and Stack

Non-leaf function must use stack to save return address!

47
New cards

Function Return

Push the return address (in LR) on the entry, pop the return address to PC for function return.

48
New cards

Stack Usage

Calc PUSH {LR} ; save LR in stack

49
New cards

BL Add2 ; r0 = Add2(a, b)

50
New cards

ADD r0, #10 ; r0 = r0 + 10

51
New cards

POP {PC} ; restore LR

52
New cards

Leaf Function Return

Leaf function uses 'BX LR' for return.

53
New cards

Non-Leaf Function Return

Non-leaf function uses 'PUSH {LR}' and 'POP {PC}'.

54
New cards

Add3 Function

int Add3(int a, int b, int c)

55
New cards

{

56
New cards

return Add2(Add2(a, b), c);

57
New cards

}

58
New cards

Alternative Add3 Function

int Add3(int a, int b, int c)

59
New cards

{

60
New cards

int t1 = Add2(a, b);

61
New cards

int t2 = Add2(t1, c);

62
New cards

return t2;

63
New cards

}

64
New cards

Assembly Code for Add3

Add3 PUSH {LR} ; push return address

65
New cards

BL Add2 ; r0 = Add2(a, b)

66
New cards

MOV r1, r2 ; r2 = c

67
New cards

BL Add2 ; r0 = Add2(r0, c)

68
New cards

POP {PC} ; return

69
New cards

Non-Leaf Function: Preserved and Non-Preserved

Refers to functions that may or may not preserve the state of certain registers across function calls.

70
New cards

Assembly code

A low-level programming language that is closely related to machine code.

71
New cards

Add3

A function that takes three integer parameters and returns their sum using the Add2 function.

72
New cards

PUSH {LR}

An assembly instruction that saves the link register (LR) to the stack.

73
New cards

BL Add2

An assembly instruction that branches to the Add2 function and links the return address.

74
New cards

MOV r1, r2

An assembly instruction that moves the value in register r2 to register r1.

75
New cards

POP {PC}

An assembly instruction that pops the top value from the stack into the program counter (PC), effectively returning from a function.

76
New cards

Stack

A data structure that follows the Last In First Out (LIFO) principle, used for storing temporary data.

77
New cards

Non-Preserved Register

A register that may be modified during function calls and does not need to retain its value.

78
New cards

Add4

A function that takes four integer parameters and returns the sum of two Add2 function calls.

79
New cards

PUSH {r2}

An assembly instruction that saves the value of register r2 onto the stack.

80
New cards

POP {r1}

An assembly instruction that retrieves the top value from the stack into register r1.

81
New cards

ADD r0, r2, r0

An assembly instruction that adds the values in registers r2 and r0 and stores the result in r0.

82
New cards

What is different: t1 must be preserved

Indicates that the variable t1 needs to retain its value across a function call in the context of Add4.

83
New cards

What happens if those are not enough?!

A rhetorical question regarding the limitations of register availability for passing arguments.

84
New cards

sum6

A function that takes six integer arguments and returns their sum.

85
New cards

int32_t

A data type representing a 32-bit signed integer.

86
New cards

PUSH

An assembly instruction that adds values to the stack.

87
New cards

POP

An assembly instruction that removes values from the stack.

88
New cards

PC

Program Counter, a register that contains the address of the next instruction to execute.

89
New cards

MOVS

An assembly instruction that moves a value into a register and sets the condition flags.

90
New cards

ADDS

An assembly instruction that adds two values and sets the condition flags.

91
New cards

LDRD

An assembly instruction that loads two registers from memory.

92
New cards

BX

An assembly instruction that branches to the address in a register.

93
New cards

CALLING CONVENTION

A set of rules defining how functions receive parameters and return values.

94
New cards

extern

A keyword used in C to declare a function defined in another file.

95
New cards

global

A keyword used in assembly to declare a symbol that can be accessed from other files.

96
New cards

sum3

A function that takes three integer arguments and returns their sum.

97
New cards

data memory

Memory used to store data variables during program execution.

98
New cards

register

A small amount of storage available directly in the CPU for quick data access.

99
New cards

ADDS sp, sp, #8

An assembly instruction that adjusts the stack pointer to remove two values from the stack.

100
New cards

sum6(1, 2, 3, 4, 5, 6)

An example function call that passes six integer values to the sum6 function.