HOT

07 计算机网络--嵌套的子程序调用

1 .Nested subroutine call

2 .Handling recursive procedure calls Example. Compute factorial (n) int fact (int n) { if (n < 1) return (1); else return (n * fact(n-1)) } (Plan) Put n in \$a0. Result should be available in \$v0. {Structure of the fact procedure} fact: subi \$sp, \$sp, 8 \$sp sw \$ra, 4(\$sp) {why?} a0 ra sw \$a0, 0(\$sp) \$fp OLD NEW \$sp (current top of the stack)

3 . calling program procedure fact … … 4000 push ra 996 a0 = n (3) 4004 push a0 jal fact (4000) if n<1 then {v0=1 1000 read fact(n) from v0 Return to ra} 1004 a0=n-1 jal fact (4000) 4024 v0=old a0* fact(n-1) return to old ra \$sp a0 = 1 n=3 a0 ra = 4024 a0 = 2 ra= 4024 result v0 a0 = 3 ra = 1004 The growth of the stack as the recursion unfolds

4 .Now test if n < 1 (i.e. n = 0). In that case return 0 to \$v0. slti \$t0, \$a0, 1 # if n ≥ 1 then goto L1 beq \$t0, \$zero, L1 addi \$v0, \$zero, 1 # return 1 to \$v0 addi \$sp, \$sp, 8 # pop 2 items from stack jr \$ra # return L1: addi \$a0, \$a0, -1 # decrement n jal fact # call fact with (n – 1) Now, we need to compute n * fact (n-1) lw \$a0, 0(\$sp) # restore argument n lw \$ra, 4(\$sp) # restore return address addi \$sp, \$sp, 8 # pop 2 items mult \$v0, \$a0, \$v0 # return n * fact(n-1) jr \$ra # return to caller

5 .Run time environment of a MIPS program Low address Stack pointer Temporary local variables Return address Growth of stack Saved argument registers beyond a0-a3 Frame pointer High address

6 .A translation hierarchy HLL program COMPILER Assembly language program ASSEMBLER Machine language module LINKER Library routine Executable machine language program LOADER Memory

7 .What are Assembler directives? Instructions that are not executed, but they tell the assembler about how to interpret something. Here are some examples: . text {Program instructions here} . data {Data begins here} . byte 84, 104, 101 . asciiz “The quick brown fox” . float f1,. . . , fn . word w1, . . . . wn . space n {reserve n bytes of space}

8 .How does an assembler work? In a two-pass assembler PASS 1: Symbol table generation PASS 2: Code generation Follow the example in the class.

0 点赞
0 收藏
0下载