implement LBA to CHS conversion in assembly

This commit is contained in:
mykola2312 2024-08-07 17:31:28 +03:00
parent 721cd172c8
commit c0e1b0efc2
2 changed files with 39 additions and 34 deletions

View file

@ -36,7 +36,7 @@ entry:
.bootloader:
call serial_init
mov $73728, %eax
mov $2342424, %eax
call lba_to_chs
.halt:
jmp .halt

View file

@ -10,56 +10,61 @@ lba_to_chs:
# we actually gonna need stack frame for that one lol
push %bp
mov %sp, %bp
sub $10, %sp
sub $12, %sp
# -4(%bp) dword LBA
# -6(%bp) word cylinder
# -8(%bp) word head
# -10(%bp) word sector
mov %eax, -4(%bp) # save LBA
# -6(%bp) word disk_sectors * 2
# -8(%bp) word cylinder
# -10(%bp) word head
# -12(%bp) word sector
mov %eax, -4(%bp) # save LBA
# chs->head = lba / (conf->num_heads * conf->num_sectors);
# (conf->num_heads * conf->num_sectors)
movw disk_heads, %ax
mulw disk_sectors
mov %ax, %bx
# lba /
# chs->head = (lba % (conf->num_sectors * 2)) / conf->num_sectors;
# (conf->num_sectors * 2)
xor %dx, %dx
mov -4(%bp), %eax
div %ebx
movw disk_sectors, %ax
mov $2, %bx
mul %bx
# ax - num_sectors * 2
mov %ax, -6(%bp)
# (lba %
mov %ax, %bx # disk_sectors * 2
mov -4(%bp), %eax # lba
div %ebx # %
# edx, as remainder, now must be divided by num_sectors
mov %dx, %ax # remainder ain't gonna exceed 16 bit value
xor %dx, %dx # always clear god damn EDX it can screw your div/mul!
divw disk_sectors
# ax - head
mov %ax, -10(%bp)
# chs->cylinder = (lba / (conf->num_sectors * 2));
mov -4(%bp), %eax # LBA
mov -6(%bp), %bx # disk_sectors * 2
div %ebx # /
# ax - cylinder
mov %ax, -8(%bp)
# chs->cylinder = (lba / conf->num_sectors) % conf->num_heads;
# (lba / conf->num_sectors)
xor %dx, %dx
# chs->sector = (lba % conf->num_sectors + 1);
mov -4(%bp), %eax # LBA
movw disk_sectors, %bx
mov -4(%bp), %eax
div %ebx
# % conf->num_heads
xor %dx, %dx
divw disk_heads
# dx, as remainder, now has cylinder
mov %dx, -6(%bp)
# chs->sector = (lba % conf->num_sectors) + 1;
xor %dx, %dx
mov -4(%bp), %eax
divw disk_sectors
# dx now has sector num, but we need to increment it
# dx - sector, but needs +1
inc %dx
# dx - sector
mov %dx, -10(%bp)
mov %dx, -12(%bp)
xor %eax, %eax # flush eax because it loves to screw up itoa
# now lets print C H S values
mov -6(%bp), %ax
mov -8(%bp), %ax
mov $str_cylinder, %si
call prints_number
mov -8(%bp), %ax
mov -10(%bp), %ax
mov $str_head, %si
call prints_number
mov -10(%bp), %ax
mov -12(%bp), %ax
mov $str_sector, %si
call prints_number