当前位置:网站首页>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.
边栏推荐
- 机器人系统动力学——惯性参数
- 2022 Season 6 perfect children's model toxon division finals came to a successful conclusion
- MIT-6874-Deep Learning in the Life Sciences Week6
- Highlight display of Jinbei LB box, adhering to mini special effects
- AttributeError: ‘Version‘ object has no attribute ‘major‘
- GNN hands on practice (II): reproduction graph attention network gat
- Oracle creates a stored procedure successfully, but the compilation fails
- Applying applet container technology to IOT ecological construction
- ‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
- How does the diode work?
猜你喜欢

Xinguan has no lover, and all the people benefit from loving deeds to warm the world -- donation to the public welfare action of Shangqiu children's welfare home

Launch of Rural Revitalization public welfare fund and release of public welfare bank for intangible cultural heritage protection of ancient tea tree

UAV project tracking record 83 -- PCB diagram completion
![[C language quick start] let you know C language and get started with zero basics ③](/img/ab/8dee3ee264429fc57ce18fdf8b2ea0.png)
[C language quick start] let you know C language and get started with zero basics ③

MySQL index, transaction and storage engine of database (2)

KOREANO ESSENTIAL打造气质职场范

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

开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

乡村振兴公益基金启动暨古茶树非遗保护公益行发布

Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi
随机推荐
Koreano essential creates a professional style
What is the real performance of CK5, the king machine of CKB?
[JVM] brief introduction to CMS
MySQL index, transaction and storage engine of database (3)
技能梳理[email protected]在oled上控制一条狗的奔跑
Magnetic levitation 3D lamp
Rider does not prompt after opening unity script
MySQL index, transaction and storage engine of database (2)
Test memory read rate
WGet -- 404 not found due to spaces in URL
JS get the substring of the specified character position and the specified character position interval of the specified string [simple and detailed]
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
磁悬浮3D灯
‘Failed to fetch current robot state‘ when using the ‘plan_ kinematic_ path‘ service #868
KOREANO ESSENTIAL打造气质职场范
Description of event object
MIT-6874-Deep Learning in the Life Sciences Week5
Oracle creates a stored procedure successfully, but the compilation fails
打通供应链 深圳礼品展助跨境电商寻破局之道
“昆明城市咖啡地圖”活動再度開啟