How to Write Data to a Specific Memory Address in x86 Assembly Language

x86 Assembly programming language

In this article, we show how to write data to a specific memory address in x86 assembly language.

So the 8086 processor, in particular, is a 16-bit processor.

16 bits is 2 bytes of data.

Each memory address can hold 2 bytes of data within it, being that the address is bits in length.

The register that we use in x86 for the 8086 processor is the bx register.

This register allows us to write to any memory address.

How this works is that we first move the memory address into the bx register using the mov operation. We then mov the data value that we want to write into this address into the same bx register, however, within, brackets. The brackets around the bx register signify that this is data we are writing to the address within the bx register.

One point to note is that we can write either a single byte of data to this memory address or we can write 2 bytes to this memory address.

We can write a single byte of data to the register using the byte keyword.

We can write 2 bytes of data to the register using the word keyword.

We show how to do all of this below.

So in the code below we want to write data to memory address, 0xFFEC

We write 0xAB to this memory address.

So in order to write data to a memory address, we first have to move that memory address into the bx register.

This has to be the bx register, because if you use another register such as the ax register, the code will produce an error and will not run.

We move the address, 0xFFEC, into the bx register. The 0x signifies that the number is hexadecimal, which is the native number format of the x86 language.

In order to write data now to this register, we use the mov operation again, but this time we place the bx register within brackets ([ ]). This signifies that we want to place this data within at the address specified for the bx register.

You now can emulate the code and if you go to memory and specify the address,0XFFEC, you will now see the value 0xAB in it. So we have successfully written data to the memory address we specified.

One thing to note is that when you are writing data to the bx register, it will always write to the 16 bits (2 bytes) unless you explicitly tell it something else.

For example, above, we write 0xAB as our data to the memory address.

0xAB is only a byte, but this operation actually writes 2 bytes to the memory address location.

In actuality, 0xAB00 is written to the register.

However, if you just wanted to write one byte to the register instead the full 2 bytes of the register, you would add the keyword, byte, before the write operation.

This is shown in the code below.

With the byte keyword specified before the [bx] register write operation, only one byte, the high byte of the register, is written to. The low byte register is unaffected. Whatever is in the register prior to the operation remains unchanged.

If you want to write to the entire register, you would do this adding in the keyword, word, before the [bx] register write operation.

This is shown in the code below.

So this is how to write data to a specific memory address in x86 assemby language.

Related Resources

HTML Comment Box is loading comments...