Search code examples
linuxassemblygccarm

ARM Assembly loading string produces weird terminal output


I am trying to space out my string by replacing each character with a space string combined with an underscore string to create _ _ _ _.

However, I get a weird output:
Terminal Output

If I load 1 character e.g ldr r1, = '_'
it works however I get _____ and I am trying to get _ _ _ _ _. What is the best way to do this?

NOTE: I do not know C and I am new to ARM.

My function:

sub r3, r0, #1 @has the length
    ldr r0, = buffer @has the word

    mov r5, #0 @start of increment
    mov r6, r0 @copies word to r6
loop:
     ldr r1, =spaceChar
     strb r1, [r6, r5]
     add r5, r5, #1
     cmp r3, r5
     bne loop
    mov r1,r6 
    ldr r0, = HiddenWord
    bl printf
    pop {r4,lr}
    bx lr
 
.data
 
    HiddenWord: 
        .asciz "Word: %s"
    spaceChar:
        .asciz " _"
    buffer:
        .space 100

Solution

  • Since you already statically allocate the buffer the simplest solution is to preset it with " _" using your assembler and just put the terminating zero in the right place. I don't think you mentioned which assembler you use, the following works in gnu assembler:

        ldr r0, =buffer-1
        mov r1, #0
        strb r1, [r0, r3, lsl #1]
    
        ldr r0, =HiddenWord
        ldr r1, =buffer
        bl printf
        pop {r4,lr}
        bx lr
     
    .data
     
        HiddenWord: 
            .asciz "Word: %s\n"
        buffer:
    .rept 50
        .ascii "_ "
    .endr
    

    If you want to fill the buffer programmatically, that could look like:

        ldr r0, =buffer @has the word
        ldr r1, =0x205f @ underscore + space
    loop:
        strh r1, [r0], #2
        subs r3, r3, #1
        bne loop
        mov r1, #0         @ replace final
        strb r1, [r0, #-1] @ space with zero