Permalink
Browse files

-just doing my monthly commit of all this shit

  • Loading branch information...
granthaack committed Dec 7, 2017
1 parent e86deb4 commit 41a8bae705b778cfb84d67f77d7234870cdf8d1a
View
@@ -14,8 +14,14 @@ echo -e "${RED}Creating object files for foo1.c, foo2.c, foo3.c...${NC}"
gcc -c foo1.c foo2.c foo3.c
echo -e "${RED}Linking foo1.o, foo2.o, foo3.o...${NC}"
gcc -o foo foo1.o foo2.o foo3.o
echo -e "${RED}Listing nm output for foo1.o${NC}"
nm foo1.o
echo -e "${RED}Listing nm output for foo2.o${NC}"
nm foo2.o
echo -e "${RED}Listing nm output for foo3.o${NC}"
nm foo3.o
echo -e "${RED}Listing nm output for foo${NC}"
nm foo
echo -e "${RED}T means a symbol is in the text section of memory. This means the instructions are of fixed size and read only. U means the symbol is undefined${NC}"
View
@@ -0,0 +1,65 @@
Assembly code for int main()
0x000005a0 <+0>: lea 0x4(%esp),%ecx
0x000005a4 <+4>: and $0xfffffff0,%esp
0x000005a7 <+7>: pushl -0x4(%ecx)
0x000005aa <+10>: push %ebp
0x000005ab <+11>: mov %esp,%ebp
0x000005ad <+13>: push %ebx
0x000005ae <+14>: push %ecx
0x000005af <+15>: sub $0x10,%esp
0x000005b2 <+18>: call 0x470 <__x86.get_pc_thunk.bx>
0x000005b7 <+23>: add $0x1a49,%ebx
moving 0xdeaf to int dead. dead is at a -0xc (-12) byte offset below BOS
0x000005bd <+29>: movl $0xdeaf,-0xc(%ebp)
allocating 0xc (12) bytes on the stack for int dead
0x000005c4 <+36>: sub $0xc,%esp
precall for foo
0x000005c7 <+39>: pushl -0xc(%ebp)
calling foo
0x000005ca <+42>: call 0x5f9 <foo>
prolog for foo
0x000005cf <+47>: add $0x10,%esp
precall for printf
0x000005d2 <+50>: mov %eax,-0x10(%ebp)
0x000005d5 <+53>: sub $0x8,%esp
0x000005d8 <+56>: pushl -0x10(%ebp)
0x000005db <+59>: lea -0x1950(%ebx),%eax
0x000005e1 <+65>: push %eax
calling printf
0x000005e2 <+66>: call 0x400 <printf@plt>
prolog for printf
0x000005e7 <+71>: add $0x10,%esp
housekeeping before program exits
0x000005ea <+74>: mov $0x0,%eax
0x000005ef <+79>: lea -0x8(%ebp),%esp
0x000005f2 <+82>: pop %ecx
0x000005f3 <+83>: pop %ebx
0x000005f4 <+84>: pop %ebp
0x000005f5 <+85>: lea -0x4(%ecx),%esp
0x000005f8 <+88>: ret
----------------------------------------------
Assembly code for int foo()
0x000005f9 <+0>: push %ebp
0x000005fa <+1>: mov %esp,%ebp
0x000005fc <+3>: sub $0x10,%esp
0x000005ff <+6>: call 0x623 <__x86.get_pc_thunk.ax>
0x00000604 <+11>: add $0x19fc,%eax
doing math on int bar. bar is at a 0x8 (8) byte offset above
0x00000609 <+16>: subl $0x2,0x8(%ebp)
0x0000060d <+20>: shll $0x10,0x8(%ebp)
0x00000611 <+24>: movl $0xbeef,-0x4(%ebp)
0x00000618 <+31>: mov 0x8(%ebp),%eax
0x0000061b <+34>: or %eax,-0x4(%ebp)
0x0000061e <+37>: mov -0x4(%ebp),%eax
0x00000621 <+40>: leave
0x00000622 <+41>: ret
View
@@ -0,0 +1,4 @@
#!/bin/bash
cat asm.txt
cat main.c
View
Binary file not shown.
View
@@ -0,0 +1,11 @@
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
echo -e "${RED}Listing C source of my program...${NC}"
cat lab17.c
echo -e "${RED}Running my program...${NC}"
./lab17
echo -e "${RED}Printing annotated ASM of int main() in my program${NC}"
cat mainasm.txt
View
@@ -0,0 +1,143 @@
Listing C source of my program...
#include <stdio.h>
int main(void)
{
int codeface = 0xc0deface;
for(int i = 0; i < 5; i++)
{
int deadbeef = 0xdeadbeef;
printf("i is %i\n", i);
if((i % 2) == 0)
{
int baddecaf = 0xbaddecaf;
printf("%i is even\n", i);
int j = 10;
while(j > 6)
{
int beadcafe = 0xbeadcafe;
printf("j is %i\n", j);
j--;
}
}
}
}
Running my program...
i is 0
0 is even
j is 10
j is 9
j is 8
j is 7
i is 1
i is 2
2 is even
j is 10
j is 9
j is 8
j is 7
i is 3
i is 4
4 is even
j is 10
j is 9
j is 8
j is 7
Printing annotated ASM of int main() in my program
;Set up the stack
5a0: 8d 4c 24 04 lea 0x4(%esp),%ecx
5a4: 83 e4 f0 and $0xfffffff0,%esp
5a7: ff 71 fc pushl -0x4(%ecx)
5aa: 55 push %ebp
5ab: 89 e5 mov %esp,%ebp
;Push callee save registers onto the stack and allocate 32 bytes space for them
5ad: 53 push %ebx
5ae: 51 push %ecx
5af: 83 ec 20 sub $0x20,%esp
;Load position of the code into EBX. Global objects can now be called with an offset from EBX.
5b2: e8 b9 fe ff ff call 470 <__x86.get_pc_thunk.bx>
5b7: 81 c3 49 1a 00 00 add $0x1a49,%ebx
;Initialize int codeface on the stack at a 20 byte offset from BOS
5bd: c7 45 ec ce fa de c0 movl $0xc0deface,-0x14(%ebp)
;Begin setting up the for loop. Set i = 0.
5c4: c7 45 f4 00 00 00 00 movl $0x0,-0xc(%ebp)
;Jump to the beginning of the for loop
5cb: eb 75 jmp 642 <main+0xa2>
;Initialize int deadbeef on the stack at a 24 byte offset from BOS.
5cd: c7 45 e8 ef be ad de movl $0xdeadbeef,-0x18(%ebp)
;Do printf("i is %i\n", i);
5d4: 83 ec 08 sub $0x8,%esp
5d7: ff 75 f4 pushl -0xc(%ebp)
5da: 8d 83 e0 e6 ff ff lea -0x1920(%ebx),%eax
5e0: 50 push %eax
5e1: e8 1a fe ff ff call 400 <printf@plt>
5e6: 83 c4 10 add $0x10,%esp
5e9: 8b 45 f4 mov -0xc(%ebp),%eax
;Check if i%2 == 0. If i%2 != 0, jump over the next section
5ec: 83 e0 01 and $0x1,%eax
5ef: 85 c0 test %eax,%eax
5f1: 75 4b jne 63e <main+0x9e>
;Allocate int baddecaf on the stack at a 28 byte offset from BOS
5f3: c7 45 e4 af ec dd ba movl $0xbaddecaf,-0x1c(%ebp)
; Do printf("%i is even\n", i);
5fa: 83 ec 08 sub $0x8,%esp
5fd: ff 75 f4 pushl -0xc(%ebp)
600: 8d 83 e9 e6 ff ff lea -0x1917(%ebx),%eax
606: 50 push %eax
607: e8 f4 fd ff ff call 400 <printf@plt>
60c: 83 c4 10 add $0x10,%esp
60f: c7 45 f0 0a 00 00 00 movl $0xa,-0x10(%ebp)
;Jump to while(j > 6)
616: eb 20 jmp 638 <main+0x98>
;Allocate int beadcafe on the stack at a 32 byte offset from BOS
618: c7 45 e0 fe ca ad be movl $0xbeadcafe,-0x20(%ebp)
;Do printf("j is %i\n", j);
61f: 83 ec 08 sub $0x8,%esp
622: ff 75 f0 pushl -0x10(%ebp)
625: 8d 83 f5 e6 ff ff lea -0x190b(%ebx),%eax
62b: 50 push %eax
62c: e8 cf fd ff ff call 400 <printf@plt>
631: 83 c4 10 add $0x10,%esp
634: 83 6d f0 01 subl $0x1,-0x10(%ebp)
;Check if j > 6
638: 83 7d f0 06 cmpl $0x6,-0x10(%ebp)
;If j>6, jump to the top of the while loop
63c: 7f da jg 618 <main+0x78>
;Increment i in the for loop
63e: 83 45 f4 01 addl $0x1,-0xc(%ebp)
;Check if i == 4 (The same thing as i > 5 in this context)
642: 83 7d f4 04 cmpl $0x4,-0xc(%ebp)
;If i == 4, jump to the top of the for loop
646: 7e 85 jle 5cd <main+0x2d>
;Cleanup
648: b8 00 00 00 00 mov $0x0,%eax
64d: 8d 65 f8 lea -0x8(%ebp),%esp
650: 59 pop %ecx
651: 5b pop %ebx
652: 5d pop %ebp
653: 8d 61 fc lea -0x4(%ecx),%esp
656: c3 ret
657: 66 90 xchg %ax,%ax
659: 66 90 xchg %ax,%ax
65b: 66 90 xchg %ax,%ax
65d: 66 90 xchg %ax,%ax
65f: 90 nop
File renamed without changes.
View
No changes.
View
@@ -0,0 +1,13 @@
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
echo -e "${RED}Printing C source of my program...${NC}"
cat lab19.c
echo -e "${RED}Printing annotated ASM of my program...${NC}"
cat mainasm.txt
echo -e "${RED}Printing stack dump of my program...${NC}"
cat stackdump.txt
View
@@ -5,7 +5,7 @@ NC='\033[0m'
echo -e "${RED}Compiling programs...${NC}"
gcc -m32 -g lab20noinline.c -o lab20noinline
gcc -m32 -g lab20inline.c -o lab20inline -O2
gcc -m32 -g lab20inline.c -o lab20inline
gcc -m32 -g lab20noinline.c -c -o lab20noinline-object.o
gcc -m32 -g lab20inline.c -c -o lab20inline-object.o -O3
View
Binary file not shown.
Binary file not shown.
View
@@ -1,6 +1,6 @@
#include <stdio.h>
__attribute__((always_inline)) int hamming_distance(int x, int y);
__attribute__((always_inline)) inline int hamming_distance(int x, int y);
int main(void)
{
@@ -13,7 +13,7 @@ int main(void)
return hamming_distance(x, y);
}
__attribute__((always_inline)) int hamming_distance(int x, int y) {
__attribute__((always_inline)) inline int hamming_distance(int x, int y) {
int z = x ^ y;
int r = 0;
for (; z > 0; z >>= 1)
Oops, something went wrong.

0 comments on commit 41a8bae

Please sign in to comment.