Understanding of boot loader assembly code and memory locations

ⅰ亾dé卋堺 提交于 2019-12-06 07:37:45

following bootloader code:

It's missing info how it will set up relative address of assembled code, i.e. how the local offsets will be calculated. Usually bootloaders start with org 0x7C00 to make it explicit the code expects to be started at cs:ip = 0000:7C00. But would you do that, the ds=07C0 would be wrong, that one suggests the code expects the offsets be assembled as if it starts at 07C0:0000 instead of 0000:7C00. While both addresses target the identical physical memory address, the segment:offset pairs are different then.

Each segment increments in blocks of 16 bytes, so 07C0h will give you address range 0x7C00-0x7C0F. The next segment 07C1h will give you address 0x7C10-0x7C1F.

Each segment gives you 64kiB range, although the start address advances only by 16 bytes, so there's lot of overlap between segments and you can address the same physical address by many combinations. I.e. ds=07C0 gives you window into physical memory range 07C00-17BFF.


Then your conversions of values to hexadecimal are wrong (see also Michael comments), 288 = 0x120, and 4096 = 0x1000, but you correctly conclude there's 512B of bootloader code (single sector of block device), 4096B of spare space, and then 4096B of stack space. Would you fill up the stack by pushing more than 4096 bytes into it, it will not hit the spare space after code, but will wrap around to 08E0:FFFE (well above the original start of stack).

I am assuming that it won't be sequential and the assembler will compile the machine code differently.

Quite opposite, instructions and defined bytes in the source code are emitted sequentially in the resulting machine code. Use the "listing" command line switch to see yourself how the assembler emits machine code for particular lines. Would you for example move the text_string db 'This is my cool new OS!', 0 line at the beginning after BITS 16 directive, that text would be then at the beginning of machine code, loaded and executed by BIOS at/from 0000:7C00 address, executing the text bytes as instructions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!