scanf()函数如何在汇编代码中工作?

时间:2014-03-16 20:57:10

标签: assembly scanf sparc

我是汇编编码的新手,我正在尝试弄清楚scanf函数的工作原理。我只想取用户输入的整数并向其添加5并打印出来。我无法让它工作,这是我到目前为止所做的。我试着单独打印用户输入,但仍然无法使用它。

有人可以向我解释这个过程吗?

.section ".data"
prompt: .asciz "\nEnter a number: "
format: .asciz "%d"
format2: .asciz "Your number plus 5 is: %d"
input: .word 0
nl: .asciz "\n"


define(a1, 5)

.align 4
.section ".text"
.global main
main:
save %sp, -96, %sp



!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!  Prompt the user for a number
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

set   prompt, %o0            !point o0 to the prompt
call  printf                 !call printf to print the prompt
nop

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!  uses scanf to store the integer in register o1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

set   format, %o0            !address of the format
set   input, %o1             !address of the location for the max
set   nl, %o2
call  scanf                  !reads user input, coverts to a
nop                          !number and stores at the memory referenced by input


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!Prints out the integer plus 5
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

set   format2, %o0
set   input, %o1
ld    [%o1], %o1             
add   %o1, %a1, %o1           
call   printf
nop


ret
restore

1 个答案:

答案 0 :(得分:0)

define(a1, 5)最有可能进行简单的文字替换,因此add %o1, %a1, %o1将汇总为add %o1, %5, %o1%5将解析为%g5注册号码为add %o1, 5, %o1而不是你想要的。解决方案:只需使用%或为常量提供一些其他符号名称,并使用不带scanf符号的名称。其余的看起来是对的,除了你传递给set nl, %o2({{1}})的额外但无害的论点。

提示:说" 紧急"没有得到任何更快的回复,但提供更多细节,例如确切的问题,可能。