andy0130tw
3/17/2017 - 6:40 AM

aligned v.s. unaligned (?

aligned v.s. unaligned (?

	.file	"aligned-vs-unaligned.c"
	.section	.rodata
.LC0:
	.string	"aligned   (%p) = %d\n"
.LC1:
	.string	"unaligned (%p) = %d\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
	addq	$-128, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-112(%rbp), %rdx
	movl	$0, %eax
	movl	$12, %ecx
	movq	%rdx, %rdi
	rep stosq
	movq	%rdi, %rdx
	movl	%eax, (%rdx)
	addq	$4, %rdx
	movb	$1, -112(%rbp)
	movb	$2, -111(%rbp)
	movb	$3, -110(%rbp)
	movb	$4, -109(%rbp)
	movb	$5, -108(%rbp)
	leaq	-112(%rbp), %rax
	movq	%rax, -120(%rbp)
	movq	-120(%rbp), %rax
	movl	(%rax), %edx
	movq	-120(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rdi
	movl	$0, %eax
	call	printf@PLT
	leaq	-112(%rbp), %rax
	addq	$1, %rax
	movq	%rax, -120(%rbp)
	movq	-120(%rbp), %rax
	movl	(%rax), %edx
	movq	-120(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rcx
	xorq	%fs:40, %rcx
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005"
	.section	.note.GNU-stack,"",@progbits
#include<stdio.h>

int main() {
  char c[100] = { 1, 2, 3, 4, 5 };
  int* p = (int*) c;
  printf("aligned   (%p) = %d\n", p, *p);
  p = (int*) (c + 1);
  printf("unaligned (%p) = %d\n", p, *p);
}