In: Computer Science
Explain EAX, EBX, ECX, and EDX with examples. It is okay to take
an example as a real case.
(1 or more in a4)
PLEASE UPVOTE IF THIS ANSWER SEEMS HELPFUL AS IT GIVES THE CONFIDENCE TO HELP MORE STUDENTS
THANKYOU
Data Registers
Four 32-bit data registers are used for arithmetic, logical, and other operations. These 32-bit registers can be used in three ways −
As complete 32-bit data registers: EAX, EBX, ECX, EDX.
Lower halves of the 32-bit registers can be used as four 16-bit data registers: AX, BX, CX and DX.
Lower and higher halves of the above-mentioned four 16-bit registers can be used as eight 8-bit data registers: AH, AL, BH, BL, CH, CL, DH, and DL.
Some of these data registers have specific use in arithmetical operations.
AX is the primary accumulator; it is used in input/output and most arithmetic instructions. For example, in multiplication operation, one operand is stored in EAX or AX or AL register according to the size of the operand.
BX is known as the base register, as it could be used in indexed addressing.
CX is known as the count register, as the ECX, CX registers store the loop count in iterative operations.
DX is known as the data register. It is also used in input/output operations. It is also used with AX register along with DX for multiply and divide operations involving large values.
EAX
eax is a 32-bit general-purpose register with two common uses: to store the return value of a function and as a special register for certain calculations. It is technically a volatile register, since the value isn't preserved. Instead, its value is set to the return value of a function before a function returns.
It stands for a general purpose register. The 16 bit AX register can be addressed as AH (high byte) and AL (low byte). The EAX register is the 32 bit version of the AX register. The E stands for extended. thus EAX stands for Extended Accumulator Register.
Eax is the accumulator, which is the default register of many addition multiplication commands.
EAX,AX,AH,AL : Called the Accumulator register. It is used for I/O port access, arithmetic, interrupt calls, etc...
AX
was a 16-bit accumulator, while AH
and AL
could be thought of as 8-bit registers on their
own or as a way to access the high-order and the
low-order bytes of AX
.
The X
in AX
meant to be a placeholder
that stood for both H
and L
.
This is in a way similar to how much later the “x” in x86 was meant to refer to 8086, 80186, 80286, etc.
EAX
stood for extended AX
.
And AX
now refers to the lower half of
EAX
, while AH
and AL
continue to refer to the two AX
bytes.
A simple example:
CODE void funtction1() { int A = 10; A += 66; } compiles to... funtction1: 1 pushl %ebp # 2 movl %esp, %ebp #, 3 subl $4, %esp #, 4 movl $10, -4(%ebp) #, A 5 leal -4(%ebp), %eax #, 6 addl $66, (%eax) #, A 7 leave 8 ret
Explanation: 1. push ebp 2. copy stack pointer to ebp 3. make space on stack for local data 4. put value 10 in A (this would be the address A has now) 5. load address of A into EAX (similar to a pointer) 6. add 66 to A
EBX
EBX stands for "Extended Base Register".
EBX is a base register that stores the base address in memory addressing.
When you issue an int 0x80
, your program is
interrupted and the kernel inspects the state of the registers.
From eax
it grabs the number of the system call you
want to execute and from the other registers it grabs additional
data. For example, for the write
system call, it grabs
the file descriptor from ebx
, a pointer to the buffer
you want to write from ecx
and the number of bytes you
want to write from edx
. The kernel does not know what
your intentions are, it just stupidly grabs whatever is in the
registers, so it does matters which registers you use.
However, in general, it does not matter what registers you use
for what values. In your own code, you are free to use almost all
registers (except for such as registers as esp
) for
whatever purpose you want as long as you don't interact with other
peoples code.
The only places where it matters which registers are used is when you want to interact with code written by other people, such as when calling functions or the operating system or when writing functions that are going to be called by others. In such cases, you have to set the relevant registers to the expected values or possibly preserve their contents.
For example, when you write a function called by other people's
code, it is expected that you return the result of your function in
eax
and preserve the contents of the registers
ebx
, esi
, edi
,
esp
, and ebp
. If you use these registers
for your own purposes, you have to first save their values
somewhere (e.g. on the stack) and restore them to their original
values before returning.
There are also some instructions that expect there operands to
be in certain registers (such as stos
or
idiv
), but for most instructions you are free to
choose whatever registers you want.
In the cases where it matters, the rules which registers are used for what purpose are written down in an Application Binary Interface (ABI) document. This document can be understood as an agreement between all programmers as to which data to expect in which registers when calling functions or the operating system. Strict adherence to the ABI is necessary to make your code work correctly when calling/called by other people's code.
Example
The following assembly language code displays the string 'Hello World' on the screen −
section .text global _start ;must be declared for linker (ld) _start: ;tells linker entry point mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data msg db 'Hello, world!', 0xa ;string to be printed len equ $ - msg ;length of the string
ECX
ecx is a volatile general-purpose register that is occasionally used as a function parameter or as a loop counter. Functions of the "__fastcall" convention pass the first two parameters to a function using ecx and edx.
For example, in multiplication operation, one operand is stored in EAX or AX or AL register according to the size of the operand. BX is known as the base register, as it could be used in indexed addressing. CX is known as the count register, as the ECX, CX registers store the loop count in iterative operations.
ECX is the counter and the internal counter of the repeated (REP) prefix command and loop command.
ECX stands for Extended Counter Register.
eax, ebx, ecx and so on are actually registers,
which can be seen as "hardware" variables, meaning
different than higher level-language's variables. Registers can be
used in your software directly with instructions such as
mov
, add
or cmp
. The leading
e
means extended that is your register is 32 bits
wide. On the other hand, 64-bits registers begin by
r
.
Example
Here's how you add two numbers in assembly:
Here's the C/C++ equivalent:
int a = 3; int c = 7; a += c; return a;
And finally here's the assembly code:
mov eax, 3 mov ecx, 7 add eax, ecx ret
EDX
edx is a volatile general-purpose register that is occasionally used as a function parameter. Like ecx, edx is used for "__fastcall" functions. Besides fastcall, edx is generally used for storing short-term variables within a function.
EDX stands for Extended Data Register.
EdX is always used to place the remainder produced by integer division.
A feature of assembly language is that each line in the source code usually contains a single instruction to the processor, for example MOV EAX,EDX will move the content of the EDX register into the EAX register. Here the "MOV" instruction is called a "mnemonic". This may seem a pretty basic instruction if you are used only to a higher level language. However, in Windows, you also have the use of a vast high level language (Windows itself). Thus, using Windows and assembler gives you the benefit of both a high level language and also control of the processor - perfect combination!
Example
Stack instructions. The stack is an area of memory provided by Windows for each running program to use as a temporary storage area. Examples of such instructions are:-
PUSH EAX ;push the contents of EAX register onto the stack
POP EDX ;pop the last thing pushed on the stack to EDX
PUSH 1000h ;push hex value 1000 on the stack
MOV EBP,ESP ;keep current stack pointer in the EBP register
SUB ESP,30h ;move the stack pointer to make an area for local data
MOV D[EBP-20h],500h ;insert the value 500 hex in the local data area
Example including EAX,EBX,ECX,EDX.
Look at the following simple program to understand the use of registers in assembly programming. This program displays 9 stars on the screen along with a simple message −
section .text global _start ;must be declared for linker (gcc) _start: ;tell linker entry point mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov edx,9 ;message length mov ecx,s2 ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data msg db 'Displaying 9 stars',0xa ;a message len equ $ - msg ;length of message s2 times 9 db '*'
When the above code is compiled and executed, it produces the following result −
OUTPUT
Displaying 9 stars *********