Skip to content

Instantly share code, notes, and snippets.

@landonf
Last active December 16, 2015 00:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landonf/14369e439bc147cce633 to your computer and use it in GitHub Desktop.
Save landonf/14369e439bc147cce633 to your computer and use it in GitHub Desktop.
_calculate_points: @ @calculate_points
@ BB#0:
push {r4, r7, lr}
add r7, sp, #4
mov r4, r0
bl _current_position_x
str r0, [r4]
bl _current_position_y
str r0, [r4, #4]
pop {r4, r7, pc}
typedef struct Point {
int x;
int y;
} Point;
int current_position_x (void) {
return 5;
}
int current_position_y (void) {
return 10;
}
void calculate_points (Point *result) {
result->x = current_position_x();
result->y = current_position_y();
}
.section __TEXT,__text,regular,pure_instructions
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
.section __TEXT,__const_coal,coalesced
.section __TEXT,__picsymbolstub4,symbol_stubs,none,16
.section __TEXT,__StaticInit,regular,pure_instructions
.syntax unified
.section __TEXT,__text,regular,pure_instructions
.globl _current_position_x
.align 2
_current_position_x: @ @current_position_x
@ BB#0:
movw r0, #5 ; Set 5 as the return value
bx lr ; Jump to the return address
.globl _current_position_y
.align 2
_current_position_y: @ @current_position_y
@ BB#0:
movw r0, #10 ; Set 10 as the return value
bx lr ; Jump to the return address
.globl _calculate_points
.align 2
_calculate_points: @ @calculate_points
@ BB#0:
push {r7, lr} ; Save r7 (this is the previous frame pointer) and lr (this is the return address)
mov r7, sp ; Save the current stack pointer. This is the frame pointer.
sub sp, sp, #4 ; Allocate 4 bytes on the stack
str r0, [sp] ; Store the pointer to 'result' in the newly allocated stack space. Remember that arguments are passed starting at r0
bl _current_position_x ; Call current_position_x()
ldr r1, [sp] ; Load the pointer to 'result' from the stack
str r0, [r1] ; Store the return value of current_position_x() at result+0x0
bl _current_position_y ; Call current_position_y()
ldr r1, [sp] ; Load the pointer to 'result' from the stack
str r0, [r1, #4] ; Store the return value of current_position_x() at result+0x4
mov sp, r7 ; Restore the saved stack frame pointer
pop {r7, pc} ; Restore the caller's frame pointer, -and- restore the caller's return address.
; We pop the lr value saved in the first instruction *into* pc, which causes a jump to the return address
.section __DATA,__objc_imageinfo,regular,no_dead_strip
L_OBJC_IMAGE_INFO:
.long 0
.long 0
.subsections_via_symbols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment