From a0dacc4b9e882f6b5f32aeda136255e1edd0fca6 Mon Sep 17 00:00:00 2001 From: ridethepig Date: Wed, 21 Sep 2022 14:25:18 +0800 Subject: [PATCH] print all cluster numbers ok --- loader.asm | 136 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 43 deletions(-) diff --git a/loader.asm b/loader.asm index d1c34a7..506bb40 100644 --- a/loader.asm +++ b/loader.asm @@ -28,15 +28,15 @@ DispStr: pop bp ret ;---------------------------------------------------------------------------- -; @param ax: the int to print +; @param bp + 4: the word to print as uint_16t ;---------------------------------------------------------------------------- ; print a word as decimal to current cursor - DispInt: push bp mov bp, sp pusha mov di, 0 + mov ax, [bp + 4] .loop1: mov dx, 0 mov bx, 10 @@ -61,8 +61,9 @@ DispInt: pop bp ret ;---------------------------------------------------------------------------- -; give you a new line +; no param ;---------------------------------------------------------------------------- +; give you a new line DispNewline: push bp mov bp, sp @@ -81,6 +82,27 @@ DispNewline: ret ;---------------------------------------------------------------------------- +; @param bp + 4: low byte holds the ascii to print +;---------------------------------------------------------------------------- +; putchar to current cursor pos +DispPutchar: + push bp + mov bp, sp + pusha + ; get cursor pos + mov ax, 0300h + mov bh, 0 + int 10h + ; putchar + mov ax, [bp + 4] + mov ah, 0Eh ; no need to set al, ax holds the param + mov bl, 0Fh + int 10H + + popa + pop bp + ret +;---------------------------------------------------------------------------- ; 函数名: StringCmp ;---------------------------------------------------------------------------- ; 作用: @@ -143,11 +165,11 @@ ReadSector: pop bp ret .ReadFail: - jmp $ ; 如果cf位置1,就意味着读入错误,这个时候建议直接开摆 ;------------------------------------------------------------------------------ ; 函数名: GetNextCluster +; data => BaseOfSectorBuf : 0 ;------------------------------------------------------------------------------ ; 作用: ; ax存放的是当前的簇(cluster)号,根据当前的簇号在fat表里查找,找到下一个簇的簇号,并将返回值存放在ax @@ -176,9 +198,9 @@ GetNextCluster: mov ax, [es:bx] pop bx ; 取回奇数标识信息 cmp bx, 0 ; 如果是第奇数个FAT项还得右移四位 - jz EvenCluster ; 可能是微软(FAT是微软创建的)第一个亲儿子的原因,有它的历史局限性 + jz .EvenCluster ; 可能是微软(FAT是微软创建的)第一个亲儿子的原因,有它的历史局限性 shr ax, 4 ; 当时的磁盘很脆弱,经常容易写坏,所以需要两张FAT表备份,而且人们能够制作的存储设备的容量很小 -EvenCluster: +.EvenCluster: and ax, 0FFFh ; 读完需要与一下,因为高位是未定义的,防止ax值有误 mov word [bp - 2], ax ; 这里用了一个技巧,这样在popa的时候ax也顺便更新了 @@ -191,7 +213,7 @@ EvenCluster: ; @retval ax: first cluster no ;------------------------------------------------------------------------------ ; Find File by name, return first cluster no. - +; data => BaseOfSectorBuf:OffsetOfSectorBuf FuncFindFile: push bp mov bp, sp @@ -247,58 +269,86 @@ FuncFindFile: pop bp ret +;------------------------------------------------------------------------------ +; @param bp + 4: first cluster number +;------------------------------------------------------------------------------ +; Find File by name, return first cluster no. +; data => BaseOfSectorBuf:OffsetOfSectorBuf +FuncPrintClusterNumbers: + push bp + mov bp, sp + pusha + mov ax, [bp + 4] +.loop: + mov ax, dx + push ax + call DispInt ; ax now holds the current cluster number + add sp, 2 + push ' ' + call DispPutchar + add sp, 2 + + call GetNextCluster ; 根据数据区簇号获取文件下一个簇的簇号 + mov dx, ax ; dx <- 下一个簇的簇号 + cmp dx, 0FFFh ; 判断是否读完了(根据文档理论上dx只要在0xFF8~0xFFF都行,但是这里直接偷懒只判断0xFFF) + jnz .loop + + popa + pop bp + ret + MAIN: mov ax, cs ; cs <- 0 mov ds, ax ; ds <- 0 mov ss, ax ; ss <- 0 mov ax, BaseOfSectorBuf mov es, ax ; es <- BaseOfSectorBuf - +; Display "This is {name}'s boot" call DispNewline - mov ax, cs - push ax - mov ax, StudentString - push ax - mov ax, _endStudentString - StudentString - push ax - call DispStr - add sp, 6 ; pop for 3 times - mov ax, LoaderFileName - push ax + push cs + push StudentString + push _endStudentString - StudentString + call DispStr + add sp, 6 ; pop for 3 times + +; Read loader.bin's cluster number + push LoaderFileName call FuncFindFile - mov dx, ax ; temporarily save read result to dx - pop ax + add sp, 2 + mov dx, ax ; temporarily save read result to dx +; Print first cluster number call DispNewline - mov ax, cs - push ax - mov ax, clusternoString - push ax - mov ax, _endclusternoString - clusternoString - push ax + + push cs + push clusternoString + push _endclusternoString - clusternoString call DispStr add sp, 6 ; pop for 3 times - mov ax, dx ; dx holds the read result - call DispInt - mov ax, AA1FileName - push ax + push dx ; dx holds the read result + call FuncPrintClusterNumbers + add sp, 2 + +; Read aA1.txt's cluster number + push AA1FileName call FuncFindFile - mov dx, ax ; temporarily save read result to dx - pop ax + add sp, 2 + mov dx, ax ; temporarily save read result to dx +; Print first cluster number call DispNewline - mov ax, cs - push ax - mov ax, clusternoString - push ax - mov ax, _endclusternoString - clusternoString - push ax + + push cs + push clusternoString + push _endclusternoString - clusternoString call DispStr - add sp, 6 ; pop for 3 times - mov ax, dx ; dx holds the read result - call DispInt + add sp, 6 + + push dx ; dx holds the read result + call FuncPrintClusterNumbers + add sp, 2 jmp $ ;============================================================================== @@ -307,7 +357,7 @@ MAIN: BaseOfStack equ 07c00h ; Boot状态下堆栈基地址(栈底, 从这个位置向低地址生长) ; according to osdev, memory from 0x0000:0x7e00 to 0x7000:0xFFFF is free to use BaseOfSectorBuf equ 07000h ; diff from where we are here -OffsetOfSectorBuf equ 0000h ; diff from where we are here +OffsetOfSectorBuf equ 0400h ; the first 400h(=1024d) bytes left for fat1 read buf RootDirSectors equ 14 ; 19~32 SectorNoOfRootDirectory equ 19 ; Root Sector starts from 19 SectorNoOfFAT1 equ 1 @@ -335,7 +385,7 @@ LeftRootDirSectors dw RootDirSectors ; 还未搜索的根目录 RootDirSectorNow dw SectorNoOfRootDirectory ; 目前正在搜索的根目录扇区 BufferPacket times 010h db 0 ; ReadSector函数会用到的,用于向int 13h中断的一个缓冲区 -LoaderFileName db "LOADE BIN", 0 ; 8:3, fill with whitespace +LoaderFileName db "LOADER BIN", 0 ; 8:3, fill with whitespace AA1FileName db "AA1 TXT", 0 ; the 0 at end is for StrCmp StudentString db "This is LiMaoliang's boot" ; size = 25 _endStudentString