1

this doesn't work:

.global main
.type main%function

main:
        ldr r4,[r1]
        mov r1,r4
        add r1,r1,#4    // I go to the next argv
        ldr r0,=message
        b printf
message:
        .asciz "%s\n"

but output is:

$ ./a.out a b c
out

this works though:

.global main
.type main%function

main:
        ldr r4,[r1,#4]    // this works, if I write #4 HERE
        mov r1,r4
        //add r1,r1,#4    // commented
        ldr r0,=message
        b printf
message:
        .asciz "%s\n"

output:

$ ./a.out a b c
a

Also, I don't get how can it be possible that MOV can move memory addresses (32 bit) in another register (in the line mov r1,r4) if MOV can only move 8 bit of immediates/data...

Allexj
  • 1,375
  • 6
  • 14
  • 29
  • 1
    `r1` is address of the argument array, so `[r1]` is the first argument, i.e. the address of the string that contains the executable name (`"./a.out"`). Add 4 to that and you get the address of the string `"out"`. – Michael Dec 29 '20 at 15:25
  • 1
    Also, the reason why you can only use certain immediates is because the immediate needs to be encoded into the instruction word. And since the instruction word has a fixed size, there are only so many bits available to encode the immediate. When moving between registers, the value contained in the register at runtime is irrelevant to the instruction encoding. – Michael Dec 29 '20 at 15:28
  • 1
    The first thing is that you need to understand the argv data structure. Gaining this understanding by using assembly complicates things if you don't know the data structure and you're poking at bytes & words without type errors from a compiler, but at least you're skipping some of C's nuances regarding array parameters. See these to visualize the argv data structure: https://qph.fs.quoracdn.net/main-qimg-eefa7cf0507e8be8c4ef10c7d0f418e1.webp, and, https://stackoverflow.com/questions/7631282/pointer-to-pointer-with-argv. – Erik Eidt Dec 29 '20 at 16:56
  • 1
    If you also want to understand argv from perspective of C's type system, see here: https://stackoverflow.com/questions/779910/should-i-use-char-argv-or-char-argv; there's issues you don't see in assembly where pointers are essentially untyped (at least until they are used in a dereference). – Erik Eidt Dec 29 '20 at 16:57
  • thanks @Michael for the help :) – Allexj Dec 30 '20 at 16:48
  • also thanks to you @ErikEidt :) – Allexj Dec 30 '20 at 16:48

0 Answers0