Assembly language - course design 2 - restart the computer and boot the existing operating system

The installation program and the code shown on the page were completed last time. This time, the function development began. The first two functions of this development are restarting the computer and booting the existing operating system.

This time, the function development is based on the last code. However, the code has been adjusted and a boot program has been added. Why do you do this? This will be explained below.

First, the adjusted code is given. The functions are the same as those in chapter (I). They are the installation program and the welcome information display on the page. The difference is that the boot program is added and the task program is written to sector 2.

boot segment                ; Read the task program of 0 track 0 side 2 sector in the floppy disk into 0:7e00 Then jump to the address to execute the program
    mov ax, 0               ; 7e00 = 7c00 + 200h,The offset address is the same as 7 c00 The address difference is 512 bytes, leaving 512 bytes of space,
    mov es, ax              ; Other function development will use the free 512 byte space
    mov bx, 7e00h
​
    mov al, 1
    mov ch, 0
    mov cl, 2
    mov dl, 0
    mov dh, 0
    mov ah, 2
    int 13h
    
    mov ax, 0
    push ax
    mov ax, 7e00h
    push ax
    retf
​
db 512 dup(0)
boot ends
​
code segment
    jmp short start                 ; Jump to start Execution procedure
    
    option1: db '1) reset pc', 0
    option2: db '2) start system', 0
    option3: db '3) clock', 0
    option4: db '4) set clock', 0
​
    start:
        mov bp, 0
        mov bx, offset option1
        call printf
​
        mov bp, 160
        mov bx, offset option2
        call printf
​
        mov bp, 320
        mov bx, offset option3
        call printf
​
        mov bp, 480
        mov bx, offset option4
        call printf
​
        stop: jmp short stop        ; Dead loop, pauses the current position and does not continue the program down
    printf:
        mov ax, 0b800h
        mov es, ax
        mov si, 0
        mov ch, 0
        va: mov cl, cs:[bx + 07e00h]    ; Gets the display character plus 7 e00
        jcxz ok
        mov es:[si + bp], cl
        add si, 2
        inc bx
        jmp va
        ok: ret
db 512 dup(0)                       ; Define 512 byte 0 data, because writing to the floppy disk is 512 bytes in a sector, and the program is not enough
code ends                           ; 512 Byte, other supplementary 0
​
; Write floppy disk code snippet
setup segment
    en:
        mov ax, code                ; Write the segment address of the code. The task program is in code In paragraph
        mov es, ax
        mov bx, 0                   ; Offset address, write data from the first byte
        
        mov al, 1                   ; The number of sectors written. Here, write 1 sector
        mov ch, 0                   ; Track number
        mov cl, 2                   ; Sector code, write sector 2
        mov dl, 0                   ; Drive letter, floppy drive starts from 0, 0: floppy drive A
        mov dh, 0                   ; Head number (face)
        
        mov ah, 3                   ; int 13h Function number of (3 indicates write Sector)
        int 13h
        
        mov ax, boot                ; Will boot program boot Write 0 track 0 side 1 sector
        mov es, ax
        mov bx, 0
        
        mov al, 1
        mov ch, 0
        mov cl, 1
        mov dl, 0
        mov dh, 0
​
        mov ah, 3
        int 13h
        
        mov ax, 4c00h
        int 21h
setup ends
end en

It can be seen from the code given above that there is no difference from the last code except that the task program is written to sector 2, the data of sector 2 is read to 0:7e00 in sector 1, and the program is executed here. Then the problem comes. Last time, the program was read into 0:7c00 by default and executed. There is no problem. The program is executed normally. The welcome page display information is displayed normally. Why should the code be changed this time? Load the task program to 0:7e00 for execution, leaving a space of 512 bytes. The reason why I do this will be explained in the following developed functions.

User input "1", "2", "3" and "4" correspond to different functions. The first thing to deal with is to obtain the information entered by the user, and use int 16h interrupt to read the keyboard buffer.

; Get keyboard input
input: mov ah, 0
       int 16h
​
       cmp al, '1'          ; 1: Restart the computer and jump to the label reset Execute the restart computer program
       je reset
       cmp al, '2'          ; 2: Boot the existing operating system and jump to the label system Execute a program that boots an existing operating system
       je system
​
       jmp short input

The next step is to develop the functions of restarting the computer program and booting the existing operating system program. First, let's look at the function of restarting the computer program.

reset: mov ax, 0ffffh           ; To restart the computer is to make the program jump to FFFF:0 Execute at the unit, which will the operation after power on
       push ax                  ; Execute it again, and the effect is to restart the computer
       mov ax, 0
       push ax
       retf

Restarting the computer is relatively simple. Just jump the program to FFFF:0 unit for execution. The following is the function of developing and guiding the existing operating system program, which took me a long time to complete.

