1 / 16

Lecture 9: Assembly Language

Lecture 9: Assembly Language. Computer Engineering 211 Spring 2002. 0x7fff0000. E(LSB). 0x7ffefffc. E(MSB). 0x7ffefff8. Parameter vector. Passing parameters on stack. int A, B, C, D, E; char F; foo(A, B, C, D, E, F);. A ($s0), B ($s1), C ($s2), D ($s3) E ($s4), F ($s5).

norah
Download Presentation

Lecture 9: 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 9: Assembly Language Computer Engineering 211 Spring 2002

  2. 0x7fff0000 E(LSB) 0x7ffefffc E(MSB) 0x7ffefff8 Parameter vector Passing parameters on stack int A, B, C, D, E; char F; foo(A, B, C, D, E, F); A ($s0), B ($s1), C ($s2), D ($s3) E ($s4), F ($s5) Calling context: ---------- move $a0, $s0 move $a1, $s1 move $a2, $s2 move $a3, $s3 sw $s5, -4($sp) addi $sp, $sp, -4 sw $s4, -4($sp) addi $sp, $sp, -4 jal foo Stack before foo call $sp F

  3. Stack before foo call 0x7ffefff8 $sp F E $ra 0x7ffefff4 Passing parameters on stack, Contd. Character ‘0’, ASCII code: 0x30 foo (AA, BB, CC, DD, EE, FF) int AA, BB, CC, DD, EE; char FF; { if (FF != ‘0’) AA = AA + BB + CC + DD; else AA = EE; } foo: sw $ra, -4($sp) addi $sp, $sp, -4 lw $t0, 8($sp) #$t0 contains FF li $t1, 0x30 beq $t0, $t1, elsepart add $a0, $a0, $a1 add $a0, $a0, $a2 add $a0, $a0, $a3 j join elsepart: lw $a0, 4($sp) join: lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra

  4. Stack before foo call $sp F E 0x7ffefff8 Passing parameters on stack, Contd. Calling context: ---------- move $a0, $s0 move $a1, $s1 move $a2, $s2 move $a3, $s3 sw $s5, -4($sp) addi $sp, $sp, -4 sw $s4, -4($sp) addi $sp, $sp, -4 jal foo move $s0, $a0 addi $sp, $sp, 8

  5. A by Value Call by Value/reference for Register parameters Interpret at the calling context end! A by reference: ----- ----- addi $sp, $sp, -4 jal foo move $s0, $a0 addi $sp, $sp, 8 int A, B, C, D, E; char F; foo(A, B, C, D, E, F); foo (AA, BB, CC, DD, EE, FF) int AA, BB, CC, DD, EE; char FF; { if (FF != ‘0’) AA = AA + BB + CC + DD; else AA = EE; }

  6. Stack before foo call $sp F E $ra 0x7ffefff4 E 30 F 10 $gp Global Data Call by Value/reference for Stack parameters # EE & FF by value foo: sw $ra, -4($sp) addi $sp, $sp, -4 lw $t0, 8($sp) #$t0 contains FF li $t1, 0x30 beq $t0, $t1, elsepart add $a0, $a0, $a1 add $a0, $a0, $a2 add $a0, $a0, $a3 j join elsepart: lw $a0, 4($sp) join: lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra Interpret at the called procedure end! lw $s4, 30($gp); lw $s5, 10($gp)

  7. Stack before foo call $sp Addr(F) E $ra 0x7ffefff4 E 30 F 10 $gp Call by reference for Stack parameters # FF by reference foo: sw $ra, -4($sp) addi $sp, $sp, -4 lw $t0, 8($sp) lw $t0, 0($t0) #$t0 contains FF li $t1, 0x30 beq $t0, $t1, elsepart add $a0, $a0, $a1 add $a0, $a0, $a2 add $a0, $a0, $a3 j join elsepart: lw $a0, 4($sp) join: lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra

  8. Stack before foo call $sp Addr(F) E $ra 0x7ffefff4 E 30 F 10 $gp Call by reference for Stack parameters Calling context: ---------- move $a0, $s0 move $a1, $s1 move $a2, $s2 move $a3, $s3 la $t0, 10($gp) sw $t0, -4($sp) addi $sp, $sp, -4 sw $s4, -4($sp) addi $sp, $sp, -4 jal foo move $s0, $a0 addi $sp, $sp, 8

  9. Activation Record/Stack Frame • Stack frame/activation record (AR) created for each procedure call/activation. • What goes into it? • Caller: parameter vector • Callee: Saved return address. • Callee: saved registers (spilled registers). • Callee: local variables

  10. Register Saving $t0-$t9:Temporaries – caller has no guarantee that the called procedure will not modify them. $s0-$s7:Saved Temporaries – caller is guaranteed that the called procedure will retain their value on return. • Caller saves the $t0-$t9 needed after a procedure call on the stack before initiating the call. • Callee saves the $s0-$s7 it wants to use, and restore them before returning from the call.

  11. Register saving A: $t0, B: $t1, C:$s0, D:$s1, E: $s2, F:$s3, Z: $s4 int A, B, C, D, E, F, Z; --- Z=SUM(A, B, C, D, E, F) --- #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 Caller saved

  12. Register saving 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 Callee saved

  13. $t0 $t1 F 10000 E $sp $ra 9996 $s0 9988 $s1 Stack Growth in SUM 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

  14. Result on the Stack int A, B, C, D, E, F, Z; --- X,Y,Z=MAX3(A, B, C, D, E, F) A: $t0, B: $t1, C:$s0, D:$s1, E: $s2, F:$s3, X:$s4, Y:$s5, Z: $s6 --- #save any temp registers sw $t0, -4($sp) move $a2, $s0 sw $t1, -8($sp) move $a3, $s1 #stack parameters jal MAX3 sw $s3, -12($sp) sw $s4, -16($sp) addi $sp, $sp, -16 #reg parameters move $a0, $t0 move $a1, $t1

  15. Result on the Stack, Contd. MAX3: sw $ra, -4($sp) addi $sp, $sp, -4 lw $ra, 0($sp) lw $s0, 4($sp) # load E addi $sp, $sp, 4 lw $s1, 8($sp) # load F jr $ra # a bunch of if-then-else # place MAX1: $t0, MAX2: $t1, MAX3: $t3 # return the first two words in $v0 & $v1 move $v0, $t0 move $v1, $t1 # the extra word returned on stack # overwrites the parameter vector sw $t3, 4($sp)

  16. Result on the Stack, Contd. A: $t0, B: $t1, C:$s0, D:$s1, E: $s2, F:$s3, X:$s4, Y:$s5, Z: $s6 --- #save any temp registers sw $t0, -4($sp) jal SUM sw $t1, -8($sp) #retrieve result #stack parameters move $s4, $v0 sw $s3, -12($sp) move $s5, $v1 sw $s4, -16($sp) lw $s6, 0($sp) addi $sp, $sp, -16 #adjust parameter #reg parameters #vector move $a0, $t0 lw $t1, 8($sp) move $a1, $t1 lw $t0, 12($sp) move $a2, $s0 addi $sp, $sp, 16 move $a3, $s1

More Related