Skip to content

Instantly share code, notes, and snippets.

@hraban
Created June 28, 2012 11:56
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 hraban/3010925 to your computer and use it in GitHub Desktop.
Save hraban/3010925 to your computer and use it in GitHub Desktop.
Initialize variables using static functions instead of macros
.file "test.c"
.text
.type a, @function
a:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
movq $10, -8(%rbp)
movq -8(%rbp), %rax
imulq $48879, %rax, %rax
movq %rax, -8(%rbp)
movq -8(%rbp), %rax
leave
ret
.cfi_endproc
.LFE0:
.size a, .-a
.type b, @function
b:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
call a
movq %rax, -8(%rbp)
movabsq $78288478382, %rax
addq %rax, -8(%rbp)
movq -8(%rbp), %rax
leave
ret
.cfi_endproc
.LFE1:
.size b, .-b
.type c, @function
c:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $24, %rsp
call b
movq %rax, -8(%rbp)
salq $7, -8(%rbp)
movq -8(%rbp), %rax
movq %rax, -24(%rbp)
movabsq $-3689348814741910323, %rdx
movq -24(%rbp), %rax
mulq %rdx
movq %rdx, %rax
shrq $3, %rax
movq %rax, -8(%rbp)
movq -8(%rbp), %rax
leave
ret
.cfi_endproc
.LFE2:
.size c, .-c
.section .rodata
.LC0:
.string "%lu\n"
.text
.globl main
.type main, @function
main:
.LFB3:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $32, %rsp
movl %edi, -20(%rbp)
movq %rsi, -32(%rbp)
call c
movq %rax, -8(%rbp)
movl $.LC0, %eax
movq -8(%rbp), %rdx
movq %rdx, %rsi
movq %rax, %rdi
movl $0, %eax
call printf
movl $0, %eax
leave
ret
.cfi_endproc
.LFE3:
.size main, .-main
.ident "GCC: (Debian 4.4.5-8) 4.4.5"
.section .note.GNU-stack,"",@progbits
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%lu\n"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB14:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movabsq $1002098779801, %rsi
movl $.LC0, %edi
xorl %eax, %eax
call printf
xorl %eax, %eax
addq $8, %rsp
ret
.cfi_endproc
.LFE14:
.size main, .-main
.ident "GCC: (Debian 4.4.5-8) 4.4.5"
.section .note.GNU-stack,"",@progbits
#include <stdio.h>
#include <stdlib.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
static uint64_t
a(void)
{
uint64_t x = 10;
x *= 0xbeef;
return x;
}
static uint64_t
b(void)
{
uint64_t x = a();
x += 78288478382;
return x;
}
static uint64_t
c(void)
{
uint64_t x = b();
x <<= 7;
x /= 10;
return x;
}
int
main(int argc, char *argv[])
{
(void) argc;
(void) argv;
uint64_t x = c();
printf("%" PRIu64 "\n", x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment