0

I'm stuck with this problem:

I want to compare two char with their to know which one come first in alphabetic order (ascii position). So I do something like this:

LOADBYTEA  string,x; it loads in the accumulator A, a byte from the string at                                        ;                   position x
CPA  char2,d       ;compare it with the first char enter with keybord input
BRLT   less        ;if A is lesser than char2, then goto less label
BRGT   greater     ;if A is greater than char2, then goto greater label

the thing is it always brench to less label, whether I enter : a z or z a in keyboard input..

The CPA (compare) function make a substraction of the variable char2 and the accumulator A. If the answer is negative, then it'll brench to less. So if a enter z a, it should be 7A-61=19 and should brench to greater but doesn't!

a z brench to less like it should. It's like the answer is always negative I don't know why...

Thanks for help!

chari char,d
chari EOL,d
chari char2,d

ldbytea char,d
cpa char2,d 
brlt less
brgt gt

less:charo '1',i
stop

gt:charo '2',i 
stop





char:.block 1
char2:.block 1
EOL:.block 1 ;the \n
.end 
Magnus Karlsson
  • 3,549
  • 3
  • 31
  • 57
Gregory
  • 19
  • 1
  • 8
  • Use the simulator to verify at the `CPA` instruction the value of `A` and `char2`. Also, provide full code, see [MCVE](http://stackoverflow.com/help/mcve). – Jester Jul 25 '15 at 15:48
  • With the debugger is see the value of A and char2 which are good! I'm not sure its a good idea to provide the full code because first its 400 lines and it's written in french so you won't understand :s – Gregory Jul 25 '15 at 15:50

1 Answers1

0

The problem is that CPA compares words, not bytes, and your char2 is a single byte, followed by the EOL. So the CPA will use the word made up from those two as a big endian number. If you examine that in the simulator as I told you, you can see that the operand is in fact 7A0A or 610A for z and a, respectively.

Since there is no CPBYTEA, you will have to take care of this yourself. A possible solution is to allocate a temporary word and zero extend your character there, such as:

chari char,d
chari EOL,d
chari char2,d

ldbytea char,d
ldbytex char2, d
stx tmp, d
cpa tmp, d
brlt less
brgt gt

less:charo '1',i
stop

gt:charo '2',i
stop


char:.block 1
char2:.block 1
EOL:.block 1 ;the \n
tmp: .block 2
.end

Also note that the equal case will just fall through to the less label as well.

Jester
  • 56,577
  • 4
  • 81
  • 125