Is chatGPT a good coder?

Page 1/2
| 2

Par Totta

Expert (88)

Portrait de Totta

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 ou Inscrivez-vous pour poster

Par aoineko

Paragon (1137)

Portrait de aoineko

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.

Par Prodatron

Paragon (1857)

Portrait de Prodatron

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.

Par Micha

Expert (110)

Portrait de Micha

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.

Par ARTRAG

Enlighted (6977)

Portrait de ARTRAG

19-03-2023, 11:58

Awesome!

Par Ped7g

Expert (67)

Portrait de Ped7g

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.

Par Micha

Expert (110)

Portrait de Micha

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).

Par Micha

Expert (110)

Portrait de Micha

21-03-2023, 10:21

Yesterday I tried some more z80 coding with Bing GPT 4. I asked it to make a routine that multiplies HL with 5. Total failure... Then I asked to make a routine that multiplies A by 2. Worked (it used add a,a). Then I asked it to multiply by 3. Worked also (ld b,a - add a,a - add a,b). Then I asked to multiply by 4. Failed... and other multiplications would also fail.

Today I tried again. I asked: write a z80 assembly routine that multiplies a by 15 using the info on this page : http://map.grauw.nl/articles/mult_div_shifts.php
That worked, it copied the example on the page that multiplies 3 by 15 and left out the ld a,3. Great!
The I asked to multiply a by 11. It came with this:

; Multiply A by 11 (= A x 8 + A x 2 + A x 1)
ld b,a ; Store value of A x 1 in B
add a,a ; x2
ld c,a ; Store value of A x 2 in C
add a,a ; x4
add a,a ; x8
add a,c ; Add A x 2 to A x 8 + A x 1, result: a = (A * 11)
add a,b 

This is really amazing! It understood the concepts explained on the page and used them accordingly. If we would have one large webpage with all z80 tips and tricks combined with msx specific things (video, audio) it will probably be able to pull off some amazing stuff...

Par ro

Scribe (5059)

Portrait de ro

21-03-2023, 11:11

wow

Par Micha

Expert (110)

Portrait de Micha

21-03-2023, 17:40

I made a 2 page msx-basic "manual" that explains colors, the put sprite command, screen command and the stick command, etc. I fed it into Bing by just pointing the browser to that text file. I asked: "Please use this page and everything you already know to write a pong game in msx basic".

It came with this:

10 SCREEN 2,0,0:DEFINT A-Z
20 COLOR 15,1,1
30 X=120:Y=80:DX=2:DY=1
40 PADDLE=80
50 SPRITE$(0)=STRING$(8,255)
60 PUT SPRITE 0,(X,Y),15,0
70 PUT SPRITE 1,(16,PADDLE),15,0
80 PUT SPRITE 2,(240,PADDLE),15,0

90 IF STICK(0)=1 AND PADDLE>16 THEN PADDLE=PADDLE-4
100 IF STICK(0)=5 AND PADDLE<144 THEN PADDLE=PADDLE+4

110 X=X+DX:Y=Y+DY

120 IF Y<16 OR Y>176 THEN DY=-DY

130 IF X<32 AND (YPADDLE+48) THEN BEEP:GOTO 150
140 IF X<32 AND (Y>=PADDLE AND Y<=PADDLE+48) THEN DX=-DX

150 IF X>224 AND (YPADDLE+48) THEN BEEP:GOTO 170
160 IF X>224 AND (Y>=PADDLE AND Y<=PADDLE+48) THEN DX=-DX

170 PUT SPRITE 0,(X,Y),15,0
180 PUT SPRITE 1,(16,PADDLE),15,0
190 PUT SPRITE 2,(240,PADDLE),15,0

200 GOTO 90

It gave a syntax error in 40; but when I changed "PADDLE" to "P" I got a (sort of) working pong game !!!!!
No iterations or whatsoever. AMAZING !
With a better manual and 2 or 3 iterations (e.g. telling that you want 2 players instead of 1) it will provide a perfect program...

Par RvS

Expert (95)

Portrait de RvS

21-03-2023, 18:35

So you did all the hard work and Microsoft can charge extra for the new capabilities Smile

Page 1/2
| 2