Local Attacks

From ICO wiki
Revision as of 16:49, 9 January 2016 by Lphanvan (talk | contribs) (Created page with "int power(long base, long exponent) { int counter; int result = 1; for (counter = 0; counter < exponent; counter++) result *= base; return result; }...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

int power(long base, long exponent) {

   int counter;
   int result = 1;
   for (counter = 0; counter < exponent; counter++)
       result *= base;
   return result;

}


/* When processor enters the function body the arguments are already placed in registers r0=5 (base), r1=3 (exponent) */

cmp r1, #0 /* Compare exponent to 0 */ mov r2, #1 /* Place constant 1 in register r2,

                                    this corresponds to result = 1 in C code */

ble .L2 /* Exponent was not less than 0, so no jump to L2 mov r3, #0 /* Place constant 0 in register r3,

                                    this corresponds to variable counter */

add r3, r3, #1 /* Perform r3 = 0 + 1 which results in 1 being stored to r3

                                    this corresponds to first invocation of counter++ in C code */

cmp r3, r1 /* Compare counter (1 in this case) to exponent (3), this will be used by bne instruction below */ mul r2, r0, r2 /* Perform r2 = r0 * r2 which results in 1 * 5 = 5 being placed in r2

                                    this corresponds to first invocation of result *= base in C code */

bne .L3 /* The comparison resulted in counter being not equal to exponent, so we jump back to L3

                                    this corresponds to first invocation of counter < exponent in C code */

add r3, r3, #1 /* Perform r3 = 1 + 1 which results in 2 being stored to r3

                                    this corresponds to second invocation of counter++ in C code */

cmp r3, r1 /* Compare counter (2 in this case) to exponent (3), this will be used by bne instruction below */ mul r2, r0, r2 /* Perform r2 = r0 * r2 which results 5 * 5 = 25 being placed in r2

                                    this corresponds to second invocation of result *= base in C code */

bne .L3 /* The comparison resulted in counter being not equal to exponent, so we jump back to L3

                                    this corresponds to second invocation of counter < exponent in C code */

add r3, r3, #1 /* Perform r3 = 2 + 1 which results in 3 being stored to r3

                                    this corresponds to third invocation of counter++ in C code */

cmp r3, r1 /* Compare counter (3 in this case) to exponent (3), this will be used by bne instruction below */ mul r2, r0, r2 /* Perform r2 = r0 * r2 which results 25 * 5 = 125 being placed in r2

                                    this corresponds to third invocation of result *= base in C code */

bne .L3 /* The comparison resulted in counter being equal to exponent, so we DO NOT jump back to L3 */

mov r0, r2 /* Copy register r2 contents (125) to register r0 */ bx lr /* Jump back to caller */ /* Function returns with 125 placed in r0 this is where caller function should expect the return value */ /* The other registers will still hold whatever values were left there: r1 = 3, r2 = 125, r3 = 3 */