Comparing Characters (ARM Assembly)

泄露秘密 提交于 2021-01-29 04:28:30

问题


I'm trying to compare a Character from a Char-Array with a char in my assembly code.

This is the C-Code I use to start the assembly code:

char a[] = "abc";
char b = 'a'; 
int size = 3;

int d = _asm_main(a);
printf("Char a: %s\n",a);
printf("Erg:%d\n",d);

and this is the assembly code:

_asm_main:

push {r6,r7,r8,lr}

mov r8,r0

ldr r7,[r8,#2]
mov r6,r7
b compare

compare:

cmp r6,#'c'
beq true
b false

true:

mov r0,#1
b end

false:

mov r0,#2
b end

end: 

pop {r6,r7,r8,pc}

BX lr

It works for 'c' but if I try it with 'a' or 'b' I always get into the false lable. I don't get why it works for one of the three and not for the other two.


回答1:


A crude illustration using ldrb to deal with the ASCII byte and gdb debugger.

.data
        array:          .string "abc"
.text
        .global _start
_start:
        nop
        ldr r0,=array
        ldrb r1, [r0,#0]
        ldrb r2, [r0,#1]
        ldrb r3, [r0,#2]
...

gdb:

16              ldr r0,=array
(gdb) si
17              ldrb r1, [r0,#0]
(gdb)
18              ldrb r2, [r0,#1]
(gdb)
19              ldrb r3, [r0,#2]
(gdb)
_exit () at stuff.s:25
25              mov r7, #1
(gdb) i r
r0             0x20094  131220
r1             0x61     97
r2             0x62     98
r3             0x63     99


来源:https://stackoverflow.com/questions/41808072/comparing-characters-arm-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!