当前位置:网站首页>Implementation of monitor program with assembly language
Implementation of monitor program with assembly language
2022-06-30 10:23:00 【Yangxiangrui】
1 Introduce the monitoring program
The monitor is simply the operating system . For a particularly simple operating system, a monitor is waiting for a user's command and then executing .
The monitoring program described here can run user programs placed on floppy disks . You can receive ls Instructions , Output user program information . Basically, there is no function ……
All source floppy disks , Extraction code :pn49
For an introduction to the user program, please see :x86 introduction - Take over bare metal control
2 Software support
operating system :Ubuntu 20.04 LTS
Virtual machine software :VirtualBox 6.1.6(VB This program has bug, Direct use is recommended bochs, At present, the monitoring program has not triggered VB Of bug)
Debug Software :bochs x86 Simulator 2.6.11
compiler :nasm 2.14.02
compiler :gcc 9.3.0
3 Run the monitor
The traditional practice is to start with a runnable program , Otherwise, I won't see a lot of results .
Enter project directory , then make Generate the required files , Use make build Making floppy disks , then make run use bochs Run the written program .
$ make # Generate all binaries
$ make build # Use your own program to make floppy disks
$ make run # function bochs virtual machine
$ make clean # Clear all binaries , It does not include the prepared floppy disk
For those familiar with Makefile My little friend looks directly at makefile You can understand the compilation process , If you want to compile directly from the command line, use the following instructions .
First, generate pure binary code of all programs , Corresponding make Instructions
# Generate binaries for all user programs
$ nasm -f bin soft0.asm -o soft0.img
$ nasm -f bin soft1.asm -o soft1.img
$ nasm -f bin soft2.asm -o soft2.img
$ nasm -f bin soft3.asm -o soft3.img
# Generate the monitor binaries , Use directly as a floppy disk
$ nasm -f bin load.asm -o flop.img
# Generate user program information table
$ nasm -f bin table.asm -o table.img
# I wrote a program to make floppy disks C Program
$ gcc -o make_flop write.c
And then use make_flop Put the user program 、 The user program information is written to the floppy disk where the monitor program is stored , Corresponding make build.
./make_flop
Finally, use bochs Just run this floppy disk , Corresponding to make run.
bochs
bochs Configuration file for bochsrc as follows
floppya: 1_44="flop.img", status=inserted #Use FLoppy Disk A
boot: floppy
Because the function of echo after input is not realized , So you can't see what I entered , In fact, the five characters I type in are s、0、1、2、3 , You can execute the command without entering .
4 Monitor interpretation
The monitoring procedures are divided into A module : Input 、 Run the user program 、 Read floppy disk 、 Display the user information table 、 Clear the screen
First, let's introduce the simplest Clear the screen , Removing stack pressing and other operations only use two lines of assembly . I was really sick at first , Write the assembly code manually to clear the entire display area .
clean:
push ax;
mov ax, 03h; Function number
int 10h
pop ax;
ret;
Then there is the second simple Input and judgment input The process of . It's basically a while Cycle and add a few if Judgment is over , If the character entered is s Then jump to the information table that displays the user program , Otherwise, execute the function that calls the user program , After that, it will return to input Receive the user's next instruction .
input:
; Here you begin to read the input from the keyboard , Determine which program to run
mov ah, 0; Read a character from the keyboard , Stored in the al
int 16h;
mov ah, 0;
; If the character entered is s, Then display the program information in the floppy disk
cmp al, 's'
je show_table
mov ah, 0;
sub al, '0';
call run;
jmp input
Here's the main thing Execute user program The process of . This function takes a parameter ax, Indicates the number of the user program , from 0 To 3( My parameters are passed in registers for convenience , Later, C Mixed programming with assembly must be done on the stack ).
First, the screen will be cleared before each execution ( call clean).
MBR The monitoring program is stored in the logical sector 0, The information table of the user program is stored in the logical sector 1, User programs 0 Stored in logical sector 2, User programs 1 Stored in logical sector 3, And so on . So the monitor program first puts the user program 0 Called into memory 0x7e00 It's about , User programs 1 Just put it in 0x8000 It's about , And so on .( The operations of reading floppy disk are bug, Logical sector exceeded 17 I can't read , I solved this problem later , Don't look here )
; Read the program from the floppy disk
add ax, 2; The first 0 The first program is in 2 A logical sector
call load; use first load Call the program into memory
sub ax, 2; Reuse ax It means the first one 0 Procedures
Then assign the corresponding physical address of the user program to ax register , Such as user programs 0 Namely 0x7e00. And then directly jmp bx, Be careful before jumping , To assign the return address to bx, The user program also directly jmp bx return . The whole process is cs All segment registers are 0, So all jumps are made using physical addresses .
; The offset address is calculated below : (cx:)ax = ax * 0x200(bx)
mov bx, 0x200;
mul bx;
add ax, 0x8000;
; mov dx, ax;
; Start passing parameters , And jump to the program
mov cx, 0;
mov bx, end;
add bx, 0x7c00
jmp ax; This relative offset is cs: ax
After the jump is completed, there is nothing to monitor the program , The user program is the last experiment “ Take over bare metal control ” A modified version of the user program for . When the execution is over, you can jmp bx, Then the display range is no longer full screen , It is 1/4 Screens .
Display the user program information table This function has reached C The kernel rewrites , Plus before FAT12 File system emulator dir The command also displays this information , Therefore, the concrete implementation here does not make much sense . Just say the meaning of the displayed information .
On the picture below 4 Information about user programs , Each line shows a user program . Take the first act :
The first number indicates the number of the user program :0
The second number represents the first sector of the user program ( logical sector ):2
The third number indicates the number of sectors occupied by the user program :1

