News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

ChatGPT+Gemini Experiments

Started by zedd151, December 30, 2024, 03:40:41 PM

Previous topic - Next topic

zedd151

One thing I just noticed during a lengthy ChatGPT session. You are only allowed a certain number of queries using the latest version of ChatGPT. After that you get dumbed down results, using a less that optimal version of ChatGPT... now I have to remove those crappy functions. :eusa_boohoo:
:cool:

daydreamer

Quote from: zedd151 on January 01, 2025, 05:08:34 PMOne thing I just noticed during a lengthy ChatGPT session. You are only allowed a certain number of queries using the latest version of ChatGPT. After that you get dumbed down results, using a less that optimal version of ChatGPT... now I have to remove those crappy functions. :eusa_boohoo:
Well someone used to downgrade to win7,could downgrade chatgpt to before dumbed down ?:)
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

zedd151

Only three out of the twelve functions (that I have asked ChatGPT for so far) actually do what they say they do, and work 100% of the time. The easiest functions, of course.

Hmmm... is ChatGPT supplying non working code so the users can supply to ChatGPT the debugged and fixed code? Very sneaky if that's the case. More likely they would supply working code more often for paying customers, that pay their monthly fee. The free version has yet another limitation?  :badgrin:

Anyway the easier algorithms are easy enough to spot where the errors occur, in ollydbg of course. But I won't tell that to the chat bot.  :biggrin:

Time-wise this is getting to be an expensive venture. Might just be easier/faster to work on getting my assembly code working the way I need it to work, without these so-called shortcuts.  :greensml:
:cool:

daydreamer

Quote from: zedd151 on January 02, 2025, 02:07:20 PMAnyway the easier algorithms are easy enough to spot where the errors occur, in ollydbg of course. But I won't tell that to the chat bot.  :biggrin:

Time-wise this is getting to be an expensive venture. Might just be easier/faster to work on getting my assembly code working the way I need it to work, without these so-called shortcuts.  :greensml:
I agree with using debugger where errors occur,but want to add : debugger also good to single step to better  understand some new code works ,and I use debugger to get right new Opcode's I never used before and recently get right packed compares

If you feel most comfortable with coding asm,and years of experience you got fast code production speed,its the way to prefer when experienced c coder/asm newbie might prefer c code as starting point -> asm
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

zedd151

I am having a lot of fun, having ChatGPT generate code then testing it. Both C and assembly.
Today I have been playing around with Googles AI counterpart, Gemini. Big fun.  :azn:

Who needs AI. I certainly don't!!! But is surely fun to experiment with, especially when I run out of ideas of my own. I might even start a subscription to one of these services for continual use of the 'premium' version (or whatever it's called), I would have to check and see first, if I can cancel at any time, without any type of penalty.  :biggrin:

If I find anything truly worthwhile, I'll be sure to post it. So far, I am just monkeying around, nothing serious.
:cool:

daydreamer

Well yesterday artists got inspired by a woman to get ideas to new artwork,or even made portrait like Mona Lisa
Nothing strange with get inspired by chatgpt ,when some where inspired by Eliza programming to try write their own version or inspired write other string manipulation, like Swedish to Danish translation, short word list + change Swedish words that usually ends in "a" with Danish "e" ending,making it look Danish
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

zedd151

An interesting exchange between myself and Googles 'Gemini' AI, regarding a function to calculate entropy. It failed miserably in generating masm32 compatible code even though I provided some corrections to the auxilliary code, as it would not even assemble or link without those numerous changes. But the entropy function itself was flawed, as I found out once the program assembled and linked without any errors. (That code I did not 'fix', I left the algo intact.


Quote from: zedd151Yes, it is helpful. But as I am more proficient and knowledgable about assembly language, I preferred assembly.
I might disassemble the C program to see if I can make a masm32 compatible version of it myself.
Would this violate any rules?


Quote from: Google_GeminiYou're absolutely right! Disassembling C code can be a great way to learn about how compilers translate high-level code into assembly language.

Here's a general approach to disassembling C code and understanding the resulting assembly:

Compile the C code: Compile the C code with optimization flags disabled (e.g., -O0 for GCC) to generate assembly code that is easier to read and understand.
Disassemble the executable: Use a disassembler tool (like objdump on Linux or ndisasm on Windows) to disassemble the compiled executable file.
Analyze the assembly code:
Identify the assembly instructions corresponding to different parts of the C code (e.g., loops, conditional statements, function calls).
Pay attention to how the compiler handles data structures, function calls, and memory management.
Compare the generated assembly code with your own hand-written assembly code to understand the differences and learn from the compiler's optimizations.
Note:

Disassembled code can be complex and difficult to read, especially for optimized code.
The generated assembly code can vary significantly depending on the compiler, compiler flags, and the target architecture.
By analyzing the disassembled code, you can gain valuable insights into how compilers translate C code into machine code, which can help you write more efficient and optimized assembly code in the future.

I hope this helps! Let me know if you have any other questions.

Anyway, for anyone interested here is first, the C code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX_BYTES 256

double calculate_entropy(unsigned char* data, size_t size) {
    double entropy = 0.0;
    size_t counts[MAX_BYTES] = {0};

    // Count occurrences of each byte value
    for (size_t i = 0; i < size; i++) {
        counts[data[i]]++;
    }

    // Calculate probabilities
    for (int i = 0; i < MAX_BYTES; i++) {
        double probability = (double)counts[i] / size;
        if (probability > 0) {
            entropy -= probability * log2(probability);
        }
    }

    return entropy;
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        return 1;
    }

    FILE* file = fopen(argv[1], "rb");
    if (file == NULL) {
        fprintf(stderr, "Error opening file: %s\n", argv[1]);
        return 1;
    }

    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    rewind(file);

    unsigned char* data = (unsigned char*)malloc(file_size);
    if (data == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        fclose(file);
        return 1;
    }

    if (fread(data, 1, file_size, file) != file_size) {
        fprintf(stderr, "Error reading file.\n");
        fclose(file);
        free(data);
        return 1;
    }

    fclose(file);

    double entropy = calculate_entropy(data, file_size);

    printf("Entropy: %.4f bits/byte\n", entropy);

    free(data);
    return 0;
}

