Getting Approximately Memory Size in x86 Protected Mode without Memory Map: Tech Support Guide
In this article, we will discuss how to get approximately memory size in x86 protected mode without using a memory map. This is a common scenario in tech support, where the goal is to determine the amount of memory available on a system without relying on external tools or libraries.
Understanding the x86 Protected Mode Memory Model
The x86 protected mode memory model is based on a segmented architecture, where memory is divided into segments, each with its own base address and limit. The Global Descriptor Table (GDT) is used to define the segments and their limits, and the processor uses this information to translate virtual addresses into physical addresses.
In this model, the base address of the GDT is set to 0, and the limit is set to 4 GB. This means that the processor can access up to 4 GB of memory, but only the first 1 MB is directly accessible. To access memory beyond the first 1 MB, the processor must use paging, which is a more complex memory management technique.
Getting Approximately Memory Size without Memory Map
To get approximately memory size without using a memory map, we can use the following steps:
- Set up the GDT with a base address of 0 and a limit of 4 GB.
- Load the GDT into the processor.
- Use the CR0 register to enable protected mode.
- Use the CPUID instruction to determine the maximum addressable memory.
Here is an example of how to do this in assembly language:
; Set up the GDT
mov ax, 0
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; Set the GDT base address to 0 and the limit to 4 GB
lgdt [gdt_descriptor]
; Load the GDT into the processor
mov eax, cr0
or eax, 1
mov cr0, eax
; Use CPUID to determine the maximum addressable memory
mov eax, 0
cpuid
mov eax, 1
cpuid
mov eax, ebx
shr eax, 20
and eax, 0xFFFFF000
mov edx, eax
; Convert the address to MB
mov eax, edx
mov ebx, 1024 * 1024
div ebx
mov edx, eax
; Print the result
mov eax, 4
mov ebx, 1
mov ecx, message
mov edx, edx
int 0x80
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
gdt_start:
dw 0
dq 0
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
message db "Approximately memory size: ", 0
In this example, we first set up the GDT with a base address of 0 and a limit of 4 GB. We then load the GDT into the processor and enable protected mode. Finally, we use the CPUID instruction to determine the maximum addressable memory and convert it to MB before printing it to the screen.
In this article, we have discussed how to get approximately memory size in x86 protected mode without using a memory map. By understanding the x86 protected mode memory model and using the CR0 register and the CPUID instruction, we can determine the maximum addressable memory on a system.