0

I have stored the string '0123456789', so now the ASCII value of each character is stored as one byte in the memory, How can add each consecutive 2 bytes like the following:

0+1->1, 1+2->2........etc

“0123456789”
30 31 32 33 34 35 36 37 38 39 
00000000 00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001

00010001
00001111 
00001101
00001011
00001001
00000111
00000101
00000011
00000001
00000000

My first try was something like this

ORG     $1000
START:          DC.L    '0123456789'        
                MOVE.L  #$1000, A1

                MOVE.B  (A1)+, D0
                MOVE.B  (A1)+, D1
                MOVE.B  (A1)+, D2
                MOVE.B  (A1)+, D3
                MOVE.B  (A1)+, D4
                MOVE.B  (A1)+, D6
                MOVE.B  (A1)+, D6
                MOVE.B  (A1)+, D7



    SIMHALT            



    END    START        
Paul R
  • 208,748
  • 37
  • 389
  • 560
Thair Abdalla
  • 279
  • 1
  • 5
  • 16

1 Answers1

1

As you pointed out, the values stored as character codes, not integers.

So to add them, you must convert to integer by subtracting '0'. Let's assume you use a platform where the decimal digits are encoded in sequential codes (like C requires, for instance). This is common.

Here's a sub-routine that adds two such digits, pointed to by a1 as in your code, and returning the sum in d1. It only advances a single character, so if you do this in a loop you will sum first index 0 and 1, then index 1 and 2, and so on.

sum_two:
   move.b (a1)+,d1
   subi.b #'0',d1
   add.b  (a1),d1
   subi.b #'0',d1
   rts

Note: It's been BIGNUM years since I last wrote MC68k assembly, so I might have some detail off.

unwind
  • 391,730
  • 64
  • 469
  • 606