Domanda

I initially (asked for help) and wrote a BASIC program in the 6502 pet emulator which added two n-byte integers. However, my feedback was that it was simply adding two 16 bit integers (not adding n-byte integers).

Can anyone help me understand this feedback by looking at my code and point me in the right direction to make a program that adds two n-byte integers?

Thank You for the collaboration!

Documentation: Adds two n-byte integers using absolute indexed addressing. The addends begin at memory locations $0600, $0700 and the answer is at $0800. Byte length of the integers is at $0600 (¢ —> 256)

Machine Code:

18  a2  00  ac  00  06  bd  00
07  7d  00  08  9d  00  09  e8
00  88  00      d0

Op Codes, Documentation, Variables:

A1 = $0600
B1 = $0700
B2 = $0800
Z1 = $0900

[START] = $0500

            CLC                 18             // loads x with 0
            LDX                 A2  00     // loads length on Y                     
            LDY         A1      AC  00  06     // load first operand                    
loop:       LDA         B1, x   BD  00  07     // adds second operand       
            ADC         B2, x   7D  00  08     // store result  
            STA         Z1, x   9D  00  09     // go to next byte       
            INX                 E8  00     // count how many are left       
            DEY                 88  00         // do more if needed         
            BNE    loop         D0
È stato utile?

Soluzione

It looked to me like your code does what you claim -- adds two N byte operands in little-endian byte order. I vaguely remembered the various addressing modes of the 6502 from my misspent youth and the code seems fine. X is used to index the current byte from the two numbers, Y is a counter for the length of the operands in bytes and you loop over those bytes, stored at addresses 0x0700 and 0x0800 and write the result at address 0x0900.

Rather than get the Commodore 64 out of the attic and try it out I used an online virtual 6502 simulator. On this site we can set the memory address and load the byte values in. They even link to a page to assemble opcodes too. So setting the memory locations 0x0600 to "04" and both 0x0700 and 0x0800 to "04 03 02 01" we should see this code add these two 32 bit values (0x01020304 + 0x01020304 == 0x02040608).

Stepping through the code by clicking on the PC register and setting it to 0x0500 and then single stepping we see there is a bug in your machine code. After INX which compiles to E8 we hit a spurious 0x00 value(BRK) which terminates. The corrected code as below runs to completion and the expected value is seen by reading the memory at 0x0900.

0000        CLC             18
0001        LDX #$00        A2 00
0003        LDY $0600       AC 00 06
0006 LOOP:  LDA $0700,X     BD 00 07
0009        ADC $0800,X     7D 00 08
000C        STA $0900,X     9D 00 09
000F        INX             E8
0010        DEY             88
0011        BNE LOOP:       D0 F3

Memory dump:

:0900  08 06 04 02 00 00 00 00
:0908  00 00 00 00 00 00 00 00
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top