【发布时间】:2016-03-03 20:33:53
【问题描述】:
我正在尝试为我的装配课做作业,老师的例子有一行
movl (%eax),%ebx
我的代码使用了完全相同的行,但是每当 gdb 到达它时,我就会遇到段错误。我不明白为什么。请帮我。这是完整的代码,在 32 位 Linux 上通过 QEMU 运行。
.text
.byte 12, 0x12, 012
.word 34, 0x34, 034
L1:
.long 23, 0x23, 023
.global _start
_start:
# do not change, remove or add anything other than specifying the underscores
movl $0x1700121C,%eax #hex for 385880604
movl (%eax),%ebx #moving contents of eax into ebx, long to long
movw 0x17,%bx #attemtping to move via memory location (probably buggy)
movb $0x12,%bh
# at this point, %ebx should have the value of 385880604
checkHere:
movl $1,%eax
movl $0,%ebx
int $0x80
请帮忙。感谢您的宝贵时间。
【问题讨论】:
-
movl (%eax),%ebx将%eax中的任何内容视为指针,并检索该地址的 32 位值并将其放入%ebx。如果0x1700121C不是一个有效的指针(至少看起来不是),它将出现段错误。 -
但是我们在上面一行中得到的代码是 movl $____,%eax 这不是说它必须是一个常数吗?
-
是的,
$意味着后面会跟着一个常量,但通常当涉及到指针时,它将是标签的名称。所以它将是$label,其中label是您想要其地址(指针)的标签的名称。 -
那我应该用 L1 替换 0x1700121C,你觉得呢?
-
我没有完整的问题,所以知道 L1 是否正确我无法回答,但
$L1将是有效的语法。如果我们有完整的问题,那就更容易回答了。
标签: assembly x86 computer-science cpu-registers