Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Last active December 8, 2016 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kwpolska/9f2bd44739e81ca4bcde767cdfd3576d to your computer and use it in GitHub Desktop.
Save Kwpolska/9f2bd44739e81ca4bcde767cdfd3576d to your computer and use it in GitHub Desktop.
for loop vs while loop (C, gcc 6.2.1)
--- for.s 2016-12-08 20:00:37.121281039 +0100
+++ while.s 2016-12-08 20:00:38.705280904 +0100
@@ -1,4 +1,4 @@
- .file "for.c"
+ .file "while.c"
.intel_syntax noprefix
.text
.globl main
int main() {
int n = 1;
for (int i = 2; i < 10; ++i) {
n += i;
}
return n;
}
.file "for.c"
.intel_syntax noprefix
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
mov DWORD PTR [rbp-4], 1
mov DWORD PTR [rbp-8], 2
jmp .L2
.L3:
mov eax, DWORD PTR [rbp-8]
add DWORD PTR [rbp-4], eax
add DWORD PTR [rbp-8], 1
.L2:
cmp DWORD PTR [rbp-8], 9
jle .L3
mov eax, DWORD PTR [rbp-4]
pop rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 6.2.1 20160916 (Red Hat 6.2.1-2)"
.section .note.GNU-stack,"",@progbits
int main() {
int n = 1;
int i = 2;
while (i < 10) {
n += i;
++i;
}
return n;
}
.file "while.c"
.intel_syntax noprefix
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
mov DWORD PTR [rbp-4], 1
mov DWORD PTR [rbp-8], 2
jmp .L2
.L3:
mov eax, DWORD PTR [rbp-8]
add DWORD PTR [rbp-4], eax
add DWORD PTR [rbp-8], 1
.L2:
cmp DWORD PTR [rbp-8], 9
jle .L3
mov eax, DWORD PTR [rbp-4]
pop rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 6.2.1 20160916 (Red Hat 6.2.1-2)"
.section .note.GNU-stack,"",@progbits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment