+detect_memory:
+ mov $memdet_e820_msg, %esi
+ call putstr
+ call detect_mem_e820
+ jnc memdet_done
+ mov $rdfail_msg, %esi
+ call putstr
+
+ mov $memdet_e801_msg, %esi
+ call putstr
+ call detect_mem_e801
+ jnc memdet_done
+ mov $rdfail_msg, %esi
+ call putstr
+
+ mov $memdet_88_msg, %esi
+ call putstr
+ call detect_mem_88
+ jnc memdet_done
+ mov $rdfail_msg, %esi
+ call putstr
+
+ # just panic...
+ mov $memdet_fail_msg, %esi
+ call putstr
+0: hlt
+ jmp 0b
+
+memdet_done:
+ mov $rdok_msg, %esi
+ call putstr
+ ret
+
+memdet_fail_msg: .ascii "Failed to detect available memory!\n"
+ .ascii "Please file a bug report: https://github.com/jtsiomb/pcboot/issues\n"
+ .asciz " or contact me through email: nuclear@member.fsf.org\n"
+memdet_e820_msg: .asciz "Detecting RAM (BIOS 15h/0xe820)... "
+memdet_e801_msg: .asciz "Detecting RAM (BIOS 15h/0xe801)... "
+memdet_88_msg: .asciz "Detecting RAM (BIOS 15h/0x88, max 64mb)... "
+
+ # detect extended memory using BIOS call 15h/e820
+detect_mem_e820:
+ movl $0, boot_mem_map_size
+
+ mov $buffer, %edi
+ xor %ebx, %ebx
+ mov $0x534d4150, %edx
+
+e820_looptop:
+ mov $0xe820, %eax
+ mov $24, %ecx
+ int $0x15
+ jc e820_fail
+ cmp $0x534d4150, %eax
+ jnz e820_fail
+
+ # skip areas starting above 4GB as we won't be able to use them
+ cmpl $0, 4(%edi)
+ jnz e820_skip
+
+ # only care for type 1 (usable ram), otherwise ignore
+ cmpl $1, 16(%edi)
+ jnz e820_skip
+
+ mov buffer, %eax
+ mov $boot_mem_map, %esi
+ mov boot_mem_map_size, %ebp
+ # again, that's [ebp * 8 + esi]
+ mov %eax, (%esi,%ebp,8)
+
+ # skip areas with 0 size (also clamp size to 4gb)
+ # test high 32bits
+ cmpl $0, 12(%edi)
+ jz e820_highzero
+ # high part is non-zero, make low part ffffffff
+ xor %eax, %eax
+ not %eax
+ jmp 0f
+
+e820_highzero:
+ # if both high and low parts are zero, ignore
+ mov 8(%edi), %eax
+ cmpl $0, %eax
+ jz e820_skip
+
+0: mov %eax, 4(%esi,%ebp,8)
+ incl boot_mem_map_size
+
+e820_skip:
+ # terminate the loop if ebx was reset to 0
+ cmp $0, %ebx
+ jz e820_done
+ jmp e820_looptop