-2

I don't truly understand the purpose of the EBX register. I've gotten different answers from searching what the the purpose of EBX, but the most common consensus is that EBX has no special purpose like EAX, ECX, or EDX. Why would I need to use EBX? I've removed EBX from simple tutorials and the program still worked. So what is the reason to use EBX? Why should I care about it if I can work without it?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • On more RISC-like architectures, *none* of the registers have special purposes. They can all be used interchangeably in any instruction. That is generally a Good Thing. – Nate Eldredge Sep 22 '20 at 06:32
  • The legacy purpose of BX was to be used as a pointer, BH and BL are the 8086 replacement registers for 8080 H and L registers.For 8086, BX, SI, and DI default to use segment register DS (data segment), while BP and SP default to using segment register SS (stack segment). – rcgldr Sep 22 '20 at 06:59

1 Answers1

7

Having an extra register means you can keep more stuff in registers without having to store/reload it. That's why x86-64 added R8..R15, even though almost none of them have any special purposes.

You're looking at this almost totally backwards. All general-purpose registers have some implicit uses, but that's not why they exist. x86 could have been designed without "string" instructions and without EDI or ESI registers, or without a lot of things, but then it would have been harder to write efficient code.

(The more fundamental answer to "why x86 registers exist" is the original design of 8086, which was designed to make asm source porting from 8080 simple enough to be done mechanically. https://retrocomputing.stackexchange.com/questions/5121/why-are-first-four-x86-gprs-named-in-such-unintuitive-order - that's why in x86 machine code,the register numbers are EAX, ECX, EDX,then EBX)


But yes, EBX does have a few implicit uses. In modern code, notably for one of cpuid's 4 outputs, and for cmpxchg8b / cmpxchg16b. In historical x86, as a base register for xlatb, and in 16-bit addressing modes as one of the few registers that could be used in an addressing mode.

See Why are rbp and rsp called general purpose registers? which includes implicit uses for all 8 of the "legacy" registers, and for R11.

In the i386 Linux system-call calling convention, EAX holds the call number, EBX holds the first arg, then ECX, EDX, etc. But if EBX hadn't existed, the calling convention obviously would have been designed differently.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Would I use EBX more for EAX arguments? Or more for something else? What I mean by more I mean would I use it more frequently or more commonly. – notevention Sep 22 '20 at 01:52
  • 1
    @notevention: I don't understand what you're asking. More what compared to what? Most of the time EBX is just one of the call-preserved registers that you can use when you want to save/restore one. – Peter Cordes Sep 22 '20 at 02:03
  • Allow me to reask my question. When should I know when to use EBX over the other general purpose registers? I get that EBX can be used for an argument, but what are other cases to definitely use EBX over the rest? – notevention Sep 22 '20 at 02:06
  • @notevention: There are no reasons, unless you're using `lock cmpxchg8b` or `cpuid`. You just use EBX when you want another register and you're already using some / all other registers. You generally want to use registers for everything, but eventually you run out. – Peter Cordes Sep 22 '20 at 02:09
  • So to make a long story short, EBX can be imagined as a spare register for EAX is busy? – notevention Sep 22 '20 at 02:10
  • 1
    When I need to hold a local variable across a function call, ebx is my first choice. – prl Sep 22 '20 at 02:11
  • 3
    Unless you’re writing something trivial, generally *all* of your registers should be busy. – prl Sep 22 '20 at 02:12
  • Take a look at this example "9 stars" https://www.tutorialspoint.com/assembly_programming/assembly_registers.htm If I removed the EBX, the program still works. Why? Why did they even add EBX if it works without it? – notevention Sep 22 '20 at 02:25
  • 2
    @notevention: You're still using EBX as an argument to those `int 0x80` Linux system calls, you're just relying on Linux's behaviour of initializing it (and other registers except ESP) to `0` in a static executable. And on the fact that the fd=0 stdin file descriptor refers to the same read-write file *description* when you run your program on a terminal. (i.e. stdin and stdout are duplicates of each other, on a read-write open of the terminal). It would fail if you redirected its input from a file like `strace ./my_prog < /dev/null`. – Peter Cordes Sep 22 '20 at 02:33
  • @notevention: Or if you put that code in a function you called from `main`, it would likely have some garbage value in EBX so your system calls would return a `-EBADFD` error if you used whatever garbage value that was as the `fd` first argument to a write system call. – Peter Cordes Sep 22 '20 at 02:35