Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Operating system-breaking the limit of 512 bytes

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

I. Breaking through 512 bytes

Q: How to print strings in the main boot program? -- direct printing

A. String printing in BIOS

1. Specify print parameters (AX= 0x1301,BX= 0x0007)--without loop

2. Specifies the memory address of the string (ES:BP= string address)--determined by segment address and segment offset

3. Specifies the length of the string (CX= String Length)

4. interrupt call (int 0x10)

B. Compilation of tips

1. Functions can be defined in assembly--function names are defined using tags

call function; the last instruction of the function body is ret

2. If the code defines functions, stack space needs to be defined.

Mainly used to hold key register values; stack top address is held through sp register

3."Constant Definition" in Compilation--equ

Usage is-Const equ 0x7 c00; it is also different from dx(db,dw,dd), the difference is mainly that dx definition occupies corresponding memory space, equ definition does not occupy any memory space

C. Experimentation-Defining Print Functions

a. First, makefile can be used to simplify the creation of the main boot program and the creation of binary compilation files.

.PHONY : all clean rebuildSRC := boot.asmOUT := boot.binIMG := data.imgRM := rm -frall : $(OUT) $(IMG) dd if=$(OUT) of=$(IMG) bs=512 count=1 conv=notrunc @echo "Success! "$(IMG) : bximage $@ -q -fd -size=1.44$(OUT) : $(SRC) nasm $^ -o $@clean : $(RM) $(IMG) $(OUT)rebuild : @$(MAKE) clean @$(MAKE) all

As you can see from the results of make, it runs successfully, which makes it easier to modify the compilation process later.

Set up the boot.asm file

org 0x7c00jmp short startnopstart: mov ax, cs mov ss, ax mov ds, ax mov es, ax mov sp, ax mov ax, MsgStr//Assembly implementation of printing mov cx, 6 mov bp, ax mov ax, ds mov es, ax mov ax, 0x1301 mov bx, 0x0007 int 0x10last: hlt jmp last MsgStr db "MyDTOS ... " //Print string Buf: times 510-($-$$) db 0x00 db 0x55, 0xaa

Results of printed strings

But there is no print function at the time of setting, so improve the assembly code in asm file.

There are three steps to be carried out here--1. First, the print function is defined. 2. Then the stack space is defined-define the starting address. 3. Finally, the sp stack top pointer register points to the starting address of the definition stack.

II. How does the master boot program read the data at the specified sector?

A. Structure of floppy disks

1. floppy disk has two disks, one for each head

2. Each disk is divided into several circles, called cylinders.

3. Each cylinder is divided into sectors of 512 bytes each

Floppy disk data read and write-floppy disk data a sector 512 bytes for the unit read, specify the location of the data head number, cylinder number, sector number. Its formula is

Floppy data read in BIOS (int 0x13)

Read and write flow of floppy disk data

What we need to note here is that 16 in the assembly is a division operation (div)--dividend is placed in AX register, divisor is placed in general register or memory unit (8 bits), and the result is quotient located in AL, remainder located in AH.

1. First, modify the asm file according to the knowledge mentioned above, and view the virtual floppy disk

org 0x7c00jmp short startnopdefine: BaseOfStack equ 0x7c00header: BS_OEMName db "D.T.Soft" BPB_BytsPerSec dw 512 BPB_SecPerClus db 1 BPB_RsvdSecCnt dw 1 BPB_NumFATs db 2 BPB_RootEntCnt dw 224 BPB_TotSec16 dw 2880 BPB_Media db 0xF0 BPB_FATSz16 dw 9 BPB_SecPerTrk dw 18 BPB_NumHeads dw 2 BPB_HiddSec dd 0 BPB_TotSec32 dd 0 BS_DrvNum db 0 BS_Reserved1 db 0 BS_BootSig db 0x29 BS_VolID dd 0 BS_VolLab db "D.T.OS-0.01" BS_FileSysType db "FAT12 "start: mov ax, cs mov ss, ax mov ds, ax mov es, ax mov sp, BaseOfStack mov ax, 34//read as 34 sectors because data.img is 29 bytes in 34 sectors when viewed in binary mov cx, 1 mov bx, Buf call ReadSector mov bp, Buf mov cx, 29 call Printlast: hlt jmp last ; es:bp --> string address; cx --> string lengthPrint: mov ax, 0x1301 mov bx, 0x0007 int 0x10 ret; no parameterResetFloppy://write floppy drive push ax push dx//push stack mov ah, 0x00 mov dl, [BS_DrvNum] int 0x13 pop dx pop ax//pop stack ret; ax --> logic sector number; cx --> number of sector; es:bx --> target addressReadSector://read floppy drive push bx push cx push dx push ax call ResetFloppy push bx push cx mov bl, [BPB_SecPerTrk] div bl mov cl, ah//remainder add cl, 1//Calculate sector number mov ch, al//quotient shr ch, 1//quotient shifted right into cylinder sign mov dh, al and dh, 1//head number mov dl, [BS_DrvNum] pop ax pop bxa mov ah, 0x02read: int 0x13 jc read//read again after failure pop ax pop dx pop cx pop bx retMsgStr db "MyDTOS! " MsgLen equ ($-MsgStr)Buf: times 510-($-$$) db 0x00 db 0x55, 0xaa

