forked from AllenDowney/ExercisesInC
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d5598f
commit a835bec
Showing
1 changed file
with
224 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,230 @@ | ||
#include <stdio.h> | ||
|
||
void printSomething() { | ||
puts("Something"); | ||
} | ||
|
||
int main() { | ||
int x = 5; | ||
int y = x + 1; | ||
printf(y); | ||
char b[10]; | ||
char a = b[100]; | ||
printf("%i\n", a); | ||
// int x = 5; | ||
// int y = x + 1; | ||
// printf("Hello, World! %i\n", y); | ||
// printSomething(); | ||
// | ||
// int a = 3; | ||
// int b = 4; | ||
// int c = ab() + b; | ||
// printf("c is %d\n", c); | ||
return 0; | ||
} | ||
|
||
% probably takes up less memory and makes it so that we need fewer operations | ||
/* | ||
* 1. This is the output I received: | ||
* .file "hello.c" | ||
.section .rodata | ||
.LC0: | ||
.string "Hello, World!" | ||
.text | ||
.globl main | ||
.type main, @function | ||
main: | ||
.LFB0: | ||
.cfi_startproc | ||
pushq %rbp | ||
.cfi_def_cfa_offset 16 | ||
.cfi_offset 6, -16 | ||
movq %rsp, %rbp | ||
.cfi_def_cfa_register 6 | ||
subq $16, %rsp | ||
movl $5, -4(%rbp) <---- Here is where I think the code that corresponds to this line is | ||
leaq .LC0(%rip), %rdi | ||
call puts@PLT | ||
movl $0, %eax | ||
leave | ||
.cfi_def_cfa 7, 8 | ||
ret | ||
.cfi_endproc | ||
.LFE0: | ||
.size main, .-main | ||
.ident "GCC: (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406" | ||
.section .note.GNU-stack,"",@progbits | ||
* | ||
* | ||
* ---------------------------------------------------------------------------- | ||
* | ||
* | ||
* 2. This is the output I received with the optimization flag -O2: | ||
* .file "hello.c" | ||
.section .rodata.str1.1,"aMS",@progbits,1 | ||
.LC0: | ||
.string "Hello, World!" | ||
.section .text.startup,"ax",@progbits | ||
.p2align 4,,15 | ||
.globl main | ||
.type main, @function | ||
main: | ||
.LFB23: | ||
.cfi_startproc | ||
leaq .LC0(%rip), %rdi | ||
subq $8, %rsp | ||
.cfi_def_cfa_offset 16 | ||
call puts@PLT | ||
xorl %eax, %eax | ||
addq $8, %rsp | ||
.cfi_def_cfa_offset 8 | ||
ret | ||
.cfi_endproc | ||
.LFE23: | ||
.size main, .-main | ||
.ident "GCC: (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406" | ||
.section .note.GNU-stack,"",@progbits | ||
* | ||
* It looks like the line disappeared (probably because I wasn't using the value of x at all). | ||
* | ||
* ---------------------------------------------------------------------------- | ||
* | ||
* | ||
* 3. These are the outputs I received: | ||
* Printing X, not optimized: | ||
* .file "hello.c" | ||
.section .rodata | ||
.LC0: | ||
.string "Hello, World! %i\n" | ||
.text | ||
.globl main | ||
.type main, @function | ||
main: | ||
.LFB0: | ||
.cfi_startproc | ||
pushq %rbp | ||
.cfi_def_cfa_offset 16 | ||
.cfi_offset 6, -16 | ||
movq %rsp, %rbp | ||
.cfi_def_cfa_register 6 | ||
subq $16, %rsp | ||
movl $5, -4(%rbp) | ||
movl -4(%rbp), %eax | ||
movl %eax, %esi | ||
leaq .LC0(%rip), %rdi | ||
movl $0, %eax | ||
call printf@PLT | ||
movl $0, %eax | ||
leave | ||
.cfi_def_cfa 7, 8 | ||
ret | ||
.cfi_endproc | ||
.LFE0: | ||
.size main, .-main | ||
.ident "GCC: (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406" | ||
.section .note.GNU-stack,"",@progbits | ||
* | ||
* Printing X, optimized: | ||
* .file "hello.c" | ||
.section .rodata.str1.1,"aMS",@progbits,1 | ||
.LC0: | ||
.string "Hello, World! %i\n" | ||
.section .text.startup,"ax",@progbits | ||
.p2align 4,,15 | ||
.globl main | ||
.type main, @function | ||
main: | ||
.LFB23: | ||
.cfi_startproc | ||
leaq .LC0(%rip), %rsi | ||
subq $8, %rsp | ||
.cfi_def_cfa_offset 16 | ||
movl $5, %edx | ||
movl $1, %edi | ||
xorl %eax, %eax | ||
call __printf_chk@PLT | ||
xorl %eax, %eax | ||
addq $8, %rsp | ||
.cfi_def_cfa_offset 8 | ||
ret | ||
.cfi_endproc | ||
.LFE23: | ||
.size main, .-main | ||
.ident "GCC: (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406" | ||
.section .note.GNU-stack,"",@progbits | ||
* | ||
* The 5 is back because I'm using it now in the print statement! | ||
* | ||
* | ||
* ---------------------------------------------------------------------------- | ||
* | ||
* | ||
* 4. Here are the outputs I received: | ||
* Without optimization: | ||
* | ||
* .file "hello.c" | ||
.section .rodata | ||
.LC0: | ||
.string "Hello, World! %i\n" | ||
.text | ||
.globl main | ||
.type main, @function | ||
main: | ||
.LFB0: | ||
.cfi_startproc | ||
pushq %rbp | ||
.cfi_def_cfa_offset 16 | ||
.cfi_offset 6, -16 | ||
movq %rsp, %rbp | ||
.cfi_def_cfa_register 6 | ||
subq $16, %rsp | ||
movl $5, -8(%rbp) | ||
movl -8(%rbp), %eax | ||
addl $1, %eax | ||
movl %eax, -4(%rbp) | ||
movl -4(%rbp), %eax | ||
movl %eax, %esi | ||
leaq .LC0(%rip), %rdi | ||
movl $0, %eax | ||
call printf@PLT | ||
movl $0, %eax | ||
leave | ||
.cfi_def_cfa 7, 8 | ||
ret | ||
.cfi_endproc | ||
.LFE0: | ||
.size main, .-main | ||
.ident "GCC: (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406" | ||
.section .note.GNU-stack,"",@progbits | ||
* | ||
* | ||
* With optimization: | ||
* | ||
* .file "hello.c" | ||
.section .rodata.str1.1,"aMS",@progbits,1 | ||
.LC0: | ||
.string "Hello, World! %i\n" | ||
.section .text.startup,"ax",@progbits | ||
.p2align 4,,15 | ||
.globl main | ||
.type main, @function | ||
main: | ||
.LFB23: | ||
.cfi_startproc | ||
leaq .LC0(%rip), %rsi | ||
subq $8, %rsp | ||
.cfi_def_cfa_offset 16 | ||
movl $6, %edx | ||
movl $1, %edi | ||
xorl %eax, %eax | ||
call __printf_chk@PLT | ||
xorl %eax, %eax | ||
addq $8, %rsp | ||
.cfi_def_cfa_offset 8 | ||
ret | ||
.cfi_endproc | ||
.LFE23: | ||
.size main, .-main | ||
.ident "GCC: (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406" | ||
.section .note.GNU-stack,"",@progbits | ||
* | ||
* The value of X and Y are shown without optimization, but the value of X disappears with optimization. | ||
* | ||
* Optimization must remove unnecessary values in some way or another; if the value of X isn't printed later in the code, for example, it just disappears from the assembly language output. | ||
*/ |