Skip to content

Instantly share code, notes, and snippets.

@uchan-nos
Created February 12, 2018 09:01
Show Gist options
  • Save uchan-nos/c40335bc3283c7084cef7710a5cad770 to your computer and use it in GitHub Desktop.
Save uchan-nos/c40335bc3283c7084cef7710a5cad770 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void func(void)
{
int a[100];
int n, m = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%d", a + i);
if (a[i] > m) m = a[i];
}
printf("max is %d\n", m);
}
/** How to compile
$ clang-5.0 -c -O2 a.c -mstack-alignment=16
tested on Ubuntu 16.04
*/
/**
From disassemble result below:
0: 55 push rbp
1: 41 56 push r14
3: 53 push rbx
4: 48 81 ec a0 01 00 00 sub rsp,0x1a0
Because first 3 `push`es shall push 8x3 = 24 bytes to the stack,
and `sub` will subtract 16-aligned value from the stack pointer,
then the stack pointer shall become to be mis-aligned.
*/
/** All disassemble result
0000000000000000 <func>:
0: 55 push rbp
1: 41 56 push r14
3: 53 push rbx
4: 48 81 ec a0 01 00 00 sub rsp,0x1a0
b: 45 31 f6 xor r14d,r14d
e: 48 8d 74 24 0c lea rsi,[rsp+0xc]
13: bf 00 00 00 00 mov edi,0x0
18: 31 c0 xor eax,eax
1a: e8 00 00 00 00 call 1f <func+0x1f>
1f: 83 7c 24 0c 00 cmp DWORD PTR [rsp+0xc],0x0
24: 7e 33 jle 59 <func+0x59>
26: 48 8d 5c 24 10 lea rbx,[rsp+0x10]
2b: 31 ed xor ebp,ebp
2d: 45 31 f6 xor r14d,r14d
30: bf 00 00 00 00 mov edi,0x0
35: 31 c0 xor eax,eax
37: 48 89 de mov rsi,rbx
3a: e8 00 00 00 00 call 3f <func+0x3f>
3f: 8b 03 mov eax,DWORD PTR [rbx]
41: 44 39 f0 cmp eax,r14d
44: 44 0f 4d f0 cmovge r14d,eax
48: 48 ff c5 inc rbp
4b: 48 63 44 24 0c movsxd rax,DWORD PTR [rsp+0xc]
50: 48 83 c3 04 add rbx,0x4
54: 48 39 c5 cmp rbp,rax
57: 7c d7 jl 30 <func+0x30>
59: bf 00 00 00 00 mov edi,0x0
5e: 31 c0 xor eax,eax
60: 44 89 f6 mov esi,r14d
63: e8 00 00 00 00 call 68 <func+0x68>
68: 48 81 c4 a0 01 00 00 add rsp,0x1a0
6f: 5b pop rbx
70: 41 5e pop r14
72: 5d pop rbp
73: c3 ret
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment