For example, add in MIPS assumes the data is in 2’s Complement (补码) and addu assumes the data is unsigned binary number.
The data stored in the register is interpreted according to the instruction that uses it.
Register Allocation
Compiler associates Register with variables in the given program
Register Spilling
When there are more variables than the available registers can hold (due to the limited number of registers), we encounter a situation known as register spilling
To handle this, some variables are temporarily saved to memory (usually the stack frame) and restored later when they are needed again
There are multiple such registers in the CPU, the main purpose is storing information about the current state of the processor or device
CSRs are typically used to control various aspects of the processor’s operation, such as interrupt handling, memory management, and power management etc
Stack Registers
Stack Pointer
A Register that holds the Memory Address of the top of the Stack Segment (start of free space on the stack or the last item on the stack) in the current execution context. Here is the Diagram
offset($sp)
Used to access a memory locationrelative to the current top of the stack.
; Decrement the stack pointer by 8 (allocate 8 bytes on the stack)sub $sp, 8; Move the value at the memory location (sp + 4) into the eax registermov 4($sp), %eax
Frame Pointer
Maintains a reference point for the current Stack Frame
At the beginning of a function (prologue), the frame pointer is typically set to the stack frame
Throughout the function: The FP remains relatively unchanged, offering a stable reference point which is used to access data inside the new stack frame via displacement addressing mode
This means that the register can be accessed using the same Instruction and addressing modes as memory.
Often used to control hardware devices, such as IO Device (Like the UART in XV6) and memory controllers
XV6-RISCV Memory Mapped Registers
memlayout.h
// XV6-RISCV Kernel Codes, memlayout.h// Physical memory layout// qemu -machine virt is set up like this,// based on qemu's hw/riscv/virt.c://// 00001000 -- boot ROM, provided by qemu// 02000000 -- CLINT// 0C000000 -- PLIC// 10000000 -- uart0 // 10001000 -- virtio disk // 80000000 -- boot ROM jumps here in machine mode// -kernel loads the kernel here// unused RAM after 80000000.// the kernel uses physical memory thus:// 80000000 -- entry.S, then kernel text and data// end -- start of kernel page allocation area// PHYSTOP -- end RAM used by the kernel// qemu puts UART registers here in physical memory.#define UART0 0x10000000L#define UART0_IRQ 10