2. Verify it and see the print results

summary

1. When assembly code defines functions, stack space needs to be defined.

2. Logical sector numbers need to be translated into physical locations on disk before data can be read

3. The location of data on a physical diskette is uniquely determined by head number, cylinder number, and sector

4. Floppy disk data is read in sectors 512

III. Break through 512 bytes

What we're going to do here

The whole idea is

Q: Now the question is how do I find the target file in the root directory?

A. Memory comparison

1. Specify Source Start Address (DS:SI)

2. Specify destination start address (ES:DI)

3. Determine if each byte is equal within the expected length (CX)

Compare and jump commands in assembly

cmp cx,0--compare cx to 0

jz equal--if the comparison is true, jump to the equal tag

B. Need a memory comparison function, and then find out if there is a target file in the root directory

org 0x7c00jmp short startnopdefine: BaseOfStack equ 0x7c00 RootEntryOffset equ 19 RootEntryLength equ 14header: BS_OEMName db "D.T.Soft" BPB_BytsPerSec dw 512 BPB_SecPerClus db 1 BPB_RsvdSecCnt dw 1 BPB_NumFATs db 2 BPB_RootEntCnt dw 224 BPB_TotSec16 dw 2880 BPB_Media db 0xF0 BPB_FATSz16 dw 9 BPB_SecPerTrk dw 18 BPB_NumHeads dw 2 BPB_HiddSec dd 0 BPB_TotSec32 dd 0 BS_DrvNum db 0 BS_Reserved1 db 0 BS_BootSig db 0x29 BS_VolID dd 0 BS_VolLab db "D.T.OS-0.01" BS_FileSysType db "FAT12 "start: mov ax, cs mov ss, ax mov ds, ax mov es, ax mov sp, BaseOfStack mov ax, RootEntryOffset mov cx, RootEntryLength mov bx, Buf call ReadSector mov si, Target mov cx, TarLen mov dx, 0 call FindEntry cmp dx, 0 jz output jmp lastoutput: mov bp, MsgStr mov cx, MsgLen call Printlast: hlt jmp last exist:noexist: pop cx pop bp pop di retMemCmp: push si push di push axcompare://implementation of comparison function cmp cx, 0 jz equal mov al, [si] cmp al, byte [di] jz goon jmp noequalgoon: inc si inc di dec cx jmp compareequal:noequal: //not equal pop ax pop di pop si retPrint: mov ax, 0x1301 mov bx, 0x0007 int 0x10 ret; no parameterResetFloppy: push ax push dx mov ah, 0x00 mov dl, [BS_DrvNum] int 0x13 pop dx pop ax retReadSector: push bx push cx push dx push ax call ResetFloppy push bx push cx mov bl, [BPB_SecPerTrk] div bl mov cl, ah add cl, 1 mov ch, al shr ch, 1 mov dh, al and dh, 1 mov dl, [BS_DrvNum] pop ax pop bx mov ah, 0x02read: int 0x13 jc read pop ax pop dx pop cx pop bx retMsgStr db "MyDTOS! " MsgLen equ ($-MsgStr)Target db "MyDTOS! "TarLen equ ($-Target)Buf: times 510-($-$$) db 0x00 db 0x55, 0xaa

Printed results

From the print result, we know that the print function under label in the previous comparison function is called, from which we can get the cx register as, and we can get that the addresses of the two registers are equal.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report