Mov r0, r3 r0 stores the return value x from mainĪdd sp, fp, #0 restore our stack sp needed ldr fp,, #4īx lr. Sub sp, sp, #28 drop the stack pointer down by 28 bytes str fp, ! save the frame pointer 4 bytes below spĪdd fp, sp, #0 make the stack pointer = the frame pointer This starts on line 10 of the args = 0, pretend = 0, frame = 24 frame_needed = 1, uses_anonymous_args = 0 link register save eliminated. Divert your eyes and try to focus on the meat of the main C function, which is the conditional logic we created. Hopefully, in another post I’ll go over the stack and frame in detail. When looking at the assembly the stack pointer and frame pointer are a large part of any program. However, to the trained eye you can spot some places the ARM assembly code is not optimal.
The -O0 is crucial since it’s turns optimization off. This is done by simply running gcc -O0 -Wall -S if.c. We want to focus on where the variables are set, and especially where we have the if statements and variable a is compared to variable b. ARM Assembly of our if statementīelow is the ARM assembly from the C code above. The BEYONDIF label represents code outside the if statement and allows us to jump out when the if condition doesn’t hold. Mov r3, #4 the statements inside the if when true Cmp r2, r3 r2 = value of a, r3 = value of bīne BEYONDIF the branch is the opposite of =