0

(1) I wonder if the is register the only place the arithmetic calculation can happen? It looks like: add BYTE PTR [var], 10 — add 10 to the single byte stored at memory address var is also correct in syntax, is there an implicit register?

(2) What about CMP instruction, is register the only place where this instruction can happen?

(3) How about a POP instruction, is the stack value also poped into an implicit non-volatile register?

(4) Non-volatile register's value must be preserved along with the function, is this correct understanding? Inside a function, it uses RSI register to get the value from the stack. Is that value required to preserved? In other words, the RSI register can only be used once by this function?

(5) If the function changes Non-volatile register value, can it preserve the previous value in either stack or another register?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
yeehaw
  • 159
  • 2
  • 11
  • 2
    The calculations happen in the ALU, not in a register. A register, just like memory, is just a place to store data. The processor may use additional registers as an implementation detail in instructions like `add byte ptr [var], 10`, but that is not obsverable. As for questions (4) and (5), these are unrelated to the other ones. Do not ask different, unrelated questions at once. – fuz Mar 07 '21 at 16:09
  • Thank you! `The processor may use additional registers as an implementation detail in instructions like add byte ptr [var], 10, ` but if such implementation is a must? In other words, what can be ALU's operand, both values from register and stack? – yeehaw Mar 07 '21 at 16:30
  • 1
    No, such an implementation is not a must. However, an internal register or latch is commonly used to hold both values coming from register, memory, or immediate operands before they are being processed by the ALU. This simplifies the processor design. – fuz Mar 07 '21 at 16:32
  • 3
    But anyway, whether the CPU uses such an internal register or not doesn't matter for you, the programmer. `add byte ptr [var], 10` is a legal instruction. That's all that matters. – fuz Mar 07 '21 at 16:33
  • I see, so there are registers inside ALU which do not belong to the CPU registers we know such eax, r8, etc., correct? – yeehaw Mar 07 '21 at 16:35
  • 2
    Right. The 8 (or 16) *architectural* registers are all that machine-language instructions can reference, but building a CPU usually requires some temporary storage for the CPU internals to use. Definitely so for a CISC ISA like x86 that has instructions like `push [rdi]` that copies from memory to memory. (Modern x86 CPUs apparently have some actual hidden registers of the same type as the visible ones like RAX, which get renamed onto physical registers. These hidden regs are reserved for use by microcode. That's a separate thing from latches between pipeline stages, etc.) – Peter Cordes Mar 07 '21 at 16:42
  • 2
    Questions (4) and (5) seem totally separate from the CPU-architecture questions of how CPUs work / what a single machine instruction can do, and are instead about software calling conventions. (The whole concept of volatile / non-volatile register is purely a software convention. [What does it mean that "registers are preserved across function calls"?](https://stackoverflow.com/q/63865026)) – Peter Cordes Mar 07 '21 at 16:44
  • 1
    @yeehaw Yes, the CPU typically has many more registers than what is visible to the programmer. And what these are changes from processor to processor. You should not make assumptions about that. – fuz Mar 07 '21 at 16:54
  • Could anyone give my question thumbs up so that I can 50 points to comment, thank you ;) – yeehaw Mar 08 '21 at 07:43

0 Answers0