From c0e1b0efc243377a9b1a12eb6baafb35afdc386b Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:31:28 +0300 Subject: [PATCH] implement LBA to CHS conversion in assembly --- src/mbr_test.s | 2 +- src/test_disk_read.s | 71 ++++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/mbr_test.s b/src/mbr_test.s index 721f8ae..15b1fa2 100644 --- a/src/mbr_test.s +++ b/src/mbr_test.s @@ -36,7 +36,7 @@ entry: .bootloader: call serial_init - mov $73728, %eax + mov $2342424, %eax call lba_to_chs .halt: jmp .halt diff --git a/src/test_disk_read.s b/src/test_disk_read.s index 2a9dc73..9244808 100644 --- a/src/test_disk_read.s +++ b/src/test_disk_read.s @@ -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