Chức năng trong MIPS

Function calls are relatively simple in a high-level language, but actually involve multiple steps and instructions at the assembly level. | Functions in MIPS Function calls are relatively simple in a high-level language but actually involve multiple steps and instructions at the assembly level. The program s flow of control must be changed. Arguments and returning values are passed back and forth. Local variables can be allocated and destroyed. Today we ll see how these issues are handled in the MIPS architecture. There are new instructions for calling functions. Conventions are used for sharing registers between functions. Functions can make good use of a stack in memory. February 3 2003 2001-2003 Howard Huang 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In this example the main function calls fact twice and fact returns twice but to different locations in main. Each time fact is called the CPU has to remember the appropriate return address. Notice that main itself is also a function It is called by the operating system when you run the program. int main t1 fact 8 t2 fact s t3 t1 t2 int fact int n int i f 1 for i n i 1 i-- f f i return f February 3 2003 Functions in MIPS 2 Control flow in MIPS MIPS uses the jump-and-link instruction jal to call functions. The jal saves the return address the address of the next instruction in the dedicated register ra before jumping to the function. jal is the only MIPS instruction that can access the value of the program counter so it can store the return address PC 4 in ra. jal Fact To transfer control back to the caller the function just has to jump to the address that was stored in ra. jr ra The code on the next page shows the jal and jr instructions that are necessary for our factorial example. February 3 2003 Functions in MIPS

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.