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

  • Thread starter Thread starter pairofstrings
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the use of registers in a Linux assembly programming code snippet. Participants explore why certain registers, such as segment registers, index registers, and pointer registers, are not utilized in the provided code. The conversation includes technical explanations and personal experiences with assembly programming in different environments.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions why the programmer does not use segment registers, index registers, or pointer registers in the assembly code.
  • Another participant explains that segment registers are not needed in this context as the code is contained within the .text and .data sections, suggesting that additional segments would require segment registers.
  • A correction is made regarding a typo in the code, clarifying that the hexadecimal representation of the linefeed character should be 0X0A instead of OXOA.
  • It is noted that index registers (like ESI and EDI) are unnecessary for this code since it does not utilize instructions that require them, such as LOOP.
  • One participant asserts that the ECX register is being used as a pointer to the string, addressing the question about pointer registers.
  • Another participant mentions that in 32-bit mode, normal registers can function as pointers, and that segment registers and virtual memory are typically managed by the operating system to provide a flat virtual address space.
  • There is a discussion about the roles of EBP and ESP as pointer registers meant for stack management, indicating their importance in other contexts.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and usage of various registers, with some agreeing on the irrelevance of segment registers in this context, while others provide alternative perspectives on pointer and index registers. The discussion remains unresolved regarding the broader implications of register usage in assembly programming.

Contextual Notes

Participants reference their familiarity with assembly programming in different environments (Linux vs. DOS), which may influence their perspectives on register usage. There is also mention of the limitations of segment registers in modern programming practices.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
8
Views
3K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
8
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
7K
Replies
6
Views
3K