ARM汇编语言菜单不起作用

时间:2014-10-27 00:50:49

标签: assembly arm

/*menu_prompt*/
.balign 4
menu_prompt: .asciz "Choose which problem.\nType 1 for Problem 1\nType 2 for Pr$
/*scanner*/
.balign 4
scan_pattern_int: .asciz "%d"
scan_read: .word 0
.balign 4
return: .word 0


.global main
 main:
    ldr r0, address_of_return
    str lr, [r1]

    ldr r0, address_of_menu_prompt
    bl printf

    ldr r0, address_of_scan_pattern_int
    ldr r1, address_of_scan_read
    bl scanf
    ldr r1, [r1]
    str lr, [r1]

    ldr r0, address_of_menu_prompt
    bl printf

    ldr r0, address_of_scan_pattern_int
    ldr r1, address_of_scan_read
    bl scanf
    ldr r1, [r1]
    cmp r1, #1
    beq in_prob_1
    cmp r1, #2
    beq in_prob_2
    cmp r1, #3
    beq in_prob_3
in_prob_1:
    mov r0, #100
    bal end
in_prob_2:
    mov r0, #200
    bal end
in_prob_3:
    mov r0, #300
    bal end
end:
    ldr lr, address_of_return 
    ldr lr, [lr]                     
    bx lr

address_of_menu_prompt: .word menu_prompt
address_of_scan_pattern_int: .word scan_pattern_int
address_of_scan_read: .word scan_read
address_of_return: .word return

运行程序后,无论我选择哪个输入(1,2或3)回显$?总是表明r0持有139,我不知道为什么。如果我选择1 r0应该保持100,2 r0应该保持200,3和r0应该保持300.

1 个答案:

答案 0 :(得分:1)

scanf并不需要保留r1的内容,因为您已按照ARM过程调用标准通过了该内容。您应该在分支到r1之后重新加载scanf