32-bit ARM ASM code supposed to print the sum of three integers, instead will print a very low negative number

152 views Asked by At

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.

0

There are 0 answers