Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

WORKING WITH BYTE AND WORD DATA IN ASSEMBLY

Now in todays topic we will try to understand a very important concept about
Byte And Word Type of data.
First of all, you should know that while programming in Assembly language we need
to use each and every bit and byte very carefully. In Assembly we have access over
the hardware and we can use it to take advantage of even each and every bit. Now
let me write a code:

section .data
integer DB 1
integer2 DB 2

section .text
global _start

_start:
MOV ebx, [integer]
MOV ecx, [integer2]
MOV eax, 1
INT 80h

Now here in this code you can understand what is actually happening, but here
is the logic, that is we
created two variables and passed data into that, than I simply passed that data
into the registers. Now I am also expecting like you that it should push 1 in ebx
and 2 in edx and by looking at the both registers they should have their
corresponding values. But here is the twist.
When I run my code and started the debugger, I got two addresses lying with
each other side by side as
0x00004 and 0x00005, something like this, now you can see that both are at the same
place in the memory, but as I ran my code and I checked for the info of ebx
register the output I got was 201(as 0x201). Now what is happening. Why is this?
Well, the answer to that is that they both are lying side by side to each other in
the memory and due to this reason since ebx is having 32 bits so while passing the
data in registers, both the values moved into the ebx register. Here 201 is 02 of
integer2 variable and 01 of integer variable. Now, this is answered, like it is due
to they are on the same position in the memory. But how to fix this problem?? Well,
the answer to this question can be answered through concept called Low Endian and
High Endian. Now what are these Low Endian And High Endian?? Well, Let me
elaborate.
We know that eax, ebx, ecx and edx are 32 bit registers (extended registers)
Now if we work in 16 bits
we will use ax, bx, cx and dx instead of eax etc. Now what if we start working in 8
bit, well in that case we will use a, b, c and d instead of all others. Now, let
suppose we are working in 16 bit assembly, in that case we would use registers as
ax, bx etc but here passing the data in registers will make a problem like if we
want to pass 8 bits data instead of 16 bit data, what would we do is that we would
use registers in some way like this, al, ah, bl, bh, cl, ch, dl, and dh. Now here
if we are accessing 8 bits, than we will also need to specify that which 8 bit part
of 16 bits we want to access, either the 8 bits at left or right since both have
different powers. So if we use al, it means we want to access the Low Endian part
of the 16 bits and if we use ah, it means we want to access High Endian part of 16
bits. Low Endian part is the 8 bits at the Right and High Endian part is the 8 bits
at the Left. This representation is something like this:
High Endian Low Endian
--------- ---------
| | | |
0000 0000 0000 0000
| |
-----------------------------
16 Bit Such as (bx)

Now to fix the above problem we will pass one variable to the Low Endian part
of a register and the
other variable to Low Endian part of another register. So will need to write the
program as:

GNU nano 8.0 firstFile.s


section .data
integer DB 1
integer2 DB 2

section .text
global _start

_start:
MOV bl, [integer]
MOV cl, [integer2]
MOV eax, 1
INT 80h

Now here you can see that we passed the data to the Low Endians of the
Registers. Now if we run our
code it will give us our desired output. But here another problem arises, that is
what if we pass data to high Endian. Than the code will become as:

section .data
integer DB 1
integer2 DB 2

section .text
global _start

_start:
MOV bh, [integer]
MOV ch, [integer2]
MOV eax, 1
INT 80h

Now here if I run this code and by fetching the info about bh and ch registers
it will give me our
desired asnwers but if we type as ebx and ecx than ebx will return 256 and ecx will
give me 512. Why? Well, it is because of the same thing that is Low Endian and High
Endian. Please Check out the below figure:

FOR EBX OR BX

High Endian Low Endian


--------- ---------
| | | |
0000 0001 0000 0000
| |
-----------------------------
16 Bit Such as (bx)

FOR ECX OR CX

High Endian Low Endian


--------- ---------
| | | |
0000 0010 0000 0000
| |
-----------------------------
16 Bit Such as (cx)

Since we have values 1 in bx so I entered the binary of the 1 in the high


endian of the bx register now
from the bh point of view indeed it is 1 but from the ebx point of the 1 has the
power of 9 or if we convert this binary "100000000" to decimal we will get 256.
Similarly for cx register. I typed 2 in ch whose binary is 0010. According to the
point of view of ch, indeed it is 2 but according to the point of ecx its power is
10 and if we convert this binary "1000000000" to decimal we will get 512. So this
is how it works, this concept was really important and i delivered it successfully.

You might also like