0

I have a program which can read the first argument (after program name) and print it. But how do I access the next argument in the list, and subsequent ones?

main:
        push {r4,r9, ip, lr}
        ldr r4, [r1, #4] // comes in as r1, need to save the location into R4                                                                                                                                                                 

        mov r1, r4
        ldr r0, =stringS
        bl printf
        mov r0, #0

// Tried various combinations trying to find how to move argv[x] to next location #4 bytes along
        //mov r1, r4                                                                                                                                                                                                                          
        //str r1, [r4, #8]                                                                                                                                                                                                                    
        ldr r1, [r4], #8
        ldr r0, =stringS
        bl printf

        mov r0, #0      // return 0                                                                                                                                                                                                           
        pop {r4, ip, pc}

What seemed like a trivial program seems to be kicking me in the teeth!

Rick Dearman
  • 356
  • 2
  • 12
  • Check `ldr r1, [r4, #8]` vs `ldr r1, [r4], #8` – Codo Nov 24 '21 at 22:25
  • 1
    The initial `ldr r4, [r1, #4]` is doing the equivalent of `r4 = argv[1]`. You can't recover the argv pointer itself from that, so you need to instead do something like `mov r4, r1`, i.e. `r4 = argv`. Then `ldr r1, [r4, #4]` does `r1 = argv[1]`, `ldr r1, [r4, #8]` does `r1 = argv[2]`, etc. Note that `ldr r1, [r4], #8` is post-increment (look it up) and is not what you want. – Nate Eldredge Nov 25 '21 at 00:44
  • So basically, you are mixing up levels of indirection: the distinction between the value of a pointer, and the value of the thing pointed to: `p` versus `*p`. – Nate Eldredge Nov 25 '21 at 00:45
  • 1
    `r1` is call-clobbered; you'll need to copy argv itself to a call-preserved register so you can call printf in a loop and increment it until hitting a NULL pointer. Try writing the function in C and look at compiler output: https://godbolt.org/z/4eEbf57eh – Peter Cordes Nov 25 '21 at 02:08
  • Thanks Nate Eldredge that was the answer, if you write it up as an answer I'll flag it as the answer. @PeterCordes that is a really good site! Thanks for the link. – Rick Dearman Nov 25 '21 at 12:43

0 Answers0