user4487002
user4487002

Reputation:

How do I calculate string length from command line in ARM?

I'm trying to get the length of a string passed in through the command line in ARM - the following code works but then returns a core dumped error:

.data
.balign 4
string_length: .asciz "String length is %d \n"

.text
.balign 4
.global main

main:
PUSH {r4-r8,lr}

MOV r4, r0
MOV r5, r1

MOV r6, #0
LDR r7, [r5, #4]

countSL:
        LDRB r0, [r7], #1
        CMP r0, #0
        ADDNE r6, r6, #1
        BNE countSL
LDR r0, address_of_string_length
MOV r1, r6
BL printf

address_of_string_length: .word string_length

What am I doing wrong?

Upvotes: 0

Views: 1448

Answers (1)

cooperised
cooperised

Reputation: 2599

Your are missing a return instruction at the end of main, so at the end of your function the CPU is just continuing to execute whatever is next in memory. There's no way to predict what effect this will have, but it's never going to be good!

You are also not popping the registers that you pushed, and you can do this at the same time as returning by popping the pushed value of lr directly into pc:

POP {r4-r8,pc}

Upvotes: 1

Related Questions