|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Use of registers
Hi,
I have a problem this question. count the number of values in array data1 that equal the corresponding value in the array data2. both arrays have length 100. Code:
; initialization of pointers
ldx data1
ldy data2
ldab count ;initialize counter
loop:
ldaa 1,x+ ;going through data1
ldab 1,y+ ; going through data2
CBA ; compare A-B
bne loop ; if not 0 branch back to loop
inb ; increment B
cmpb #100 ; check if end of array
bne loop
swi ; end of program
My problem here is that I have I am sure I dont use the accumulator B well . I used it for initialization and to get the data through data2. Can please someone suggest me how to handle this problem? Thank you B Last edited by brad sue : March 2nd, 2008 at 11:22 AM. |
|
#2
|
||||
|
||||
|
Yes, you've definitely got a problem using B like that; the counter is getting overwritten when you get the value from data2, and isn't saved or restored anywhere. Your options are to either push the counter onto the stack while performing the compare, then restoring it for the loop indexing, or else use a memory compare rather than loading the value at the data2 into B. The latter is probably the better alternative.
Code:
loop:
ldaa 1,x+ ;going through data1
cmpa 1,y+ ; compare A directly to data2
bne loop ; if not 0 branch back to loop
incb ; increment B
cmpb #100 ; check if end of array
bne loop
I'm not certain if this is right, as I don't know the processor that well, but from what I see in the instruction set, it should work. For the pointers, are you sure you want ldx/ldy and not leax/leay? If I am not mistaken, as it is written now, what you are loading into X and Y are the values held in the locations data1 and data1. If those are pointers, then that is correct; but if those are the actual arrays, as seems to be the case, then you want to load their effective addresses instead. Since you don't show the two strings, I'm not sure which is the case.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF #define KINSEY (rand() % 7) λ Scheme is the Red Pill Scheme in Short • Understanding the C/C++ Preprocessor Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1 FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov Last edited by Schol-R-LEA : March 2nd, 2008 at 01:01 PM. |
|
#3
|
|||
|
|||
|
Thank you Schol-R-LEA,
I believe it should work too. I was not aware that I can use the indexed addressing with cmpa instruction. You are right for Code:
ldx data1 ldy data2 It should be: Code:
ldx #data1 ldy #data2 This is how we usually initialize pointers in the course. Thank you again B |
|
#4
|
||||
|
||||
|
Quote:
To be fair, that's the part I wasn't certain about, though if I am reading the documentation correctly, you can; like most CPUs with a tightly limited register file, the 6800 series (which the 68HCS12 is a descendant of - not, as someone recently claimed, the 68000, which is a completely different design) has a lot of memory-direct operations. Otherwise, you'd need to push B, load the test values and do the comparison, thern pop B for the loop. Neadless to say, you really would rather avoid that. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Use of registers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|