0
ユーザーに2つの整数の入力を求め、その2つの整数の合計を出力しようとしています。私は単一の整数nを出力する方法を考え出しましたが、2番目の整数mは印刷できません。ARMアセンブリで2つの数値を追加して印刷する
このコードを実行すると、nの値だけが出力されることを除いて、期待どおりに機能します。私はこのコードのビットはR2 R1の代わりに使用するように変更:
/* 1. Prompt the user to enter an integer
* 2. Read an integer from the keyboard into memory
* 3. Prompt the user to enter another integer
* 4. Read an integer from the keyboard into memory
* 5. Load the two integers into CPU registers
* 6. Add them together
* 7. Print the result
*/
.data
prompt: .asciz "Enter a number: " @ user prompt
.align 2
sformat:.asciz "%d" @ Format string for reading an int with scanf
.align 2
pformat:.asciz "The sum is: %d\n" @ Format string for printf
.align 2
n: .word 0 @ int n = 0
m: .word 0 @ int m = 0
.text
.global main
main: stmfd sp!, {lr} @ push lr onto stack
@ printf("Enter a number: ")
ldr r0, =prompt
bl printf
@ scanf("%d\0", &n)
ldr r0, =sformat @ load address of format string
ldr r1, =n @ load address of int n variable
bl scanf @ call scanf("%d", &n)
@ printf("Enter a number: ")
ldr r0, =prompt
bl printf
@ scanf("%d\0" &m)
ldr r0, =sformat @ load address of format string
ldr r2, =m @ load address of int m variable
bl scanf @ call scanf("%d", &m)
@ printf("You entered %d\n", n)
ldr r0, =pformat @ load address of format string
ldr r2, =m
ldr r2, [r2]
ldr r1, =n @ load address of int variable
ldr r1, [r1] @ load int variable
add r1, r2, r1
bl printf @ call printf("You entered %d\n", n)
ldmfd sp!, {lr} @ pop lr from stack
mov r0, #0 @ load return value
mov pc, lr @ return from main
.end
編集:ここでは
はステップや私のコードです@ scanf("%d\0" &m)
ldr r0, =sformat @ load address of format string
ldr r2, =m @ load address of int m variable
bl scanf @ call scanf("%d", &m)
これは動作しますが、私は理由を理解していません。
'scanf()'を初めて呼び出す前に、 'ldr r1、= n'を実行します。もう一度呼び出す前に、 'ldr r2、= m'を実行します。あなたは違いを見つけることができますか? – EOF
そうだと思います。私はr2にmのアドレスをロードしようとしているので、r1とr2を追加することができます。 編集:私はそれをr1に変更しましたが、今は動作しますが、理由はわかりません。 –
これは無関係です。関連する部分は 'scanf()'が ''%d "'に対応するポインタを期待しているところです。 – EOF