Why Aren't Other Registers Used in This Programming Code?

  • Thread starter Thread starter pairofstrings
  • Start date Start date
AI Thread Summary
The discussion centers on an assembly program for Linux that outputs "hello world" and exits. The original poster questions why the code does not utilize segment, index, or pointer registers. The response clarifies that segment registers are unnecessary in this context because the program operates within the predefined .text and .data sections, which do not require additional segmentation. It also points out a typo in the hex representation of the linefeed character. The use of registers like EAX, EBX, ECX, and EDX is explained, highlighting that ECX serves as a pointer to the string. Index registers are deemed irrelevant here since the program does not employ looping instructions. The discussion concludes by noting that in 32-bit mode, general-purpose registers can function as pointers, while segment registers are typically managed by the operating system, allowing for a flat virtual address space.
pairofstrings
Messages
411
Reaction score
7
section .text
global _start:
_start:
mov eax, 4
mov ebx, 1
mov ecx, string
mov edx, length
int 80h

mov eax, 1
mov ebx, 0
int 80h

section .data
string: db "hello world", OxOA
length: equ 13

My question is simple. Why is the programmer not using other registers such as segment registers, index registers or pointer registers? I googled everywhere but couldn't find any explanation. If you have any material please share. thank you.
 
Technology news on Phys.org
pairofstrings said:
section .text
global _start:
_start:
mov eax, 4
mov ebx, 1
mov ecx, string
mov edx, length
int 80h

mov eax, 1
mov ebx, 0
int 80h

section .data
string: db "hello world", OxOA
length: equ 13

My question is simple. Why is the programmer not using other registers such as segment registers, index registers or pointer registers? I googled everywhere but couldn't find any explanation. If you have any material please share. thank you.
This appears to be assembly programming in Linux. I'm very familiar with assembly programming in DOS, but not at all in Linux.

Do you understand what this program does? The first block of code causes the string "hello world" to be displayed on the computer screen. The second block of code causes the program to exit.

To answer your questions, I'm not sure about the one you asked about segment registers. It appears to me that segment registers are not needed. The code portion of this program is in the .text section (the code segment) and the .data section (data segment) contains the string and a variable for its length. If you had more code or more data than would fit in the code and data segments, I imagine that you would need some extra segments, and then you would need to use segment registers to identify the segment you are using.

BTW, your code has a typo. The hex number after the string is the linefeed character, ASCII 10. You have OXOA - it should be 0X0A. In other words, with the digit 0 before and after X, not the capital O that you have.

As far as index registers (such as ESI and EDI), this code doesn't need them, since it is not using any instructions such as LOOP that require them.

As far as pointer registers, I'm not sure what you mean. The code stores the address of the string in ECX, so this register is being used as a pointer.

In a little more detail, what is going on here is this.
EAX <--- 4 -- output a string
EBX <--- 1 -- send output to stdout (the computer screen)
ECX <--- string -- store the address of the string
EDX <--- length -- store 13
int 80h -- system call into the kernel. The value in EAX specifies which system call. The values in the other registers are parameters.

For the other system call,
EAX <--- 1 -- exit
EBX <--- 0 -- return code (I think)
int 80h --- system call into the kernel.
 
Unless you code a kernel, you won't need to worry about segment registers. They're totally ignored because they're a pain to work with in C (and because there are other protection methods that work better).

When you say "pointer registers," do you mean EBP and ESP? Those are meant for stack management. They're too important for other things to be used for passing parameters.
 
pointer registers
In 32 bit mode, the normal registers can be used as pointers (EAX, ECX, EDX) as well as the traditional pointer registers (EBX, ESI, EDI, EBP, ESP), and segment registers and virtual memory are usually preset by the OS to provide a flat virtual address space for your program.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top