COMP8551 Assembly Language II
COMP 8551
Advanced Games
Programming
Techniques
Assembly Language: ARM/NEON
Borna Noureddin, Ph.D.
British Columbia Institute of Technology
XCode / ARM example
•ARM = RISC
architecture
•Can use
XCode to see
assembly
code
•Can embed in
C code 2
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
XCode / ARM example
Registers
• r0 – r3: parameters passed to a function
• r4 – r11: function’s local variables
• r12: scratch register
• r13: stack pointer.
• r14: link register (address of next instruction to
execute when returning from the current
function)
• r15: program counter (address of currently
executing instruction)
3
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
XCode / ARM example
Registers
• w0 – w3: parameters passed to a function
• w4 – w11: function’s local variables
• w12: scratch register
• w13: stack pointer.
• w14: link register (address of next instruction to
execute when returning from the current
function)
• w15: program counter (address of currently
executing instruction)
4
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
XCode / ARM example
What does this simple function look like in
Assembler?
5
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
int addFunction(int a, int b) {
int c = a + b;
return c;
}
!
XCode / ARM example
6
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
_addFunction: ; @addFunction
Lfunc_begin0:
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:13:0
sub sp, sp, #16 ; =16
str w0, [sp, #12]
str w1, [sp, #8]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:14:13
ldr w0, [sp, #12]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:14:17
ldr w1, [sp, #8]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:14:15
add w0, w0, w1
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:14:9
str w0, [sp, #4]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:15:12
ldr w0, [sp, #4]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:15:5
add sp, sp, #16 ; =16
ret
XCode / ARM example
7
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
_addFunction: ; @addFunction
Lfunc_begin0:
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:13:0
sub sp, sp, #16 ; =16
str w0, [sp, #12]
str w1, [sp, #8]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:14:13
ldr w0, [sp, #12]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:14:17
ldr w1, [sp, #8]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:14:15
add w0, w0, w1
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:14:9
str w0, [sp, #4]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:15:12
ldr w0, [sp, #4]
/Users/borna/Documents/dev/ios/armneon/armneon/main.m:15:5
add sp, sp, #16 ; =16
ret
XCode / ARM example
8
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
0x1000a2768 <+0>: sub sp, sp, #0x10 ; =0x10
0x1000a276c <+4>: str w0, [sp, #0xc]
0x1000a2770 <+8>: str w1, [sp, #0x8]
0x1000a2774 <+12>: ldr w0, [sp, #0xc]
0x1000a2778 <+16>: ldr w1, [sp, #0x8]
0x1000a277c <+20>: add w0, w0, w1
0x1000a2780 <+24>: str w0, [sp, #0x4]
0x1000a2784 <+28>: ldr w0, [sp, #0x4]
0x1000a2788 <+32>: add sp, sp, #0x10 ; =0x10
0x1000a278c <+36>: ret
XCode / ARM example
What about calling the function?
9
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51__attribute__((noinline))
int addFunction(int a, int b) {
int c = a + b;
return c;
}
void fooFunction() {
int add = addFunction(12, 34);
printf(“add = %i”, add);
}
XCode / ARM example
1
0
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
0x1000ae728 <+0>: sub sp, sp, #0x20 ; =0x20
0x1000ae72c <+4>: stp x29, x30, [sp, #0x10]
0x1000ae730 <+8>: add x29, sp, #0x10 ; =0x10
0x1000ae734 <+12>: orr w0, wzr, #0xc
0x1000ae738 <+16>: mov w1, #0x22
0x1000ae73c <+20>: bl 0x1000ae700 ; addFunction at main.m:13
XCode / ARM example
1
1
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
__attribute__((noinline))
int add_two_int(int x, int y) {
int ret;
asm volatile (
“add %w[ret], %w[x], %w[y]”
// outputs
: [ret]”=r”(ret)
// inputs
: [x]”r”(x), [y]”r”(y)
);
return ret;
}
sub sp, sp, #16
str w0, [sp, #12]
str w1, [sp, #8]
ldr w0, [sp, #12]
ldr w1, [sp, #8]
; InlineAsm Start
add w0, w0, w1
; InlineAsm End
str w0, [sp, #4]
ldr w0, [sp, #4]
add sp, sp, #16
ret
XCode / ARM / NEON example
NEON for vectorization:
http://www.crickettechnology.com/blog/?p
=691
1
2
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
1
3
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
Additional Reading
http://www.crickettechnology.com/blog/?p=691
http://www.raywenderlich.com/37181/ios-assembly-tutorial
http://www.computernostalgia.net/articles/assembly.htm
http://en.wikipedia.org/wiki/Assembly_language#Current_usage
https://software.intel.com/en-us/articles/optimizing-the-rendering-pipeline-
of-animated-models-using-the-intel-streaming-simd-extensions
http://en.wikipedia.org/wiki/SIMD
1
4
©
B
or
na
N
ou
re
dd
in
C
O
M
P
85
51
E N D