0

What does the symbol '=' mean in this program?

.data
    var1:
        .word 1
    var2:
        .word 1

    scan_format:
        .asciz "%d %d"

    print_format:
        .asciz "%d\n"

.text
.global main
    main:
        sub sp, sp, 16
        str x30, [sp, 8]

        ldr    x0, =scan_format
        ldr    x1, =var1
        ldr    x2, =var2
        bl scanf
        ldr    x1, =var1
        ldr    x2, =var2
        ldr    x1, [x1]
        ldr    x2, [x2]
        add    x3, x1, x2

        ldr    x0, =print_format    
        mov    x1, x3
        bl     printf
        ldr x30, [sp, 8]
        add sp, sp, 16

        sub x0, x0, x0
        ret
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
AshGi
  • 13
  • 3
  • 4
    Does this answer your question? [What is the difference between =label (equals sign) and \[label\] (brackets) in ARMv6 assembly?](https://stackoverflow.com/questions/17214962/what-is-the-difference-between-label-equals-sign-and-label-brackets-in-ar) – tolik518 Nov 08 '21 at 13:43
  • Note that most programs should use `adrp` and friends instead, see https://stackoverflow.com/questions/41906688/what-are-the-semantics-of-adrp-and-adrl-instructions-in-arm-assembly. It saves code size and provides position independence for free. – Nate Eldredge Nov 08 '21 at 13:59
  • By the way, I'm not sure what your purpose is in studying this program, but it has quite a lot of missed optimizations, and at least one actual bug. Finding them might be a good experience in learning the instruction set! – Nate Eldredge Nov 08 '21 at 14:45
  • @NateEldredge I think .string can be used instead of .asciz here – AshGi Nov 09 '21 at 13:48
  • @AshGi: That's true, but it doesn't make any difference. They both do exactly the same thing: emit the specified characters followed by a zero byte. That's not one of the issues I had in mind. – Nate Eldredge Nov 09 '21 at 14:16
  • @NateEldredge can still be improved like this: sub sp, sp, 4 \n str x30, [sp, 4] ... ldr x30, [sp, 4] \n add sp, sp, 4 – AshGi Nov 09 '21 at 15:41
  • @AshGi: No, that won't work, because the stack pointer [must remain aligned to 16 bytes on most systems](https://stackoverflow.com/a/63003387/634919). And also remember that the `x` registers are 64 bits, which is 8 bytes, not 4. (Which is related to the bug in the original code...) – Nate Eldredge Nov 09 '21 at 15:57
  • @AshGi: The other things I had in mind were: (1) you can avoid needing separate `add/sub` to adjust the stack pointer if you use the pre/post-increment addressing modes on your load/store; (2) the code could be much shorter if you use stack memory for your two variables, instead of static `.data` memory; (3) if the two variables are adjacent, you can load both in one instruction with `ldp`; (4) `mov x1, x3` would not be necessary if you had written `add x1, x1, x2` instead of using `x3`; – Nate Eldredge Nov 09 '21 at 17:36
  • (5) `sub x0, x0, x0` looks like an x86-ism; on ARM64, the simpler alternatives of `mov x0, 0` or `mov x0, xzr` are also more efficient. – Nate Eldredge Nov 09 '21 at 17:36

0 Answers0