我正在解决涉及创建小编译器的任务。我的编译器为简单赋值语句生成以下汇编代码
// global declaration
int k;
float x,y,z;
// inside main func
k=10;
x = y = k;
汇编代码(我' m 获取分段错误(核心转储):
# 19
# b_push_ext_addr (x)
subl $8, %esp
movl $x, (%esp)
# b_push_ext_addr (y)
subl $8, %esp
movl $y, (%esp)
# b_push_ext_addr (k)
subl $8, %esp
movl $k, (%esp)
# b_deref (signed int)
movl (%esp), %eax
# Proplm on next Line ... why?
movl (%eax), %edx
movl %edx, (%esp)
# b_convert (signed int -> float)
fildl (%esp)
fstps (%esp)
# b_assign (float)
movl (%esp), %edx
addl $8, %esp
movl (%esp), %eax
movl %edx, (%eax)
movl %edx, (%esp)
# b_deref (float)
movl (%esp), %eax
movl (%eax), %edx
movl %edx, (%esp)
# b_convert (float -> double)
flds (%esp)
fstpl (%esp)
# b_convert (double -> float)
fldl (%esp)
fstps (%esp)
# b_assign (float)
movl (%esp), %edx
addl $8, %esp
movl (%esp), %eax
movl %edx, (%eax)
movl %edx, (%esp)
# b_pop ()
addl $8, %esp
当我使用gdb时:它指向这一行:
(gdb) x/i $eip
=> 0x80485bd <main+336>: mov (%eax),%edx
答案 0 :(得分:2)
实际的错误是在类似的指令下进一步向下:
fstps (%esp) # stack has some float
# b_assign (float)
movl (%esp), %edx # edx has a float now
addl $8, %esp
movl (%esp), %eax
movl %edx, (%eax)
movl %edx, (%esp) # write the float onto stack
# b_deref (float)
movl (%esp), %eax # load the float from stack
movl (%eax), %edx # try to use float as pointer => segfault