• Codegolf exercise on sum of cubes in x86 + C compiler for check

    From Rosario19@Ros@invalid.invalid to alt.lang.asm on Fri Jan 3 13:43:59 2025
    From Newsgroup: alt.lang.asm

    In this link there is one codegolf exercise I try to solve.

    https://codegolf.stackexchange.com/questions/277465/is-it-in-the-sequence-sum-of-the-first-n-cubes

    It is about build one function with 1 parameter of input.
    If the number of imput is sum of the cube of some consecutive numbers
    1,2,...n
    it should return 1, else should return 0.

    this is my answer function q3, it return -1 for error,
    0 if the number is not a sum of consecutive numbers,
    else return 1.

    -----------------------------------
    section _DATA use32 public class=DATA
    global _main
    extern _printf
    Iq3IIdIIIdIn db "q3(%d)=%d" , 13, 10, 0
    section _BSS use32 public class=BSS
    section _TEXT use32 public class=CODE

    ;0i,4ra,8Par
    align 4
    q3:
    push esi
    xor ecx, ecx
    xor esi, esi
    jmp short .1
    .e: xor eax, eax
    dec eax
    jmp short .z
    .1: inc esi
    mov eax, esi
    mul esi
    mul esi
    add ecx, eax
    jc .e
    cmp ecx, dword[esp+ 8]
    jb .1
    xor eax, eax
    cmp ecx, dword[esp+ 8]
    ja .z
    inc eax
    .z:
    pop esi
    ret

    align 4
    _main:
    pushad
    mov esi, 0
    .1: inc esi
    push esi
    call q3
    add esp, 4
    cmp eax, 0
    je .2
    push eax
    push esi
    push Iq3IIdIIIdIn
    call _printf
    add esp, 12
    .2: cmp esi, 1000
    jbe .1
    mov esi, -1
    push esi
    call q3
    add esp, 4
    push eax
    push esi
    push Iq3IIdIIIdIn
    call _printf
    add esp, 12
    popad
    xor eax, eax
    ret
    -----------------------

    executable obtained from
    Nasmw -fobj rndasm.asm
    bcc32 rndasm.obj
    ------------
    obtain as result for numbers <= 1001,
    and what happen in overflow seen the last
    q3(1)=1
    q3(9)=1
    q3(36)=1
    q3(100)=1
    q3(225)=1
    q3(441)=1
    q3(784)=1
    q3(-1)=-1
    -----------------------
    ;The origin
    section _DATA use32 public class=DATA
    global _main
    extern _printf
    "q3(%d)=%d\n" db "q3(%d)=%d",13,10,0
    section _BSS use32 public class=BSS
    section _TEXT use32 public class=CODE

    ;0i,4ra,8Par
    align 4
    q3:
    <i
    c^=c|i^=i|#.1
    .e: a^=a|--a |#.z
    .1: ++i |a=i |mul i|mul i|c+=a|jc .e|c<^8#.1
    a^=a|c>^8#.z|++a
    .z:
    i
    ret

    align 4
    _main:
    pushad
    i=0
    .1: ++i|q3<(i)|a==0#.2|_printf<("q3(%d)=%d\n",i,a)
    .2: i<=1000#.1
    i=-1|q3<(i)|_printf<("q3(%d)=%d\n",i,a)
    popad
    a^=a
    ret
    ------------------------

    40 bytes it seems the lenght of function q3

    00000800 56 push esi
    00000801 31C9 xor ecx,ecx
    00000803 31F6 xor esi,esi
    00000805 EB05 jmp short 0x80c
    00000807 31C0 xor eax,eax
    00000809 48 dec eax
    0000080A EB1A jmp short 0x826
    0000080C 46 inc esi
    0000080D 89F0 mov eax,esi
    0000080F F7E6 mul esi
    00000811 F7E6 mul esi
    00000813 01C1 add ecx,eax
    00000815 72F0 jc 0x807
    00000817 3B4C2408 cmp ecx,[esp+0x8]
    0000081B 72EF jc 0x80c
    0000081D 31C0 xor eax,eax
    0000081F 3B4C2408 cmp ecx,[esp+0x8]
    00000823 7701 ja 0x826
    00000825 40 inc eax
    00000826 5E pop esi
    00000827 C3 ret
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Rosario19@Ros@invalid.invalid to alt.lang.asm on Fri Jan 3 13:50:19 2025
    From Newsgroup: alt.lang.asm

    On Fri, 03 Jan 2025 13:43:59 +0100, Rosario19 wrote:

    00000813 01C1 add ecx,eax
    00000815 72F0 jc 0x807

    there is a difference of above and this below?

    add ecx,eax
    jb 0x807

    it seems are the same
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From R.Wieser@address@is.invalid to alt.lang.asm on Fri Jan 3 14:18:02 2025
    From Newsgroup: alt.lang.asm

    Rosario19,

    jc 0x807
    ...
    jb 0x807

    it seems are the same

    They are. The only difference is in how/where the human programmer might
    use them. A "jump-if-borrow" folowing an addition is contra-intuitive.
    And so is (might be) a "jump-if-carry" after a subtraction.

    And by the way: my instruction listing shows "jnae" as generating the same code.

    Regards,
    Rudy Wieser


    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Kerr-Mudd, John@admin@127.0.0.1 to alt.lang.asm on Fri Jan 3 20:08:27 2025
    From Newsgroup: alt.lang.asm

    On Fri, 3 Jan 2025 14:18:02 +0100
    "R.Wieser" <address@is.invalid> wrote:

    Rosario19,

    jc 0x807
    ...
    jb 0x807

    it seems are the same

    They are. The only difference is in how/where the human programmer might use them. A "jump-if-borrow" folowing an addition is contra-intuitive.
    And so is (might be) a "jump-if-carry" after a subtraction.

    And by the way: my instruction listing shows "jnae" as generating the same code.

    Yup, they're all just synonyms for 0x72, AFAICT.
    --
    Bah, and indeed Humbug.
    --- Synchronet 3.21d-Linux NewsLink 1.2