在阅读和学习开源操作系统时,我偶然发现了一种在汇编中调用“方法”的极其复杂的方法。它使用“ret”指令来调用库方法来执行以下操作:
push rbp ; rsp[1] = rbp
mov rbp, .continue ; save return label to rbp
xchg rbp, QWORD [rsp] ; restore rbp and set rsp[1] to return label
push rbp ; rsp[0] = rbp
mov rbp, 0x0000700000000000 + LIB_PTR_TABLE.funcOffset ; rbp = pointer to func pointer
mov rbp, QWORD [rbp] ; rbp = func pointer
xchg rbp, QWORD [rsp] ; restore rbp and set rsp[0] to func pointer
; "call" library by "returning" to the address we just planted …Run Code Online (Sandbox Code Playgroud) 我目前正在编写自己的操作系统(只是为了好玩,我 16 岁)并且我创建的 outprint 函数有问题。我想更改文本颜色(不是背景颜色),但它不起作用。
我已经创建了自己的printf函数SystemOutPrint/ln(以 Java 命名,因为那是我的“主要”语言,对于那些受伤的人)并发现我可以通过在 AH 寄存器中写入来更改背景颜色。在此功能之前,我在内核中什么也没做,引导加载程序只将 GDT、LDT 和模式从 16 位设置为 32 位。所以在 mov ebx, 0xb8000 之前,视频内存是不变的。
相关代码:
kmain:
mov ebx, 0xb8000 ;Video Memory
mov esi, kernelVersion
mov ah, 0x0
;setting ah to 0x0 is not neccessary, because its the default but if you
;would put in A for example it would be light green, etc.
call SystemOutPrintln
SystemOutPrintln:
mov ecx, ebx
.printChar:
lodsb
test al,al
jz .newLine
or eax,0x0F00
mov word …Run Code Online (Sandbox Code Playgroud)