; take c Disk 0 track 0 side 1 sector read into memory 0:7c00 place,Then jump to 0:7c00 Executive procedure
system: mov ax, 0
        mov es, ax
        mov bx, 7c00h
        
        mov al, 1
        mov ch, 0
        mov cl, 1
        mov dl, 80h                 ; Hard disk C:80h
        mov dh, 0
        mov ah, 2
        int 13h
        
        mov ax, 0
        push ax
        mov ax, 7c00h
        push ax
        retf

In terms of code, it is not complicated, that is, read the sector 1 of track 0, plane 0 of Disk C to 0:7c00, and then jump to this place to execute the program. The code structure is the same as that of boot code segment, but the read data is different from the address read into memory.

The problem I encountered at the beginning was that I read the data of track 0, plane 0 and sector 1 of Disk C to 0:7e00, and then jumped to the place to execute the program, but the execution failed anyway. I couldn't boot the existing operating system. I tried back and forth many times, so I changed the way, read the task program to 0:7e00, and read the sector 1 of track 0, plane 0 of Disk C to 0:7c00, At this time, the existing operating system can be booted normally, which is why I adjusted the code, wrote the task program to sector 2, and then read the data of sector 2 to 0:7e00 for execution through the boot program. The purpose is to free 512 bytes at 7c00 for the boot program of the operating system. If anyone knows why, please don't hesitate to comment.

Finally, paste the complete code, execute and see the effect. Press "1": restart the computer; Press "2": boot the existing operating system.

boot segment
    mov ax, 0
    mov es, ax
    mov bx, 7e00h
​
    mov al, 1
    mov ch, 0
    mov cl, 2
    mov dl, 0
    mov dh, 0
    mov ah, 2
    int 13h
    
    mov ax, 0
    push ax
    mov ax, 7e00h
    push ax
    retf
​
db 512 dup(0)
boot ends
​
code segment
    jmp short start                 ; Jump to start Execution procedure
    
    option1: db '1) reset pc', 0
    option2: db '2) start system', 0
    option3: db '3) clock', 0
    option4: db '4) set clock', 0
    
    start:
        mov bp, 0
        mov bx, offset option1
        call printf
​
        mov bp, 160
        mov bx, offset option2
        call printf
​
        mov bp, 320
        mov bx, offset option3
        call printf
​
        mov bp, 480
        mov bx, offset option4
        call printf
        
        ; Get keyboard input
 input: mov ah, 0
        int 16h
​
        cmp al, '1'
        je reset
        cmp al, '2'
        je system
​
        jmp short input
​
        stop: jmp short stop        ; Dead loop, pauses the current position and does not continue the program down
    printf:
        mov ax, 0b800h
        mov es, ax
        mov si, 0
        mov ch, 0
        va: mov cl, cs:[bx + 07e00h]    ; Gets the display character plus 7 e00
        jcxz ok
        mov es:[si + bp], cl
        add si, 2
        inc bx
        jmp va
        ok: ret
    
    reset:
        mov ax, 0ffffh
        push ax
        mov ax, 0
        push ax
        retf
        
    system:
        ; take c Disk 0 track 0 side 1 sector read into memory 0:7c00 place,Then jump to 0:7c00 Executive procedure
        mov ax, 0
        mov es, ax
        mov bx, 7c00h
        
        mov al, 1
        mov ch, 0
        mov cl, 1
        mov dl, 80h                 ; Hard disk C:80h
        mov dh, 0
        mov ah, 2
        int 13h
        
        mov ax, 0
        push ax
        mov ax, 7c00h
        push ax
        retf
        
db 512 dup(0)                       ; Define 512 byte 0 data, because writing to the floppy disk is 512 bytes in a sector, and the program is not enough
code ends                           ; 512 Byte, other supplementary 0
​
; Write floppy disk code snippet
setup segment
    en:
        mov ax, code                ; Write the segment address of the code. The task program is in code In paragraph
        mov es, ax
        mov bx, 0                   ; Offset address, write data from the first byte
        
        mov al, 1                   ; The number of sectors written. Here, write 1 sector
        mov ch, 0                   ; Track number
        mov cl, 2                   ; Fan area code
        mov dl, 0                   ; Drive letter, floppy drive starts from 0, 0: floppy drive A
        mov dh, 0                   ; Head number (face)
        
        mov ah, 3                   ; int 13h Function number of (3 indicates write Sector)
        int 13h
        
        mov ax, boot
        mov es, ax
        mov bx, 0
        
        mov al, 1
        mov ch, 0
        mov cl, 1
        mov dl, 0
        mov dh, 0
​
        mov ah, 3
        int 13h
        
        mov ax, 4c00h
        int 21h
setup ends
end en

This completes function 1 and function 2, and then develops function 3 and function 4 next time.

You have created your own personal website. Welcome to shoot bricks. Address: http://www.techxiaoz.xyz

Official account

 

Keywords: Assembly Language

Added by ValdouaD on Tue, 11 Jan 2022 10:50:02 +0200