64 lines
1.2 KiB
C
64 lines
1.2 KiB
C
|
#include<inttypes.h>
|
|||
|
|
|||
|
/*
|
|||
|
PUSH EBP
|
|||
|
MOV EBP, ESP
|
|||
|
SUB ESP , 4 ; reserve 4 bytes in the stack frame -> local variable int32_t -> i
|
|||
|
MOV DWORD PTR [ EBP − 4 ] , 1 ; initialize local variable with 1
|
|||
|
|
|||
|
; EBP+8 : first parameter -> a
|
|||
|
; EBP+12 : second parameter -> b
|
|||
|
|
|||
|
loop:
|
|||
|
CMP DWORD PTR [ EPB+ 8 ] , 99 ; while (a >= 99)
|
|||
|
JL SHORT exit
|
|||
|
LEA EAX, [ EBP+12 ] ; eax = *b;
|
|||
|
DEC DWORD PTR [EAX] ; *b--;
|
|||
|
CMP DWORD PTR [ EBP+ 12 ] , 99 ; if (b >= 99)
|
|||
|
JL SHORT continue
|
|||
|
JMP SHORT exit ; break
|
|||
|
|
|||
|
continue:
|
|||
|
MOV EDX, [ EBP+8] ; edx = a
|
|||
|
LEA EAX, [ EBP − 4] ; eax = *i
|
|||
|
ADD DWORD PTR [EAX] , EDX ; *eax += edx -> i += a
|
|||
|
INC DWORD PTR [ EBP+8] ; a++;
|
|||
|
JMP SHORT loop ; loop
|
|||
|
|
|||
|
exit:
|
|||
|
MOV EAX, DWORD PTR [ EBP − 4] ; return i
|
|||
|
MOV ESP , EBP ; cleanup
|
|||
|
POP EBP ; cleanup
|
|||
|
RETN
|
|||
|
|
|||
|
*/
|
|||
|
|
|||
|
int32_t f(int32_t a, int32_t b) {
|
|||
|
int32_t i = 1;
|
|||
|
b--;
|
|||
|
while (a >= 99 && b < 99) {
|
|||
|
i += a;
|
|||
|
a++;
|
|||
|
b--;
|
|||
|
}
|
|||
|
return i;
|
|||
|
}
|
|||
|
|
|||
|
/* int32_t f(int32_t a, int32_t b) { */
|
|||
|
/* int32_t i = 1; */
|
|||
|
/* while (a >= 99) { */
|
|||
|
/* b--; */
|
|||
|
/* if (b < 99) { */
|
|||
|
/* i += a; */
|
|||
|
/* a++; */
|
|||
|
/* } else { */
|
|||
|
/* break; */
|
|||
|
/* } */
|
|||
|
/* } */
|
|||
|
/* return i; */
|
|||
|
/* } */
|
|||
|
|
|||
|
int main(void) {
|
|||
|
f(1,2);
|
|||
|
}
|