4 ms·
The problem I keep running into is that everybody's asm is different looking, and not even internally consistent. Trying to parse all of this stuff: [BITS
by inamberclad 8y ago
The problem I keep running into is that everybody's asm is different looking, and not even internally consistent. Trying to parse all of this stuff:
[BITS 16] ;tell the assembler that its a 16 bit code
Okay, so this is an instruction to the assembler saying that we're only working with 16 bit registers and to emit 16 bit code. Straightforwards enough here.
[ORG 0x7C00];Origin, tell the assembler that where the code will
;be in memory after it is been loaded
This means that the first instruction will be at physical address 0x7c00 when the code loads, right? What's the first instruction, the one below?
mov ah, 0x0A ; Set the call type for the BIOS
mov al, 66 ; Letter to display
mov cx, 3 ; Times to print it
mov bh, 0 ; Page number
Makes enough sense, just mov instructions al refers to the top 8 bits of register ax and al refers to the bottom 8 bits. I see that register bh is zeroed, but what about bl? Why not xor bx, bx or something like that? Don't instructions normally go in a section named .text? How does the BIOS know where to find this code and begin execution in the first place?
...
TIMES 510 - ($ - $$) db 0 ;fill the rest of sector with 0
Wtf is that? What's a sector? What the syntax here? Are we repeating this 510 times, or 510 minus some value? The db command is for writing bytes, but where are the bytes written to?
DW 0xAA55 ; add boot signature at the end of bootloader
Where are these bytes written? Why is DW capitalized here when db was left in lowercase before?
- wruza 8y agoORG shifts all addresses below it to given offset. That means that external loading code is supposed to put it there, otherwise all non-relative instructions will load/store at wrong locations. BL is not set because it is irrelevant. How you clear/save a register was up to you. https://en.wikipedia.org/wiki/INT_10H https://en.wikipedia.org/wiki/INT_10H .text is a section of executable format, but what you got is a boot loader. It doesn’t have sections, it’s just a first 512-byte boot disk sector that is loaded at 7C00h by BIOS and directly jmp’d into. I forgot what exactly $-$$ is, but one of those is “current” address and another should be ORG address, but I’m not sure, so this expression turns into e.g. TIMES 410 DB 0, if 100 bytes used by so far. AA55 is a boot sector end marker, something like canary value. Never looked into that in detail. Assemblers were mostly case-insensitive, like Pascal and BASIC. Case is up to you, if you have any preference.
- wruza 8y agoAs for your confusion, you brought uneasy example here. This is a boot sector, which requires some knowledge on the process, flat binary addressing by convention, and tricks that must fit in 510 bytes, if any. That seems hard (not really) because all you’ve got is a first disk sector and 64kB of BIOS. Once you boot up into OS by reading more sectors and executing these, things slowly begin to ease. Your regular DOS-based program will look like any other example on the internet. Edit: oh, this code is from the article. Bad habit of reading comments first, sorry!