Skip to main content
added 108 characters in body
Source Link
qwr
  • 12.3k
  • 6
  • 48
  • 80

#x86, 4141 39 bytes

EditChangelog: Fix byte count using not instead of neg/dec.

  • Fix byte count using not instead of neg/dec.

  • -2 bytes by not xoring edx since it is probably 0 from imul.

<!>

.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
                

#x86, 41 bytes

Edit: Fix byte count using not instead of neg/dec.

.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
        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 24 00 00 00       lea    0x24,%edi
   b:   ff d7                   call   *%edi
   d:   29 c8                   sub    %ecx,%eax
   f:   31 d2                   xor    %edx,%edx
  11:   6a 06                   push   $0x6
  13:   5b                      pop    %ebx
  14:   f7 fb                   idiv   %ebx
  16:   89 c1                   mov    %eax,%ecx
  18:   ff d7                   call   *%edi
  1a:   50                      push   %eax
  1b:   f7 d1                   not    %ecx
  1d:   ff d7                   call   *%edi
  1f:   41                      inc    %ecx
  20:   41                      inc    %ecx
  21:   ff d7                   call   *%edi
  23:   c3                      ret    

00000024 <cube>:
  24:   5e                      pop    %esi
  25:   89 c8                   mov    %ecx,%eax
  27:   f7 e9                   imul   %ecx
  29:   f7 e9                   imul   %ecx
  2b:   50                      push   %eax
  2c:   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 42 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
        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
                

#x86, 41 39 bytes

Changelog:

  • Fix byte count using not instead of neg/dec.

  • -2 bytes by not xoring edx since it is probably 0 from imul.

<!>

.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
                            # 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 22 00 00 00       lea    0x22,%edi
   b:   ff d7                   call   *%edi
   d:   29 c8                   sub    %ecx,%eax
   f:   6a 06                   push   $0x6
  11:   5b                      pop    %ebx
  12:   f7 fb                   idiv   %ebx
  14:   89 c1                   mov    %eax,%ecx
  16:   ff d7                   call   *%edi
  18:   50                      push   %eax
  19:   f7 d1                   not    %ecx
  1b:   ff d7                   call   *%edi
  1d:   41                      inc    %ecx
  1e:   41                      inc    %ecx
  1f:   ff d7                   call   *%edi
  21:   c3                      ret    

00000022 <cube>:
  22:   5e                      pop    %esi
  23:   89 c8                   mov    %ecx,%eax
  25:   f7 e9                   imul   %ecx
  27:   f7 e9                   imul   %ecx
  29:   50                      push   %eax
  2a:   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 42 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
                                # 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
                
added 1522 characters in body
Source Link
qwr
  • 12.3k
  • 6
  • 48
  • 80

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 42 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
        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
                

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 42 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
        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
                
added 60 characters in body
Source Link
qwr
  • 12.3k
  • 6
  • 48
  • 80

Edit: Fix byte count using not instead of neg/dec.

Edit: Fix byte count using not instead of neg/dec.

deleted 70 characters in body
Source Link
qwr
  • 12.3k
  • 6
  • 48
  • 80
Loading
Source Link
qwr
  • 12.3k
  • 6
  • 48
  • 80
Loading