简体   繁体   English

是否严格要求汇编才能成为操作系统的“最低”部分?

[英]Is assembly strictly required to make the “lowest” part of an operating system?

Im a mid-level(abstraction) programmer, and some months ago i started to think if i should reduce or increase abstraction(i've chosen to reduce). 我是一个中级(抽象)程序员,几个月前,我开始考虑应该减少还是增加抽象(我选择减少)。

Now, i think i've done most of the "research" about what i need, but still are a few questions remaining. 现在,我想我已经完成了大部分有关我需要的“研究”,但是仍然有几个问题。

Right now while im "doing effectively nothing", im just reinforcing my C skills (bought "K&R C Programing Lang"), and im thinking to (after feel comfortable) start studying operating systems(like minix) just for learning purposes, but i have an idea stuck in my mind, and i don't really know if i should care. 现在,虽然我“什么也没做”,但我只是在增强我的C技能(购买了“ K&R C编程语言”),然后我就想(在感到舒适之后)开始学习操作系统(例如minix)只是为了学习,但是我有一个主意,我真的不知道我是否应该关心。

In theory(i think, not sure), the higher level languages cannot refer to the hardware directly (like registers, memory locations, etc...) so the "perfect language" for the base would be assembly. 从理论上讲(我认为不确定),高级语言无法直接引用硬件(例如寄存器,内存位置等),因此基础语言的“完美语言”将是汇编语言。

I already studied assembly(some time ago) just to see how it was (and i stopped in the middle of the book due to the outdated debugger that the book used(Assembly Language Step By Step, for Linux!)) but from what i have read, i din't liked the language a lot. 我已经学习了汇编(前一段时间),只是为了了解它的状态(由于本书使用了过时的调试器,我停在了本书的中间(对于Linux来说,汇编语言逐步介绍了!)),但是我从那里已经读过,我不太喜欢这种语言。

So the question is simple: Can an operating system(bootloader/kernel) be programmed without touching in a single line of assembly, and still be effective? 所以问题很简单:操作系统(引导加载程序/内核)是否可以编程而无需碰触到任何汇编行,并且仍然有效?

Even if it can, it will not be "cross-architecture", will it? 即使可以,也不会是“跨架构”,是吗? (i386/arm/mips etc...) (i386 / arm / mips等...)

Thanks for your support 感谢您的支持

You can do a significant amount of the work without assembly. 您无需组装即可完成大量工作。 Linux or NetBSD doesnt have to be completely re-written or patched for each of the many targets it runs on. Linux或NetBSD不必针对其运行的许多目标中的每一个进行完全重写或修补。 Most of the code is portable and then there are abstraction layers and below the abstraction layer you find a target specific layer. 大多数代码是可移植的,然后有抽象层,在抽象层下面您可以找到目标特定层。 Even within the target specific layers most of the code is not asm. 即使在目标特定层内,大多数代码也不是asm。 I want to dispell this mistaken idea that in order to program registers or memory for a device driver for example that you need asm, you do not use asm for such things. 我想消除这种错误的想法,例如,为了对设备驱动程序的寄存器或存储器进行编程,例如您需要asm,请勿将asm用于此类事情。 You use asm for 1) instructions that a processor has that you cannot produce using a high level language. 您将asm用于1)处理器无法使用高级语言生成的指令。 or 2) where high level language generated code is too slow. 或2)高级语言生成的代码太慢。 For example in the ARM to enable or disable interrupts there is a specific instruction for accessing the processor state registers that you must use, so asm is required. 例如,在ARM中启用或禁用中断,有一条特定指令用于访问您必须使用的处理器状态寄存器,因此需要asm。 but programming the interrupt controller is all done in the high level language. 但是编程中断控制器都是用高级语言完成的。 An example of the second point is you often find in C libraries that memcpy and other similar heavily used library functions are hand coded asm because it is dramatically faster. 第二点的一个例子是,您经常在C库中发现memcpy和其他类似的频繁使用的库函数是手工编码的asm,因为它的速度要快得多。

Although you certainly CAN write and do anything you want in ASM, but you typically find that a high level language is used to access the "hardware directly (like registers, memory locations, etc...)". 尽管您当然可以在ASM中编写并执行任何您想做的事情,但是您通常会发现使用高级语言来直接访问“硬件(例如寄存器,内存位置等)”。 You should continue to re-inforce your C skills not just with the K&R book but also wander through the various C standards, you might find it disturbing how many "implementation defined" items there are, like bitfields, how variable sizes are promoted, etc. Just because a program you wrote 10 years ago keeps compiling and working using a/one specific brand of compiler (msvc, gcc, etc) doesnt mean the code is clean and portable and will keep working. 您不仅应该继续使用K&R书籍来增强您的C技能,而且还要徘徊在各种C标准中,您可能会发现它干扰了有多少“实现定义”项,例如位域,可变大小如何提升等。 。仅仅因为您10年前编写的程序一直在使用一种或多种特定品牌的编译器(msvc,gcc等)进行编译和工作,并不意味着该代码干净,可移植,并且将继续工作。 Unfortunately gcc has taught many very bad programming habits that shock the user when the find out they didnt know the language a decade or so down the road and have to redo how they solve problems using that language. 不幸的是,gcc教会了许多非常糟糕的编程习惯,当他们发现自己大概十年后仍不了解该语言时,就不得不震惊用户,而不得不重做如何使用该语言解决问题。

You have answered your question yourself in "the higher level languages cannot refer to the hardware directly". 您已经用“高级语言不能直接引用硬件”来回答自己的问题。

Whether you want it or not, at some point you will have to deal with assembly/machine code if you want to make an OS. 无论您是否想要,如果要制作OS,有时都必须处理汇编/机器代码。

Interrupt and exception handlers will have to have some assembly code in them. 中断和异常处理程序将必须在其中包含一些汇编代码。 So will need the scheduler (if not directly, indirectly). 因此将需要调度程序(如果不是直接,间接的话)。 And the system call mechanism. 以及系统调用机制。 And the bootloader. 还有引导程序。

What I've learned in the past reading websites and books is that: a) many programmers dislikes assembly language because of the reasons we all know. 我过去阅读网站和书籍时了解到的是:a)由于众所周知的原因,许多程序员不喜欢汇编语言。 b) the main programming language for OS's seems to be C and even C++ c) assembly language can be used to 'speed up code' after profiling your source code in C or C++ (language doesn't matter in fact) b)OS的主要编程语言似乎是C,甚至是C ++。c)在用C或C ++对源代码进行概要分析后,可以使用汇编语言来“加速代码”(实际上并不重要)

So, the combination of a mid level language and a low level language is in some cases inevitable. 因此,在某些情况下,中级语言和低级语言的组合是不可避免的。 For example there is no use to speed up code for waiting on user input. 例如,没有必要加快代码等待用户输入的时间。 If it matters to build the shortest and fastest code for one specific range of computers (AMD, INTEL, ARM, DIGITAL-ALPHA, ...) then you should use assembler. 如果为一台特定范围的计算机(AMD,INTEL,ARM,DIGITAL-ALPHA等)构建最短和最快的代码很重要,则应使用汇编器。 My opinion... 我的意见...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM