0

Disclaimer: I am very new to 32-bit ARM ASM code and this is my first program. When I try to run this program, the sum of the three integer values will be output wrong, however, the display of the three inputs is correctly printed, so I believe I am doing something wrong with my formatting for printf or using the wrong register, any help would be appreciated.

.section .data

prompt: .asciz "Hello, enter three integer values (Place a space in between each value): "

response: .asciz "You entered the numbers %d, %d, and %d and the sum is %d"
pattern: .asciz "%d %d %d"

input1: .word 0
input2: .word 0
input3: .word 0

.section .text
.global main
main:
    push {lr}

    ldr r4, =input1
    ldr r5, =input2
    ldr r6, =input3
    ldr r0, =prompt
    bl printf

    ldr r0, =pattern
    mov r1, r4
    mov r2, r5
    mov r3, r6
    bl scanf

    mov r1, r4
    ldr r1, [r1]
    mov r2, r5
    ldr r2, [r2]
    mov r3, r6
    ldr r3, [r3]

    add r7, r3, r1
    add r7, r7, r2
    ldr r0, =response
    mov r4, r7
    bl printf

    mov r0, #0
    pop {pc}

When I first got the error I attempted to change

    add r7, r3, r1
    add r7, r7, r2
    ldr r0, =response
    mov r4, r7
    bl printf

as

    add r7, r3, r1
    add r7, r7, r2
    mov r4, r7
        ldr r4, [r4]
        ldr r0, =response
    bl printf

But that didn't seem to work, if anything I believe it caused a segmentation fault.

BeefCake
  • 3
  • 2
  • 1
    Only 4 arguments are passed in registers `r0`-`r3` and one of those is the format string. You need to put the 4th number on the stack. Also, consult calling convention documentation. – Jester Nov 09 '22 at 17:00
  • In the standard calling convention, `r0..r3` are call-clobbered, so you can't expect them to still be pointing to the same place when scanf returns. You'll need to get a pointer to the buffer you scanned into and load; you could treat it like an array so you only need one base address. In your final attempt, you're adding addresses and then dereferencing, like in C `arr1[ (int)&arr2 ]` which of course doesn't make a valid address. Also like Jester said, check the calling convention docs, and/or have a compiler make an example for you on https://godbolt.org/ – Peter Cordes Nov 09 '22 at 17:01
  • More importantly `r4`-`r7` are callee saved so the values need to be saved and restored. – Jester Nov 09 '22 at 17:03

0 Answers0