Closed IlanVinograd closed 1 week ago
Register CS (Code Segment) Register IP (Instruction Pointer)
The CS:IP, in Briefly, the syntax is segment:offset A simple analogy could be chapter : verse.
Note:
this is not a general purpose register! the combination ( cs:ip ) represents the location where the CPU is currently fetching instructions to execute.
Important:
The bootloader is loaded by the BIOS at physical address 0x7C00.
This physical address can be represented as segment:offset 0x0000:0x7C00 or 0x07C0:0x0000 in 16-bit real mode addressing.
Note:
here's why:
Physical Address = Segment × 16 + Offset
Physical Address = 0x0000×16 + 0x7C00 = 0x0000 + 0x7C00 = 0x7C00
Physical Address = 0x7C0×16 + 0x0000 = 0x7C00 + 0x0000 = 0x7C00
We should explicitly set CS:IP because, in some cases, the BIOS may load the boot sector using CS:IP as 0x07C0:0x0000, which also results in the physical address 0x7C00. However, if the segment is not explicitly defined, the processor may interpret instructions and data differently.
Caution:
Unpredictability: If CS:IP is not explicitly set in the boot sector, the BIOS may use its own value for the segment, leading to unpredictable program behavior. For example, if the code expects a certain offset relative to CS, but the segment turns out to be different, the program may start executing from the wrong address.
Execution Errors: The code might access the wrong area of memory, leading to crashes or improper functioning of the boot sector. This is particularly critical during the early stages of booting, where any deviation from expected behavior can disrupt the further loading of the operating system.
Compatibility: Different BIOS implementations may have varying loading methods, and explicitly setting CS:IP ensures that your boot sector will work correctly across different systems.
ljmp 0x07C0, start
start:
; Code to execute after the jump
This code is used to explicitly set a new code segment (CS) and instruction pointer (IP) to continue execution in segment 0x07C0 at the specified offset (start).
ljmp 0x07C0, start is used for explicitly setting the segment and offset during execution.
Use of org
not give explicitly set:
org 0x7C00
This code is used to instruct the assembler that the entire program is loaded at the physical address 0x7C00, and all offsets in the program will be calculated relative to this address. This simplifies the code as it doesn't require explicitly setting the segment.
org 0x7C00 does not change the segment during execution, it only informs the assembler about the starting address for calculating offsets. Therefore, ljmp is the explicit method for setting CS:IP during program execution.
16 bits = Real Mode for example 32 bits = Protected Mode
The [BITS 16]
directive tells the assembler that the code is written for a 16-bit environment.
It ensures that the assembler generates 16-bit machine code, which the CPU can correctly execute when it in Real Mode
[BITS 16]
:Not using [BITS 16]
can cause the assembler to generate incorrect 32-bits or 64-bits code, leading to errors in instruction size and address calculation.
This can result in crashes, system hangs, or unpredictable behavior.
Note:
step 1. set directive [16 BITS]
step 2. set [ORG 0x7C00] / ljmp 0x07C0, start / jmp 0x07C0, start. Done
step 3. cli
step 4. set register
step 5. Create Stack
step 6. sti
step 7. Enable A20 Line
step 8. ReadSectors:
step 9. Set Up Global Descriptor Table (GDT)
step 10. Transition to Protected Mode 32 Bit
step 11. Protected Mode Code
[32 BITS]
step n.
To-Do Bonus
We need to consider/check where to store stage 2 on the disk: whether it should be on the same track as stage 1 or jump to the next track. This is important because when using the BIOS interrupt INT 13h with AH=02h to read sectors, if the read operation starts at a later sector, it might overflow the current track and move to the next, depending on the drive's geometry.
Is there some difference if write stage 1 on inner track or outer: of the reason speed reading and writing like radius of the track or the disk velocity is much higher on outer and lower on inner.