Is chatGPT a good coder?

By Totta

Expert (85)

Totta's picture

19-02-2023, 23:36

Asked the AI robot ChatGPT this question today:

Write a program in z80 assembler that stops the interrupts, multiplies the value in the accumulator by 2 and then starts the interrupts again

It replied

di ; disable interrupts
ld a, (ix+0) ; load value from memory into the accumulator
add a, a ; multiply the accumulator by 2
ld (ix+0), a ; store the result back in memory
ei ; enable interrupts
ret ; return from subroutine

Any thoughts about this?

Login or register to post comments

By aoineko

Paladin (887)

aoineko's picture

19-02-2023, 23:48

I played a lot with ChatGPT to make it write Z80 assembly code, but it often needs to be fixed.
When we point out its errors, it ends up correcting them, but it is not very reliable.
For it to be really efficient, it should be trained, like bots are trained for a long time before being used in professional companies.

By Prodatron

Paragon (1838)

Prodatron's picture

20-02-2023, 00:36

ChatGPT is fine for modern programming languages.
For Z80 you already see, that it is limited. Eg. it thinks that there is a MUL command in Z80.
For 8bit Basic it is already quite ok.

By Micha

Expert (92)

Micha's picture

19-03-2023, 11:52

I tried some stuff with Microsoft Bing, that combines the new GPT-4 and internet searches. I asked it to write a z80 assembly program that copies 600 bytes from &1000 to &8000. This is what it came up with:

LD HL,&1000 ; source address
LD DE,&8000 ; destination address
LD BC,&0258 ; number of bytes to copy (600 in hexadecimal)
LDIR        ; copy BC bytes from HL to DE 

Then I said: "Now I want all the copied bytes to be 1 larger than the original byte." It came back with:

LD HL,&1000 ; source address
LD DE,&8000 ; destination address
LD BC,&0258 ; number of bytes to copy (600 in hexadecimal)

loop:
    LD A,(HL)   ; load byte from source into A
    INC A       ; increment A by 1
    LD (DE),A   ; store A into destination
    INC HL      ; increment source address
    INC DE      ; increment destination address
    DEC BC      ; decrement byte counter
    LD A,B      ; check if BC is zero
    OR C        ;
    JR NZ,loop  ; if not zero, repeat loop

I would say that this is pretty impressive...

Then I said: "when a byte was 255 it will overflow to 0, but I want it to stay 255 in that case". It came with:

LD HL,&1000 ; source address
LD DE,&8000 ; destination address
LD BC,&0258 ; number of bytes to copy (600 in hexadecimal)

loop:
    LD A,(HL)   ; load byte from source into A
    CP &FF      ; compare A with 255
    JR Z,skip   ; if equal, skip increment
    INC A       ; increment A by 1
skip:
    LD (DE),A   ; store A into destination
    INC HL      ; increment source address
    INC DE      ; increment destination address
    DEC BC      ; decrement byte counter
    LD A,B      ; check if BC is zero
    OR C        ;
    JR NZ,loop  ; if not zero, repeat loop

Then I asked:"can this piece of code be optimised in terms of speed?"
It answered: "Yes, this code can be optimized for speed by using the DJNZ instruction to implement the loop and by unrolling the loop to process multiple bytes per iteration."
It tried to implement this into new code but came with an unfinished piece of code.
I said: "Forget the unrolling, just use the djnz". It responded:

LD HL,&1000 ; source address
LD DE,&8000 ; destination address
LD B,&0258  ; number of bytes to copy (600 in hexadecimal)

loop:
    LD A,(HL)   ; load byte from source into A
    CP &FF      ; compare A with 255
    JR Z,skip   ; if equal, skip increment
    INC A       ; increment A by 1
skip:
    LD (DE),A   ; store A into destination
    INC HL      ; increment source address
    INC DE      ; increment destination address
    DJNZ loop   ; decrement B and repeat loop if not zero

I said:"This doesn't seem to work; B is an 8-bit register and cannot hold a value of 258 hex"
It answered:"You are correct. I apologize for the mistake. The DJNZ instruction cannot be used in this case because the B register is an 8-bit register and cannot hold a value of 600 (or &0258 in hexadecimal)." and came back with an earlier (correct) version of the code.
I think this is all pretty mindblowing. It clearly has its limits but also seems to have a pretty decent understanding of what it is doing.

By ARTRAG

Enlighted (6930)

ARTRAG's picture

19-03-2023, 11:58

Awesome!

By Ped7g

Expert (65)

Ped7g's picture

19-03-2023, 13:06

hm, so it goes for conditional jump first with the clamp... would expect it to do better, this kind of tricks is not secret?

for the record, it could do this:

  ...
  ld a,(hl)
  sub &FF    ; ++A ; keep &FF in some register to get another speed up
  adc a,&FF ; clamp to &FF
  ld (de),a
  ...

and with regard to the size-coding, and +1 copy of area - in special case if you are allowed to modify source area (not blaming ChatGPT to not suggest this, this is just for fun extra bonus):

  ...
copy_loop:
  inc (hl)
  ldi
  jp pe,copy_loop

which reminds me it should use at least `cpi` for the original proposal to optimise it:

loop:
    LD A,(HL)   ; load byte from source into A
    INC A       ; increment A by 1
    LD (DE),A   ; store A into destination
    INC DE      ; increment destination address
    CPI         ; increment source address and decrement counter
    JP PE,loop  ; if not zero, repeat loop

ie.. it's very impressive as technological feat, but doesn't look as "good coder", in higher level languages it looks to me more like "average coder" (not exactly in good way), and with Z80 assembler it's more like beginner. BUT as average coder it seems lot more productive and have lot more energy than me, so in many cases it may be most effective to use ChatGPT for first rough versions of code, and then fix the mess, at least for me that may be quicker and more productive as my first versions of code tend to take lot of time, and I find easier to do full rewrite once somebody shows me the "bad code"... Big smile YMMV of course.

By Micha

Expert (92)

Micha's picture

19-03-2023, 14:31

If an A.I. that is not specifically trained nor created to do this already comes with these answers (that are clearly better than what 99% of humans would answer), then I'm pretty sure that if a specific coding A.I. is created that can self-learn (by compiling the code that it makes and test the results) and program in an for A.I. optimised programming language, will nail any solvable problem flawlessly in code and does that very fast. And I don't think this is too far away (months, not years).