Discussion:
Codegolf exercise on sum of cubes in x86 + C compiler for check
Add Reply
Rosario19
2025-01-03 12:43:59 UTC
Reply
Permalink
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
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
Rosario19
2025-01-03 12:50:19 UTC
Reply
Permalink
Post by Rosario19
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
R.Wieser
2025-01-03 13:18:02 UTC
Reply
Permalink
Rosario19,
Post by Rosario19
jc 0x807
...
Post by Rosario19
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
Kerr-Mudd, John
2025-01-03 20:08:27 UTC
Reply
Permalink
On Fri, 3 Jan 2025 14:18:02 +0100
Post by R.Wieser
Rosario19,
Post by Rosario19
jc 0x807
...
Post by Rosario19
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.
Loading...