7

I'm working through Computer Systems, A Programmer's Perspective (3rd edition), and Practice Problem 3.3 contains the following line:

movb $0xF, (%ebx)

I'm supposed to find out what's wrong with this line of x86-64 assembly, and the answer key states: "Cannot use %ebx as address register", which doesn't make sense to me. My understanding is that this line intends to copy 0xF to a location in main memory, however %ebx is a 32-bit register, memory addresses are 64 bits wide on 64-bit machines, and so %ebx cannot hold a memory address, therefore it cannot be dereferenced (dereferencing is what the parentheses around %ebx represent, correct?). However, looking a few pages back in the book (page 183, if you have it) there is an example detailing the five mov operand--destination combinations, one of which is:

movb $-17, (%esp)         Immediate--Memory, 1 byte

%esp is a 32-bit register just like %ebx! And this example shows a byte value being moved to a dereferenced 32-bit register! Which doesn't make sense to me, because how can %esp contain a 64-bit address? Do I completely misunderstand assembly?

4

2 回答 2

6

你是对的,

movb $-17, (%esp)         Immediate--Memory, 1 byte

不应该被允许。事实上,作者已将此作为错字发布。查看他们的勘误表(Ctrl-F 表示“p. 183”)。

于 2017-10-11T03:33:45.993 回答
2

对于 64 位 x86;指令没有错movb $0x0F, (%ebx)。它组装到 0x67、0xC6、0x03、0x0F。

书错了。

请注意,所有指令都可能是错误(简单示例:add在您想使用时使用sub),并且movb $0x0F, (%ebx)可能是错误(例如,值可能应该是0xFF,也许应该使用不同的寄存器,也许应该使用rbx,也许它应该是一个lea,..)。这并不意味着它总是一个错误(例如,32 位地址是完全合法的,有时在 64 位代码中是可取的)。

于 2019-02-08T00:05:45.597 回答