#x86, 4141 39 bytes
EditChangelog: Fix byte count using not
instead of neg
/dec
.
Fix byte count using
not
instead ofneg
/dec
.-2 bytes by not
xor
ingedx
since it is probably 0 fromimul
.
<!>
.section .text
.globl main
main:
mov $10, %ecx # n = 10
start:
lea (cube),%edi # save function pointer
call *%edi # output n^3
sub %ecx, %eax # n^3 - n
xor %edx, %edx # edx = 0 from cube
push $6
pop %ebx # const 6
idiv %ebx # k = (n^3 - n)/6
mov %eax, %ecx # save k
call *%edi # output k^3
push %eax # output k^3
not %ecx # -k-1
call *%edi # output (-k-1)^3
inc %ecx
inc %ecx # -k+1
call *%edi # output (-k+1)^3
ret
cube: # eax = ecx^3
pop %esi
mov %ecx, %eax
imul %ecx
imul %ecx
push %eax # output cube
jmp *%esi # ret
00000005 <start>:
5: 8d 3d 2422 00 00 00 lea 0x240x22,%edi
b: ff d7 call *%edi
d: 29 c8 sub %ecx,%eax
f: 31 d2 xor %edx,%edx
11: 6a 06 push $0x6
1311: 5b pop %ebx
1412: f7 fb idiv %ebx
1614: 89 c1 mov %eax,%ecx
1816: ff d7 call *%edi
1a18: 50 push %eax
1b19: f7 d1 not %ecx
1d1b: ff d7 call *%edi
1f1d: 41 inc %ecx
201e: 41 inc %ecx
211f: ff d7 call *%edi
2321: c3 ret
0000002400000022 <cube>:
2422: 5e pop %esi
2523: 89 c8 mov %ecx,%eax
2725: f7 e9 imul %ecx
2927: f7 e9 imul %ecx
2b29: 50 push %eax
2c2a: ff e6 jmp *%esi
Here is my testing version that does all the cubing at the end. After the values are pushed on the stack, the cube loop overwrites stack values. It's currently 4242 40 bytes but there should be some improvements somewhere.
.section .text
.globl main
main:
mov $10, %ecx # n = 10
start:
push %ecx # output n
mov %ecx, %eax
imul %ecx
imul %ecx
sub %ecx, %eax # n^3 - n
xor %edx, %edx # edx = 0 from imul
push $6
pop %ecx # const 6
idiv %ecx # k = (n^3 - n)/6
push %eax # output k
push %eax # output k
not %eax # -k-1
push %eax # output -k-1
inc %eax
inc %eax # -k+1
push %eax # output -k+1
dec %ecx # count = 5
add $20, %esp
cube:
mov -4(%esp),%ebx # load num from stack
mov %ebx, %eax
imul %ebx
imul %ebx # cube
push %eax # output cube
loop cube # --count; while (count)
ret