任何人都可以從堆疊的角度解釋基于手臂的匯編代碼嗎?特別是在呼叫“main”、“save_context”和“resume”部分之前的“reset_handler”的堆疊視圖?(請注意,我知道代碼在做什么,但我無法理解或想象代碼運行時堆疊的外觀或行為方式)。
*/ asm.s */
.global main, process, process_size
.global reset_handler, context_switch, running
reset_handler:
ldr r0, =process
ldr r1, =process_size
ldr r2, [r1, #0]
add r0, r0, r2
mov sp, r0
bl main
context_switch:
save_context:
stmfd sp!, {r0-r12, lr}
ldr r0, =running
ldr r1, [r0, #0]
str sp, [r1, #4]
resume:
ldr r0, =running
ldr r1, [r0, #0]
ldr sp, [r1, #4]
ldmfd sp!, {r0-r12, lr}
mov pc, lr
*/ cfile.c */
#define SIZE 2048
typedef struct process
{
struct process *next;
int *saved_stack;
int running_stack[SIZE];
}PROC;
int process_size = sizeof(PROC);
PROC process, *running;
main()
{
running = &process;
context_switch();
}
uj5u.com熱心網友回復:
作為背景——處理器的暫存器幾乎定義了它在做什么。它們通常被稱為context. 最重要的是程式計數器pc,它包含下一條指令的記憶體地址;但是它們都很重要。那么讓我們看看如何保存背景關系:
save_context:
stmfd sp!, {r0-r12, lr}
-- that instruction saved to processor context to the stack
-- it could be broken down as follows:
-- sp = sp - 14*4 4, because each register is 4 bytes, and there are 14 specfied
-- for (i=0; i < 13; i ) sp[i] = r(i);
-- sp[i] = lr `lr` is special, it holds the return address of the instruction that called us.
ldr r0, =running
-- put the address of the variable `running` into r0
ldr r1, [r0, #0]
-- load r1 with the memory address from r0. So r1 = running.
str sp, [r1, #4]
-- store the stack pointer (sp) in the `saved_sp` field of running.
-- so these three instructions perform: running->saved_stack = sp;
-- now we "fall through" to load, or `resume` a context.
resume:
ldr r0, =running
ldr r1, [r0, #0]
ldr sp, [r1, #4]
-- the inverse of the above, these three instructions effectively perform:
-- sp = running->saved_stack
ldmfd sp!, {r0-r12, lr}
-- this is the complimentary operation to the complicated save one above; but this time it is:
-- for (i=0; i < 13; i ) r(i) = sp[i];
-- lr = sp[i];
-- sp = 14*4;
mov pc, lr
-- this is a return instruction, where the program counter is loaded with the contents of the link register `lr`.
-- so, with this, it will return to main just after the call to context_switch
上面有一些模糊位: sp[i]必須將 i 縮放一個暫存器的大小(4);但較早的 sp 減少了 14*4。由于偽 C 不是真實的,因此看起來還可以。
uj5u.com熱心網友回復:
偽代碼:
reset_handler:
stack_pointer = &process process_size;
call main
這意味著堆疊指標指向&process.running_stack[SIZE]. 當堆疊指標像這樣指向堆疊緩沖區的最末端時,這意味著堆疊完全為空(ARM 使用降序堆疊)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406277.html
標籤:
