Is this piece of assembly code invalid?

梦想的初衷 提交于 2020-02-08 06:40:05

问题


I'm trying to figure out whether the following piece of assembly code is invalid.

movb $0xF, (%bl)

Is it invalid? If so, why? Thanks.


回答1:


You don't say what processor. bl is a 8-bit register at least in x86 processors, but it cannot be used for addressing.

Why is it invalid instruction? Well, the reason an assembly instruction is invalid is that there's no such instruction for the given processor. There is no possible way to encode this instruction. In this case (assuming x86), using bl or any other 8-bit register neither for addressing has not been considered necessary. In 16-bit code only 16-bit registers bx, bp, si and di can be used for memory addressing. Wikipedia has a useful list of all possible addressing modes (please do note it's using Intel syntax, your code is in AT&T syntax).

Edit: In AT&T syntax the letter b in movb defines it deals with an 8-bit operand.

To obtain more or less your goal (to use bl for addressing), you could do one of these (these are in Intel YASM/NASM syntax, MASM-style assemblers including GNU .intel_syntax noprefix want byte ptr):

For 16-bit code:

xor   bh,bh
mov   byte [bx], 0x0f

For 32-bit code:

movzx  ebx,bl
mov    byte [ebx], 0x0f

For 64-bit code:

movzx  ebx,bl               ; with implicit zero-extension to 64-bit
mov    byte [rbx], 0x0f

It's rare that you ever want to store anything to a linear address from 0..255 (one byte). In 64-bit mode where segmentation is mostly disabled so the DS base is fixed at 0, this is definitely what the instruction is doing, but especially in 16-bit mode, the DS base could be non-zero.




回答2:


This question is probably from the CSAPP book, Practice Problem 3.3. You cannot use a 8 bit register (%bl) as address register on AT&T assembly (I'm not sure about Intel's). Moreover it is not possible to determine whether $0xF is a 8, 16 or 32 bit value.



来源:https://stackoverflow.com/questions/14494285/is-this-piece-of-assembly-code-invalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!