And the last version of masm compatible assembly from Gemini (assembles and links, but crashes in the CalculateEntropy function), if anyone wants to tinker with it:
include \masm32\include\masm32rt.inc

.data?
    buffer db 4096 dup(?)
    counts DD 256 dup(?)
    entropy REAL4 ?
    fileHandle DD ?
    bytesRead DD ?
    fileSize DD ?
    bytesToRead DD ?

.const
    format db 'Entropy: %f bits/byte', 13, 10, 0
    filename db "input.bin", 0

.code
CalculateEntropy PROC countsArray:DWORD
    push esi
    push edi

    fldz            ; Initialize FPU stack with 0.0

    mov esi, countsArray ; Point ESI to the counts array
    xor ecx, ecx    ; Initialize loop counter to 0

@Loop:
    cmp ecx, 256    ; Check if all byte counts have been processed
    je @EndLoop

    mov eax, dword ptr [esi] ; Load count into a register (EAX)
    test eax, eax  ; Test if count is zero
    sahf            ; Set flags according to status word
    jbe @Skip        ; Skip if count is zero

    fild dword ptr [eax]      ; Load count onto the FPU stack
    fild dword ptr [fileSize] ; Load file size onto the FPU stack
    fdivp st(1), st(0)      ; Calculate probability (count / fileSize)
    fyl2x                    ; Calculate log2(probability)
    fmulp st(1), st(0)      ; Multiply probability by count
    fchs                    ; Negate the result
    faddp st(1), st(0)      ; Add to the running total

@Skip:
    add esi, 4              ; Move to the next count
    inc ecx                  ; Increment loop counter
    jmp @Loop

@EndLoop:
    fstp entropy            ; Store the result in the entropy variable

    pop edi
    pop esi
    ret
CalculateEntropy ENDP
    start:
        fn CreateFile, "hexbytes.bin", GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
        mov fileHandle, eax
        test eax, eax
        jz @Exit

        invoke GetFileSize, fileHandle, 0
        mov fileSize, eax

    .while (fileSize > 0)
        mov bytesToRead, 4096
        mov eax, bytesToRead
        cmp eax, fileSize
        jle @ReadChunk
        mov eax, fileSize
        mov bytesToRead, eax

    @ReadChunk:
        invoke ReadFile, fileHandle, addr buffer, bytesToRead, addr bytesRead, 0
        test eax, eax
        jz @Exit

        mov esi, offset buffer
        mov ecx, bytesToRead
    @ReadLoop:
        mov al, byte ptr [esi]
        inc dword ptr counts[eax]
        inc esi
        loop @ReadLoop

        mov eax, bytesToRead
        sub fileSize, eax

    .endw

    invoke CloseHandle, fileHandle

    invoke CalculateEntropy, addr counts

    fld dword ptr [entropy]
    fstp st(0)

    push offset entropy
    push offset format
    call crt_printf

    invoke ExitProcess, 0

@Exit:
    invoke ExitProcess, 1

end start
Not exactly how I would have read the file, but Gemini had other ideas.  :tongue: also I would not have used a .data buffer for input, but allocated memory. 

:biggrin:  Big Phun!!
The C version works flawlessly

I guess Gemini's response is approval for what I had suggested I would do to get a working asm source.  :biggrin:

C project attached.  :badgrin:
:cool: