大会,比较两个字符 [英] Assembly, comparing two chars
问题描述
我坚持这个问题:
我想比较两个char他们知道哪一个先来按字母顺序(ASCII位置)。所以我做这样的事情:
LOADBYTEA串,X;它加载在累加器A,从字符串在字节;位置x
CPA CHAR2,D;与第一个字符相比较它时,键盘输入进入
BRLT少;如果A比CHAR2较小,则转少的标签
BRGT越大;如果A大于CHAR2越大,则转更大的标签
的事情是它总是brench较少的标签,我是否输入: AZ
或 ZA
键盘输入。
的CPA(比较)函数使可变CHAR2的减法和蓄能器A.如果答案是否定的,则它会brench至少。因此,如果输入 Z A
,应该是7A-61 = 19,应该brench更大,但不会!
的Z
brench少像它应该。这就像答案永远是负面的,我不知道为什么...
感谢您的帮助!
沙里的char,D
沙里EOL,D
沙里CHAR2,Dldbytea炭,D
CPA CHAR2,D
brlt少
BRGT GT少:沙罗'1',我
停止GT:沙罗'2',我
停止焦炭:.block 1
CHAR2:.block 1
EOL:.block 1;成\\ n
。结束
的问题是, CPA
进行比较的话,不是字节,而你的 CHAR2
是一个单字节,然后由 EOL
。因此, CPA
将使用这两个组成单词为大端数。如果您检查,在模拟器作为我告诉你,你可以看到操作实际上是在 7A0A
或 610A
为以Z
和 A
,分别为。
由于没有 CPBYTEA
,你将不得不采取这种照顾自己。一个可能的解决方案是分配一个临时的字和零有延长你的性格,如:
沙里的char,D
沙里EOL,D
沙里CHAR2,Dldbytea炭,D
ldbytex CHAR2,D
STX TMP,D
CPA TMP,D
brlt少
BRGT GT少:沙罗'1',我
停止GT:沙罗'2',我
停止
焦炭:.block 1
CHAR2:.block 1
EOL:.block 1;成\\ n
TMP:.block 2
。结束
另外请注意,平等的情况下,将刚刚经历下降到少
标签以及
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
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.
这篇关于大会,比较两个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!