ENGN2219/COMP6719
Computer Architecture & Simulation Ramesh Sankaranarayana Semester 1, 2020 (based on original material by Ben Swift and Uwe Zimmer)
1
ENGN2219/COMP6719 Computer Architecture & Simulation Ramesh - - PowerPoint PPT Presentation
ENGN2219/COMP6719 Computer Architecture & Simulation Ramesh Sankaranarayana Semester 1, 2020 (based on original material by Ben Swift and Uwe Zimmer) 1 Week 5: Functions 2 Royal Choral Society: 'Hallelujah Chorus' from Handel's Messiah
1
2
Royal Choral Society: 'Hallelujah Chorus' from Handel's Messiah Royal Choral Society: 'Hallelujah Chorus' from Handel's Messiah
Copy link Copy link
3
4
5
def plus_1(x) return x + 1 public String plusOne(int x) { return x + 1; } (define plus-1 (lambda (x) (+ x 1)))
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bgt)
21
@ use the type directive to tell the assembler @ that fn_name is a function (optional) .type fn_name, %function fn_name: @ just a normal label @ @ the body of the function @ bx lr @ to go back
22
23
24
25
26
27
28
this will work in this case, but there’s still a slight problem with the use of
sp here—can you spot it?
29
30
31
32
33
34
35
36
lr instruction)
37
38
int do_all_the_things(int how_many_things){ // lies! does *none* of the things return 0; }
39
40
41
Plus_1: add r0, r0, 1 bx lr Plus_1: add r5, r2, 1 bx lr
42
r0-r3 are the parameter and scratch registers r0-r1 are also the result registers r4-r11 are callee-save registers r12-r15 are special registers (ip, sp, lr, pc)
43
r0-r3 are “scratch” registers, which means that the callee can freely use
44
@ pass by value Plus_1: add r0, 1 bx lr @ pass by reference Plus_1: ldr r3, [sp] add r3, 1 str r3, [sp] bx lr
45
46
47
48
49
50
function doStuff(a, b){ let c = a+b; let d = a-b; let e = a*b; // function body here }
51
function doArrayStuff(a, b){ let person = { name: "Esmerelda", age: 54, pets: ["rex", "daisy"] }; let junk = new Array(1000); // function body here }
52
lr)
53
54
ldr sp, =_estack
/* Highest address of the user mode stack */ _estack = 0x20018000; /* end of RAM */
55
56
sp can either point to the last “used” address used (full stack) or the
57
mov r2, 0xfe @ push the value in r2 onto the stack str r2, [sp, -4] sub sp, sp, 4 @ do some stuff here @ pop the value from the "top" of the stack into r3 ldr r3, [sp] add sp, sp, 4
58
59
60
61
ldr/str with offset can write the new address (base + offset) back to the
@ r1 := r1 + 4 str r0, [r1, 4]! @ note the "!" @ r1 := r1 - 8 ldr r0, [r1], -8 @ no "!" for post-offset
62
63
64
mov r2, 0xbc @ push str r2, [sp, -4]! @ do stuff... @pop ldr r3, [sp], 4
65
mov r2, 0xfe @ gives same result as `str r2, [sp, -4]!` push {r2} @ do stuff... @ gives same result as `ldr r3, [sp], 4` pop {r3}
66
}) syntax around the register name
@ push r0, r1, r2, r9 to stack, decrement sp by 4*4=16 push {r0-r2,r9} @ pop 4 words from the stack into r0, r1, r2, r9 pop {r0-r2,r9}
67
68
ldmdb load multiple, decrement before ldmia load multiple, increment after stmdb store multiple, decrement before stmia store multiple, increment after
69
70
71
72
.type my_func, %function @ assume three parameters in r0-r2 my_func: @ prologue push {r0-r2} @ sp decreases by 12 push {lr} @ sp decreases by 4 @ body: do stuff, leave "return value" in r3 @ epilogue mov r0, r3 @ leave return value in the right place pop {lr} @ sp increases by 4 add sp, sp, 12 @ balance out the initial "push" bx lr
73
74
push {r0,lr} bl middle_fn pop {r0,lr} bx lr middle_fn: push {r0,lr} bl inner_fn pop {r0,lr} bx lr inner_fn: @ do inner function stuff bx lr
75
76
77
78
79
80
81
@ number passed in r0, result in r1 factorial: push {r0,lr} cmp r0, #0 bne recurse @ what's the potential problem here? mov r1, #1 jmp end recurse: sub r0, #1 bl factorial add r0, #1 mul r1, r0 end: pop {r0,lr} bx lr
82
83
84
85
86
87
88
89
90
91
92
93
.macro macro_name arg_a arg_b ... @ to use the argument, prefix with "\" @ e.g. adds r0, \arg_a, \arg_b @ ... .endm
94
@ swap the values in two registers @ assumes r12 is free to use as a "scratch" register .macro swap reg_a reg_b mov r12, \reg_a mov \reg_a, \reg_b mov \reg_b, r12 .endm
95
swap r0, r3 mov r12, r0 mov r0, r3 mov r3, r12
96
97
if: @ set flags here b<c> then @ else b rest_of_program then: @ instruction(s) here rest_of_program: @ continue on...
98
.macro if condition_code condition then_code else_code \condition_code b\condition then \else_code b end_if then: \then_code end_if: .endm @ usage if "cmp r1, r2", eq, "mov r3, 1", "mov r3, 0"
99
b\condition becomes e.g. beq or blt
r1, r2" as the condition_code parameter) as long as they’re surrounded
100
.macro if condition_code condition then_code else_code \condition_code b\condition then\@ \else_code b end_if\@ then\@: \then_code end_if\@: .endm
101
.macro for register from to body mov \register, \from for\@: cmp \register, \to bgt end_for\@ \body add \register, 1 b for\@ end_for\@: .endm @ usage for r1, 1, 100 "add r3, r1"
102
103
labelname)
104
<func_name>, .-<func_name> directives
105
106
107
.macro as directive docs
108
testandbranch destination_label register_to_be_tested condition testandbranch NonZero, r0, NE CMP r0, #0 BNE NonZero
109
110
111
112