【发布时间】:2017-04-22 22:34:06
【问题描述】:
所以我有一个任务要做,这需要我在汇编中使用 scanf 和 char*。我试过这段代码:
.data
INPUT_STRING: .string "Give me a string: "
SCANF_STRING: .string "%s"
PRINTF_STRING: .string "String: %s\n"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $32, %esp
pushl $INPUT_STRING
call printf #printf("Give me a string: ")
addl $4, %esp
pushl -12(%ebp) # char*
pushl $SCANF_STRING # "%s"
call scanf scanf("%s", char*)
addl $8, %esp
pushl -12(%ebp)
pushl PRINTF_STRING
call printf #printf("String: %s\n")
addl $16, %esp
movl -4(%ebp), %ecx
xorl %eax, %eax
leave
leal -4(%ecx), %esp
ret
它首先正确地写下 printf,然后等待输入(所以 scanf 有效),然后当我输入任何内容时 -> Segmentation fault。
我知道,char* 应该以某种方式初始化,但我怎样才能从汇编级别做到这一点?
我在 Manjaro 64 位上编译它,gcc -m32
【问题讨论】: