Skip to content

Commit 900c485

Browse files
committed
完成项目5
1 parent 96ae8e4 commit 900c485

File tree

7 files changed

+118
-11
lines changed

7 files changed

+118
-11
lines changed
51 Bytes
Binary file not shown.

项目5_实现系统调用/src/lib/systema.asm

+24-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ sys_showOuch:
1717
mov es, ax ; 置ES=DS
1818
mov cx, 4 ; CX = 串长
1919
mov ax, 1301h ; AH = 13h(功能号)、AL = 01h(光标置于串尾)
20-
mov bx, 0007h ; 页号为0(BH = 0) 黑底白字(BL = 07h)
20+
mov bx, 0038h ; 页号为0(BH = 0) 黑底白字(BL = 07h)
2121
mov dh, 12 ; 行号
2222
mov dl, 38 ; 列号
2323
int 10h ; BIOS的10h功能:显示一行字符
@@ -45,10 +45,33 @@ sys_toLower:
4545
pop es ; 丢弃参数
4646
ret
4747

48+
[extern atoi]
4849
sys_atoi:
50+
push es ; 传递参数
51+
push dx ; 传递参数
52+
call dword atoi
53+
pop dx ; 丢弃参数
54+
pop es ; 丢弃参数
4955
ret
5056

57+
[extern itoa_buf]
5158
sys_itoa:
59+
push es ; 传递参数buf
60+
push dx ; 传递参数buf
61+
mov ax, 0
62+
push ax ; 传递参数base
63+
mov ax, 10 ; 10进制
64+
push ax ; 传递参数base
65+
mov ax, 0
66+
push ax ; 传递参数val
67+
push bx ; 传递参数val
68+
call dword itoa_buf
69+
pop bx ; 丢弃参数
70+
pop ax ; 丢弃参数
71+
pop ax ; 丢弃参数
72+
pop ax ; 丢弃参数
73+
pop dx ; 丢弃参数
74+
pop es ; 丢弃参数
5275
ret
5376

5477
[extern strlen]

项目5_实现系统调用/src/lib/systemc.c

+66
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include <stdint.h>
2+
#define bool unsigned short
3+
#define true 1
4+
#define false 0
25

36
/* 将字符串中的小写字母转换为大写 */
47
void toupper(char* str) {
@@ -18,4 +21,67 @@ void tolower(char* str) {
1821
str[i] = str[i]-'A'+'a';
1922
i++;
2023
}
24+
}
25+
26+
/* 翻转字符串 */
27+
void my_reverse(char str[], int len)
28+
{
29+
int start, end;
30+
char temp;
31+
for (start = 0, end = len - 1; start < end; start++, end--) {
32+
temp = *(str + start);
33+
*(str + start) = *(str + end);
34+
*(str + end) = temp;
35+
}
36+
}
37+
38+
/* 将数字转换为字符串并放在str中 */
39+
char* itoa_buf(int num, int base, char* str)
40+
{
41+
int i = 0;
42+
bool isNegative = false;
43+
44+
/* A zero is same "0" string in all base */
45+
if (num == 0) {
46+
str[i] = '0';
47+
str[i + 1] = '\0';
48+
return str;
49+
}
50+
51+
/* negative numbers are only handled if base is 10
52+
otherwise considered unsigned number */
53+
if (num < 0 && base == 10) {
54+
isNegative = true;
55+
num = -num;
56+
}
57+
58+
while (num != 0) {
59+
int rem = num % base;
60+
str[i++] = (rem > 9) ? (rem - 10) + 'A' : rem + '0';
61+
num = num / base;
62+
}
63+
64+
/* Append negative sign for negative numbers */
65+
if (isNegative) {
66+
str[i++] = '-';
67+
}
68+
69+
str[i] = '\0';
70+
71+
my_reverse(str, i);
72+
73+
return str;
74+
}
75+
76+
/* 将十进制数字字符串转换为整数 */
77+
int atoi(char *str) {
78+
int res = 0; // Initialize result
79+
80+
// Iterate through all characters of input string and
81+
// update result
82+
for (int i = 0; str[i] != '\0'; ++i) {
83+
res = res*10 + str[i] - '0';
84+
}
85+
// return result.
86+
return res;
2187
}

项目5_实现系统调用/src/merge.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dd if=./temp/stone_topright.bin of=JedOS_v1.3.img bs=512 seek=20 count=2 2> /dev
3636
dd if=./temp/stone_bottomleft.bin of=JedOS_v1.3.img bs=512 seek=22 count=2 2> /dev/null
3737
dd if=./temp/stone_bottomright.bin of=JedOS_v1.3.img bs=512 seek=24 count=2 2> /dev/null
3838
dd if=./temp/interrupt_caller.bin of=JedOS_v1.3.img bs=512 seek=26 count=1 2> /dev/null
39-
dd if=./temp/syscall_test.bin of=JedOS_v1.3.img bs=512 seek=27 count=2 2> /dev/null
39+
dd if=./temp/syscall_test.bin of=JedOS_v1.3.img bs=512 seek=27 count=3 2> /dev/null
4040

