ChatGPT with 6502
Posted: Mon Jan 02, 2023 11:03 am
I was checking out ChatGPT (https://chat.openai.com/chat) to see if it has any knowledge of 6502 programming. Interestingly, it has a little. But it does fall apart pretty quickly (see the multiplication request). It's way better with C++ and python.
Me:
Can you show me how to add two sixteen-bit numbers in 6502 assembly language?
ChatGPT:
Certainly! Here is an example of how you can add two sixteen-bit numbers in 6502 assembly language:
This code will add the two sixteen-bit numbers stored in memory at locations $2000-$2001 and $2002-$2003, and store the result in memory at locations $2004-$2005. The low bytes of the numbers are added first, and the high bytes are added subsequently. The carry flag is also set if the addition of the low bytes generates a carry.
I hope this helps! Let me know if you have any questions.
Me:
You should clear the carry flag before ADC when adding, or you may be off by one.
ChatGPT:
You are correct! I apologize for the oversight. Here is the correct code with the carry flag cleared before the addition of the low bytes:
Thank you for pointing out the error. I hope this revised code is helpful to you! Let me know if you have any other questions.
Me:
Can you show me how to multiply two eight-bit numbers?
ChatGPT:
Certainly! Here is an example of how you can multiply two eight-bit numbers in 6502 assembly language:
This code will multiply the two eight-bit numbers stored in memory at locations $2000 and $2001, and store the result in memory at locations $2002-$2003. The low byte of the result is stored in the accumulator, and the high byte is stored in the X register.
I hope this helps! Let me know if you have any questions.
Me:
6502 does not have a MUL instruction, though.
ChatGPT:
You are correct, I apologize for the error. The 6502 microprocessor does not have a built-in instruction for multiplying two numbers. To multiply two numbers in 6502 assembly language, you can use a loop to repeatedly add one number to itself a number of times equal to the other number.
Here is an example of how you can multiply two eight-bit numbers using a loop:
This code will multiply the two eight-bit numbers stored in memory at locations $2000 and $2001, and store the result in memory at locations $2002-$2003. The low byte of the result is stored in the accumulator, and the high byte is stored in the X register.
I apologize for the error in my previous response. I hope this revised code is helpful to you! Let me know if you have any other questions.
I did keep going for a bit, but I wasn't able to guide it to come up with a solution. As you can see, it tends to invent instructions, and then mis-use them.
Me:
Can you show me how to add two sixteen-bit numbers in 6502 assembly language?
ChatGPT:
Certainly! Here is an example of how you can add two sixteen-bit numbers in 6502 assembly language:
Code: Select all
; Assume that the two sixteen-bit numbers are stored in memory at locations $2000 and $2002,
; and that the low bytes are stored at the lower addresses.
; Load the low byte of the first number into the accumulator
LDA $2000
; Add the low byte of the second number to the accumulator
ADC $2002
; Store the result in the low byte of the sum
STA $2004
; Load the high byte of the first number into the accumulator
LDA $2001
; Add the high byte of the second number to the accumulator
ADC $2003
; Store the result in the high byte of the sum
STA $2005
I hope this helps! Let me know if you have any questions.
Me:
You should clear the carry flag before ADC when adding, or you may be off by one.
ChatGPT:
You are correct! I apologize for the oversight. Here is the correct code with the carry flag cleared before the addition of the low bytes:
Code: Select all
; Assume that the two sixteen-bit numbers are stored in memory at locations $2000 and $2002,
; and that the low bytes are stored at the lower addresses.
; Clear the carry flag
CLC
; Load the low byte of the first number into the accumulator
LDA $2000
; Add the low byte of the second number to the accumulator
ADC $2002
; Store the result in the low byte of the sum
STA $2004
; Load the high byte of the first number into the accumulator
LDA $2001
; Add the high byte of the second number to the accumulator
ADC $2003
; Store the result in the high byte of the sum
STA $2005
Me:
Can you show me how to multiply two eight-bit numbers?
ChatGPT:
Certainly! Here is an example of how you can multiply two eight-bit numbers in 6502 assembly language:
Code: Select all
; Assume that the two eight-bit numbers are stored in memory at locations $2000 and $2001.
; Load the first number into the accumulator
LDA $2000
; Multiply the accumulator by the second number
MUL $2001
; The result of the multiplication is stored in the accumulator (low byte) and the X register (high byte)
; Store the low byte of the result in memory at location $2002
STA $2002
; Store the high byte of the result in memory at location $2003
STX $2003
I hope this helps! Let me know if you have any questions.
Me:
6502 does not have a MUL instruction, though.
ChatGPT:
You are correct, I apologize for the error. The 6502 microprocessor does not have a built-in instruction for multiplying two numbers. To multiply two numbers in 6502 assembly language, you can use a loop to repeatedly add one number to itself a number of times equal to the other number.
Here is an example of how you can multiply two eight-bit numbers using a loop:
Code: Select all
; Assume that the two eight-bit numbers are stored in memory at locations $2000 and $2001.
; Load the first number into the accumulator
LDA $2000
; Clear the X register
CLC
; Loop for the value of the second number
LOOP:
; Add the first number to itself
ADD $2000
; Decrement the second number
DEC $2001
; If the second number is not zero, jump back to the start of the loop
BNE LOOP
; The result of the multiplication is stored in the accumulator (low byte) and the X register (high byte)
; Store the low byte of the result in memory at location $2002
STA $2002
; Store the high byte of the result in memory at location $2003
STX $2003
I apologize for the error in my previous response. I hope this revised code is helpful to you! Let me know if you have any other questions.
I did keep going for a bit, but I wasn't able to guide it to come up with a solution. As you can see, it tends to invent instructions, and then mis-use them.