Digression
I didn't expect that there were people squatting on my blog waiting for my article to update , I can only say that it is really hard for you to copy your homework …… I will try to update the article as soon as possible , Try to update before you hand in the experiment .
At present, the source code and various binary files are put on Baidu network disk , After all, the file is very small and the download is very fast , Then consider putting it in github.
边栏推荐
- Leetcode question brushing (II) -- sorting (go Implementation)
- The digital collection of sunanmin's lotus heart clearing was launched on the Great Wall Digital Art
- Some domestic image sources
- train_de.py: error: argument --save_steps: invalid int value: ‘$[$[889580/128/4]*10/2]‘
- 调试方法和技巧详解
- GD32 RT-Thread OTA/Bootloader驱动函数
- 7. development of mobile login function
- Nlopt -- Nonlinear Optimization -- principle introduction and application method
- Questions about cookies and sessions
- 逸仙电商发布一季报:坚持研发及品牌投入,实现可持续高质量发展
猜你喜欢

unable to convert expression into double array

Great Wall digital art digital collection platform releases the creation Badge

Why can't you rob scientists of NFT

机器人系统动力学——惯性参数

逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展

A brief introduction to database mysql

Test memory read rate

Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】

“昆明城市咖啡地图”活动再度开启

Splendid China: public welfare tourism for the middle-aged and the elderly - entering Foshan nursing home
随机推荐
郭琳加冕 2022第三季完美大师 全球人气季军
技能梳理[email protected]体感机械臂
1033 To Fill or Not to Fill
技能梳理[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled
逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展
GD32 RT-Thread RTC驱动函数
Implementation of iterative method for linear equations
Regular expression Basics
GNN动手实践(二):复现图注意力网络GAT
Tooltips in the era of touch
Dyson design award, changing the world with sustainable design
跳跃表介绍
KOREANO ESSENTIAL打造气质职场范
Article content cannot be copied
Quick completion guide for mechanical arm (V): end effector
About the split and join operations of strings
Setting up the d2lbook environment for Li Mu's "hands on learning and deep learning"
Why can't you rob scientists of NFT
Leetcode question brushing (IV) -- greedy thought (go Implementation)
Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】