Assembly 如何用汇编语言打印两个变量?
对于我的程序,它会要求用户输入一个正数,然后进行双阶乘计算并打印出结果。然而,我目前拥有的是,当我输入5时,输出将打印为“Result=15”。我想将输出打印为“5=15的结果”。我怎么做Assembly 如何用汇编语言打印两个变量?,assembly,arm,Assembly,Arm,对于我的程序,它会要求用户输入一个正数,然后进行双阶乘计算并打印出结果。然而,我目前拥有的是,当我输入5时,输出将打印为“Result=15”。我想将输出打印为“5=15的结果”。我怎么做 在r2中,像在C中一样,将第三个参数传递给printf(当然,要匹配格式字符串)。并将格式字符串更改为“Result of%d=%d\n”,这意味着在输出下,在BL printf之前添加“LDR R2,addr\u format”,然后添加“LDR R2,[R2]”?您的意思肯定是添加迭代次数?而迭代次数值
在r2中,像在C中一样,将第三个参数传递给printf(当然,要匹配格式字符串)。并将格式字符串更改为
“Result of%d=%d\n”
,这意味着在输出下,在BL printf之前添加“LDR R2,addr\u format”,然后添加“LDR R2,[R2]”?您的意思肯定是添加迭代次数
?而迭代次数
值应进入R1
,而产品
值应进入R2
。因为这是你想要打印它们的顺序。当我把它改为添加_迭代时,我得到了-1,而当我键入5时,我得到了-5
.global main @ start of the assembly code
main:
PUSH {LR}
BL _getInput
BL _calculate
BL _output
POP {PC}
_getInput:
PUSH {LR}
BL _displayPrompt
SUB SP, SP, #4 @ create word on stack
LDR R0, addr_format @ get the address of the the input entered by user
MOV R1, SP @ copy as parameter
BL scanf
LDR R1, [SP] @ load the data from memory address of SP into R1
LDR R2, addr_iterations @ load the address of iterations into R2
STR R1, [R2] @ store data from R1 into iterations
ADD SP, SP, #4
POP {PC}
_displayPrompt:
PUSH {LR}
LDR R0, =prompt
BL printf
POP {PC}
_calculate:
LDR R0, addr_product
LDR R0, [R0]
LDR R1, addr_iterations
LDR R1, [R1] @ Load the iteration counter
MUL R0, R0, R1 @ R0 *= R1 / product = product * counter
SUBS R1, R1, #2 @ R1-- / counter = counter - 1
LDR R2, addr_iterations @ write back
LDR R3, addr_product @ address of where to store the result
STR R0, [R3] @ store the final product back into addr_product
STR R1, [R2]
MOVLE PC, LR @ go back to main once the variable interations is 0 / return
BGT _calculate @ continue looping as long as not 0
_output:
PUSH {LR}
LDR R0, addr_message
LDR R1, addr_product
LDR R1, [R1]
BL printf
POP {PC}
addr_format: .word scanformat
addr_iterations: .word iterations
addr_product: .word product
addr_message: .word message
.data
prompt: .asciz "Enter a positive integer number: "
scanformat: .asciz "%d"
iterations: .word 1
product: .word 1
message: .asciz "Result: %d\n"