tozhan
tozhan

Reputation: 423

Program crashes on STRB instruction on ARMv6

While learning ARMv6 ASM (using raspberry pi) I was trying to implement a loop which would modify the contents of a string, however I cannot seem to store the modified byte back to memory. Using GDB and breaking at various points shows me all registry values are working perfectly until it comes to the STRB instruction. Then it crashes for some unknown reason. The loop should decrement all bytes by 1 in reverse order.

.text
.global _start
_start:
    /* Thumb mode */
    .code 32
    add     r6, pc, #1
    bx      r6

    .code 16
    mov     r4, #6
    mov     r0, pc
    add     r0, #16

loop:
    /*load, modify, store*/
    ldrb    r3, [r0]
    sub     r3, #1
    strb    r3, [r0] /*THIS IS BROKEN*/

    sub     r0, #1
    sub     r4, r4, #1
    bne     loop

    bx lr

.data
string:
    asciz "HiThere"

The STRB instruction seems to crash the program, I am using an old book to learn from, am I missing something obvious here?

EDIT: yes I know that isnt the best way to load r0 but it works...i guess it could use a label in future. EDIT2: Sorry I know my question was badly written, Ive included more code for clarity. Still crashes on STRB when stepping through in gdb.

Upvotes: 0

Views: 409

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61351

EDIT: rather than increasing r0, your loop decreases it. So, rather than scrolling through the string, you are scrolling up straight into your code, overwriting instructions with less-by-one byte values. Once you overwrite the strb instruction, SIGILL happens.

Replace

sub     r0, #1

with

add     r0, #1

PREVIOUS: I see r0 being initialized to a memory location in the code section, past pc. Is that a writable memory block? I'm not so sure.

Place the string in the data section instead.


EDIT: there's another thing; your loop has no exit condition. SUB doesn't set flags unless told to; so replace

sub     r4, r4, #1

with

subs     r4, r4, #1

Upvotes: 3

Related Questions