What happens when a computer program runs?

后端 未结 4 1431
清歌不尽
清歌不尽 2020-11-29 14:13

I know the general theory but I can\'t fit in the details.

I know that a program resides in the secondary memory of a computer. Once the program begins execution it

4条回答
  •  一向
    一向 (楼主)
    2020-11-29 14:52

    The stack

    In X86 architercture the CPU executes operations with registers. The stack is only used for convenience reasons. You can save the content of your registers to stack before calling a subroutine or a system function and then load them back to continue your operation where you left. (You could to it manually without the stack, but it is a frequently used function so it has CPU support). But you can do pretty much anything without the stack in a PC.

    For example an integer multiplication:

    MUL BX
    

    Multiplies AX register with BX register. (The result will be in DX and AX, DX containing the higher bits).

    Stack based machines (like JAVA VM) use the stack for their basic operations. The above multiplication:

    DMUL
    

    This pops two values from the top of the stack and multiplies tem, then pushes the result back to the stack. Stack is essential for this kind of machines.

    Some higher level programming languages (like C and Pascal) use this later method for passing parameters to functions: the parameters are pushed to the stack in left to right order and popped by the function body and the return values are pushed back. (This is a choice that the compiler manufacturers make and kind of abuses the way the X86 uses the stack).

    The heap

    The heap is an other concept that exists only in the realm of the compilers. It takes the pain of handling the memory behind your variables away, but it is not a function of the CPU or the OS, it is just a choice of housekeeping the memory block wich is given out by the OS. You could do this manyually if you want.

    Accessing system resources

    The operating system has a public interface how you can access its functions. In DOS parameters are passed in registers of the CPU. Windows uses the stack for passing parameters for OS functions (the Windows API).

提交回复
热议问题