Skip to content

Instantly share code, notes, and snippets.

@goshlanguage
Last active March 9, 2022 02:06
Show Gist options
  • Save goshlanguage/dd694940573a48e5d32f032a533a78fd to your computer and use it in GitHub Desktop.
Save goshlanguage/dd694940573a48e5d32f032a533a78fd to your computer and use it in GitHub Desktop.
A fun typo in C.
.section __TEXT,__text,regular,pure_instructions
.build_version macos, 12, 0 sdk_version 12, 1
.intel_syntax noprefix
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## %bb.0:
push rbp
.cfi_def_cfa_offset 16
.cfi_offset rbp, -16
mov rbp, rsp
.cfi_def_cfa_register rbp
mov edi, 2
mov esi, 5
call _power
lea rdi, [rip + L_.str]
mov esi, eax
xor eax, eax
call _printf
xor eax, eax
pop rbp
ret
.cfi_endproc
## -- End function
.globl _power ## -- Begin function power
.p2align 4, 0x90
_power: ## @power
.cfi_startproc
## %bb.0:
push rbp
.cfi_def_cfa_offset 16
.cfi_offset rbp, -16
mov rbp, rsp
.cfi_def_cfa_register rbp
mov eax, 1
test esi, esi
jle LBB1_2
.p2align 4, 0x90
LBB1_1: ## =>This Inner Loop Header: Depth=1
imul eax, edi
test esi, esi
jns LBB1_1
LBB1_2:
pop rbp
ret
.cfi_endproc
## -- End function
.section __TEXT,__cstring,cstring_literals
L_.str: ## @.str
.asciz "Power of 2^5 = %d\n"
.subsections_via_symbols
// Poorly copied from K&R C, this snippet probably doesn't function like you expect it to.
// You can either run it directly to observe the result, or compile it and step through it with GDB.
//. You can see an interesting result at 30 steps or so, watching the var p
#include <stdio.h>
int power(int, int);
int
main() {
printf("Power of 2^5 = %d\n", power(2,5));
return 0;
}
// power raises the base to the nth power
int
power(int base, int n) {
int i, p;
p = 1;
for (i = 1; i <= n; ++n) {
p = p * base;
}
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment