printf(程序集)声明变量的顺序

时间:2015-03-06 01:04:12

标签: assembly x86 gas att

这是我的工作代码:

.section .data
prompt:
    .asciz "Please input value:\n"
input:
    .asciz "%d"
output:
    .asciz "output: %d\n"
integer:
    .int
.section .text
.globl  main
main:
    nop

    pushl $prompt
    call printf
    addl $8, %esp

    pushl $integer
    pushl $input
    call scanf
    addl $8, %esp

    movl integer, %ecx
    pushl %ecx
    pushl $output
    call printf
    add $8, %esp

    pushl $0
    call exit

为什么我改变了以下的顺序:

input:
    .asciz "%d"
output:
    .asciz "output: %d\n"
integer:
    .int

到(integer高于其input

integer:
    .int
input:
    .asciz "%d"
output:
    .asciz "output: %d\n"

...然后它不再打印出您扫描的整数?是因为我们首先引用$ integer,当我们将它推入堆栈时?

1 个答案:

答案 0 :(得分:1)

您需要为.int实际指定一个值,否则它将不会执行任何操作。所以在第二种情况下,你的整数(4个字节)也与inputoutput的开头重叠。假设您输入的数字在最高有效字节中为零(即任何小于2^24的非负数),您将有效地将输出字符串截断为零长度。

要解决此问题,您只需执行.int 0或在.bss部分中声明您的整数,例如使用.lcomm integer, 4