我正在使用GNU Assembler“as”学习使用Assembly x86编程。我正在尝试编写一个简单的程序,首先要求用户输入(一位数字),递增该数字,然后将其打印出来。
这就是我的尝试:
# AT&T Syntax used.
inout: # subroutine: inout
movq $0, %rsi
movq $request, %rdi
call printf # Prints: "Enter a number please"
movq %rsp, %rbp
subq $8, %rsp
leaq -8(%rbp), %rsi
movq $formatstr, %rdi
movq $0, %rax
call scanf # Scan for input
movq -8(%rbp), %rax # copy the number from the stack to a register
incq %rax # increment it
movq %rbp, %rsp
popq %rbp # reset the stack
movq $0, %rsi
movq %rax, %rbx
movq $result, %rcx
call printf # print the string: "The result is: %d\n"
ret
.text # variables
string: .asciz "inout\n"
request: .asciz "Enter a number please\n"
formatstr: .asciz "%1d"
result: .asciz "The result is: %d\n"
.global main # Subrountine : main
main:
movq $0, %rax
movq $string, %rdi
call printf # print string "inout"
call inout
end: # end of program
mov $0, %rdi
call exit
然后我使用以下两个命令编译并运行:
gcc -o inout.o inout.s -no-pie
./inout.o
我打印前两行,输入一个数字(1),然后我得到分段错误:
"inout
Enter a numbers please"
1
Segmentation fault
你能检查一下我做错了吗?
提前致谢。