Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

byte access in NASM

1,459 views
Skip to first unread message

James Yufeng Shao

unread,
Mar 28, 2013, 12:06:18 PM3/28/13
to
normally in nasm if we want to move some contents from register to memory we usually do something like this:

mov [memory_address + offset], eax

however, when I am planning to something with a char string stored in memory, it seems things will break

for example, if memory_address is something like
memory_address:
db "hello world"

and I want to change character ''o" to "a"
I might want to do something like this

mov eax, 97
mov [memory_address + 4], eax

It doesn't work.
So how could I specify that I only want to move certain byte of my register to memory?

Ondřej Lhoták

unread,
Mar 28, 2013, 5:04:09 PM3/28/13
to
eax is a 32-bit register.

To force the mov instruction to move only single byte, make it use an
8-bit register.

In addition to the 32-bit registers, the x86 instruction set has 16-bit
and 8-bit registers. The 16-bit registers are ax, bx, cx, dx, si, and
di. Note that they overlap with the 32-bit registers (they are the
low-order half of the corresponding 32-bit register), so, for example,
writing to eax will overwrite ax. The 8-bit registers are al, ah, bl,
bh, cl, ch, dl, ch. Again, these overlap with the 16-bit registers (they
are the low-order and high-order byte of the corresponding register).

Therefore, to force a move of a single byte, use:
mov al, 97
mov [memory_address + 4], al

Or, without using a register, you can use the "byte" prefix to tell nasm
to treat a value as a byte:
mov byte [memory_address + 4], 97
or
mov [memory_address + 4], byte 97

Ondřej

James Yufeng Shao

unread,
Mar 28, 2013, 6:07:10 PM3/28/13
to
thanks for the info. However the question I am dealing with involve a div operation. That means my integer (the dividend) is stored in edx, and it seems that nasm forbid moving content from bigger register(edx) to smaller register(al)

One trick I figured out to work is use the following command:
or [memory_address + 4], edx
(assuming edx contain integer less than 256)

but it just seems hacky..I am not sure if it is a good idea

James Yufeng Shao

unread,
Mar 28, 2013, 6:09:43 PM3/28/13
to
I mean my remainder is stored in edx

Ondřej Lhoták

unread,
Mar 28, 2013, 6:41:32 PM3/28/13
to
The 8 least significant bits of edx are also register dl.

You can use bit shift operations to get the specific bits of edx that
you want into the 8 least significant bits.
0 new messages