4141

4242
echo "[+] Done."

项目5_实现系统调用/src/stringio.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void readToBuf(char* buffer, uint16_t maxlen) {
9898
}
9999

100100
/* 将整数转为指定进制的字符串 */
101-
const char* itoa(int val, int base) {
101+
char* itoa(int val, int base) {
102102
if(val==0) return "0";
103103
static char buf[32] = {0};
104104
int i = 30;

项目5_实现系统调用/src/usrprog/syscall_test.asm

+25-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ Start:
1414
int 10h ; 清屏
1515

1616
PRINT_IN_POS hint_all, hint_all_len, 0, 0
17+
mov ah, 0
18+
int 16h
19+
cmp al, 27 ; 按下ESC
20+
je QuitUsrProg ; 直接退出
1721

1822
PRINT_IN_POS hint0, hint_len, 2, 0
19-
mov ah, 00h ; 系统调用功能号
23+
mov ah, 00h ; 系统调用功能号ah=00h,显示OUCH
2024
int 21h
2125
mov ah, 0
2226
int 16h
@@ -28,7 +32,7 @@ Start:
2832
mov es, ax ; es=cs
2933
mov dx, upper_lower ; es:dx=串地址
3034
PRINT_IN_POS upper_lower, 14, 3, 0
31-
mov ah, 01h ; 系统调用功能号
35+
mov ah, 01h ; 系统调用功能号ah=01h,大写转小写
3236
int 21h
3337
PRINT_IN_POS upper_lower, 14, 4, 0
3438
mov ah, 0
@@ -40,7 +44,7 @@ Start:
4044
mov ax, cs
4145
mov es, ax ; es=cs
4246
mov dx, upper_lower ; es:dx=串地址
43-
mov ah, 02h ; 系统调用功能号
47+
mov ah, 02h ; 系统调用功能号ah=02h,小写转大写
4448
int 21h
4549
PRINT_IN_POS upper_lower, 14, 5, 0
4650
mov ah, 0
@@ -49,16 +53,29 @@ Start:
4953
je QuitUsrProg ; 直接退出
5054

5155
PRINT_IN_POS hint3, hint_len, 2, 0
52-
mov ah, 03h ; 系统调用功能号
53-
int 21h
56+
mov ax, cs
57+
mov es, ax ; es=cs
58+
mov dx, number_buf
59+
mov ah, 03h ; 系统调用功能号ah=03h,atoi
60+
int 21h ; ax=数值
61+
mov bx, 1
62+
add bx, ax ; bx=ax+1,然后使用下一个系统调用来检查其结果
5463
mov ah, 0
5564
int 16h
5665
cmp al, 27 ; 按下ESC
5766
je QuitUsrProg ; 直接退出
5867

5968
PRINT_IN_POS hint4, hint_len, 2, 0
60-
mov ah, 04h
69+
mov ax, cs
70+
mov es, ax ; es=cs
71+
mov dx, number_buf
72+
mov ah, 04h ; 系统调用功能号ah=04h,itoa
73+
int 21h ; es:dx=转换后的数字字符串
74+
mov ch, 6
75+
mov cl, 0
76+
mov ah, 05h
6177
int 21h
78+
6279
mov ah, 0
6380
int 16h
6481
cmp al, 27 ; 按下ESC
@@ -70,7 +87,7 @@ Start:
7087
mov dx, test_message1 ; es:dx=串地址
7188
mov ch, 19 ; 行号
7289
mov cl, 0 ; 列号
73-
mov ah, 05h ; 系统调用功能号
90+
mov ah, 05h ; 系统调用功能号ah=05h
7491
int 21h ; 显示第一条字符串
7592
mov dx, test_message2
7693
mov ch, 20
@@ -99,5 +116,6 @@ DataArea:
99116

100117
upper_lower db 'AbCdEfGhIjKlMn', 0 ; 字符串以'\0'结尾
101118

119+
number_buf db '12345', 0 ; 字符串以'\0'结尾
102120
test_message1 db 'This is a test message,'
103121
test_message2 db 'printed using `ah=05h` and `int 21h`.'

项目5_实现系统调用/src/usrproginfo.asm

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ UserProgInfo:
2424
UsrProgInfoBlock 3, 'stone_botleft', 1024, 0, 1, 5, offset_usrprog3
2525
UsrProgInfoBlock 4, 'stone_botright', 1024, 0, 1, 7, offset_usrprog4
2626
UsrProgInfoBlock 5, 'interrupt_caller', 512, 0, 1, 9, offset_intcaller
27-
UsrProgInfoBlock 6, 'syscall_test', 1024, 0, 1, 10, offset_syscalltest
27+
UsrProgInfoBlock 6, 'syscall_test', 1536, 0, 1, 10, offset_syscalltest
2828

2929
SectorEnding:
3030
times 512-($-$$) db 0

0 commit comments

Comments
 (0)