NASM offset and 8-bit memory offset

The Intel Software Development Guide (referred to as ISDM in this post) and the x86 Instruction Set Link (which I suppose is we know that the mov command can move data from eax/ax/al to a memory offset and vice versa.

For example, mov moffs8, al moves the contents of register al to some 8-bit memory offset moffs8 .

Now what is moffs8 ? Quoting ISDM (3.1.1.3):

moffs8, moffs16, moffs32, moffs64 . A simple memory variable (memory offset) such as a byte, word, or double word used by some variations of the MOV instruction. The actual address is set by a simple offset relative to the base of the segment. The instruction does not use the ModR / M byte. The number shown with moffs indicates its size, which is determined by the size attribute of the instruction address.

I emphasized suggestions saying that moffs8 is a byte type and has a size of 8 bits.

I am new to assembly, so right after reading this, I started playing with the instruction mov moffs8, al using NASM. Here is the code I wrote:

 ; File name: mov_8_bit_al.s USE32 section .text mov BYTE [data], al section .bss data resb 2 

This is what nasm -f bin mov_8_bit_al.s producing (in hexadecimal format):

 A2 08 00 00 00 

Here is how I understand it:

  • A2 is the mov moffs8, al for mov moffs8, al
  • 08 is a 1 byte memory offset itself
  • 00 00 00 is trash

08 00 00 00 seems to be a memory offset, but in this case it is a moffs32 , not moffs8 ! Thus, the CPU will read only one byte, executing A2 , and treat 00 as an ADD instruction or something else that is not intended.

At the moment, it seems to me that NASM is generating the wrong byte code here, but I think it's me who misunderstood something ... Maybe NASM should not be IDSM? If so, its code will not execute correctly on Intel processors, so it should follow it!

Could you explain where I am wrong?

+5
source share
1 answer

The size suffix after moffs actually refers to the size of the operand, not the size of the address itself. This reflects the meaning of the size suffix after r/m .

The manual actually speaks of this in a note:

NOTES:
* Operators moffs8, moffs16, moffs32 and moffs64 set a simple offset relative to the base of the segment, where 8, 16, 32 and 64 refer to the size of the data. The command address size attribute defines the offset size, either 16, 32, or 64 bits.

+7
source

Source: https://habr.com/ru/post/1271908/


All Articles