1 / 15

Lecture 10: Assembly Language

Lecture 10: Assembly Language. Computer Engineering 211 Spring 2002. Stack pointer vs Frame Pointer. Same variable referred with different offset with $sp inside the same procedure. SUM: sw $ra, -4($sp) sw $s0, -8($sp) sw $s1, -12($sp) addi $sp, $sp, -12

hayden
Download Presentation

Lecture 10: Assembly Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10: Assembly Language Computer Engineering 211 Spring 2002

  2. Stack pointer vs Frame Pointer Same variable referred with different offset with $sp inside the same procedure. SUM: sw $ra, -4($sp) sw $s0, -8($sp) sw $s1, -12($sp) addi $sp, $sp, -12 lw $s0, 12($sp) lw $s1, 16($sp) # load F add $v0, $a0, $a1 add $t0, $a2, $a3 add $t1, $s0, $s1 add $t0, $t0, $t1 add $v0, $v0, $t0 lw $s0, 4($sp) lw $s1, 0($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra Where do the local variables of a procedure reside? Stack. Frame pointer makes the local variable access easier.

  3. Local Variables int add-array (int *A, int lo, int Hi) { int i, temp, N; N=(Hi – lo +1) if (N < 10){ temp = 0; for (i=lo; i<lo+Hi; i++) temp = temp +A[i]; } else {temp = add-array (A, lo, (lo+[N/2])); temp = add-array(A, (lo+[N/2]+1), Hi) +temp;} return temp; }

  4. After Add-array(A, 0, 49) After Add-array(A, 50, 99) Data N 100? 50? temp i Text OS Local Variables contd. Add-array(A, 0, 99): Add-array(A, 0, 49) + Add-array(A, 50, 99)

  5. Another Example Temp: 1 1 1 -- -- 1 1 factorial (10) factorial (9) factorial (8) --- --- factorial (1) factorial (0) factorial (N) { int temp; if (N==0) return 1; else { temp = N; return (temp * factorial(N-1)); } }

  6. Factorial assembly factorial: sw $ra, -4($sp) # $s0 for temp addi $sp, $sp, 8 sw $s0, -8($sp) jr $ra addi $sp, $sp, -8 # var temp is located at # next address bne $a0, $zero, recurse move $v0, 1 j exit recurse: move $s0, $a0 sw $s0, -4($sp) #un-necessary addi $a0, $a0, -1 jal factorial mult $v0, $v0, $s0 exit: lw $s0, 0($sp) lw $ra, 4($sp) $ra $s0 temp

  7. Bottom of the stack frame: $fp $sp Frame Pointer Top of the stack frame: $sp $ra All the information relevant to a call to factorial. $s0 temp

  8. factorial(10) factorial(9) factorial(10) stack frame/AR $ra $fp $s0 $ra temp $s0 factorial(9) AR $sp temp Frame Pointer, Contd. How do we pop factorial(9) AR? $sp changes continuously. $fp moves in big steps.

  9. $fp sw $fp, -4($sp) $ra addi $sp, $sp, -4 move $fp, $sp $s0 temp $sp $ra old $fp 0x7fff0000 sw $ra, -4($sp) sw $s0, -8($sp) #create temp addi $sp, $sp, -12 $s0 temp Frame Pointer, Contd. Addr: 0x7fff0000

  10. $ra #restore old $fp lw $fp, 0($fp) addi $sp, $sp, 16 $s0 temp $ra old $fp 0x7fff0000 $s0 temp Frame Pointer, Contd. lw $ra, 8($sp) lw $s0, 4($sp) $fp $sp

  11. Factorial assembly with FP factorial: sw $fp, -4($sp) addi $fp, $sp, -4 lw $fp, 0($fp) sw $ra, -8($sp) addi $sp, $sp, 16 sw $s0, -12($sp) jr $ra addi $sp, $sp, -16 bne $a0, $zero, recurse move $v0, 1 j exit recurse: move $s0, $a0 addi $a0, $a0, -1 jal factorial mult $v0, $v0, $s0 exit: lw $s0, 4($sp) lw $ra, 8($sp)

  12. old $fp $fp $ra $s0 temp $sp FP advantage: same var. name Each variable same offset from $fp throughout program! factorial: sw $fp, -4($sp) addi $fp, $sp, -4 lw $fp, 0($fp) sw $ra, -4($fp) addi $sp, $sp, 16 sw $s0, -8($fp)jr $ra addi $sp, $sp, -16 bne $a0, $zero, recurse move $v0, 1 j exit recurse: move $s0, $a0 addi $a0, $a0, -1 jal factorial mult $v0, $v0, $s0 exit: lw $s0, -8($fp) lw $ra, -4($fp)

  13. Example Program SUM: sw $ra, -4($sp) # read E & F into $s0 & $s1 add $v0, $v0, $t0 sw $s0, -8($sp) #restore $s0 & $s1 sw $s1, -12($sp) lw $s0, 4($sp) addi $sp, $sp, -12 lw $s1, 0($sp) lw $s0, 12($sp) # load E # restore $ra lw $s1, 16($sp) # load F lw $ra, 8($sp) add $v0, $a0, $a1 addi $sp, $sp, 12 add $t0, $a2, $a3 jr $ra add $t1, $s0, $s1 add $t0, $t0, $t1

  14. $t0 $t1 F 10000 E $sp $ra 9996 $s0 9988 $s1 Example Program Contd. Calling Context: #save any temp registers sw $t0, -4($sp) move $a2, $s0 sw $t1, -8($sp) move $a3, $s1 #stack parameters jal SUM sw $s3, -12($sp) move $s4, $v0 sw $s4, -16($sp) addi $sp, $sp, -16 #reg parameters move $a0, $t0 move $a1, $t1 Return address: 2000

  15. SUM: sw $fp, -4($sp) addi $fp, $sp, -4 $fp 9996 sw $ra, -4($fp) $t0 sw $s0, -8($fp) $t1 sw $s1, -12($fp) F 10000 11000 addi $sp, $sp, -16 E $sp Old $fp=11000 $ra=2000 $s0 9984 $s1 AR(Caller) AR(SUM) Example Program Contd. --- --- lw $s1, -12($fp) lw $s0, -8($fp) lw $ra, -4($fp) lw $fp, -0($fp) addi $sp, $sp, 16 jr $ra

More Related