Unformatted text preview:

CS281 - L2!Ilanit Izrailov!1. Sumr.asm - sums an entire array’s values up to 55, except here, it is done using recursion, each time ‘cutting’ the array from the left side to the right most cell, using a pointer, until we reach a subarray of size 1, return the value in the ‘last’ cell and sums up recursively all of the return values.!Continuation:!Address MIPS InstructionChange in PCChange in Registers[00400000] 8fa40000""lw $4, 0($29)""""""""""""; 183: lw $a0 0($sp) # argc #[00400004] 27a50004""addiu $5, $29, 4"""""""""; 184: addiu $a1 $sp 4 # argv #[00400008] 24a60004""addiu $6, $5, 4""""""""""; 185: addiu $a2 $a1 4 # envp #[0040000c] 00041080""sll $2, $4, 2""""""""""""; 186: sll $v0 $a0 2 #[00400010] 00c23021""addu $6, $6, $2""""""""""; 187: addu $a2 $a2 $v0 #[00400014] 0c100009""jal 0x00400024 [main]""""; 188: jal main #[00400018] 00000000""nop""""""""""""""""""""""; 189: nop #[0040001c] 3402000a""ori $2, $0, 10"""""""""""; 191: li $v0 10 #[00400020] 0000000c""syscall""""""""""""""""""; 192: syscall # syscall 10 (exit) #[00400024] 23bdfff8""addi $29, $29, -8""""""""; 14: sub $sp,$sp,8 # push stack #[00400028] afbf0000""sw $31, 0($29)"""""""""""; 15: sw $ra,0($sp) # save return address #[0040002c] 3c011001""lui $1, 4097 [A]"""""""""; 18: la $a0,A # address of A #[00400030] 34240000""ori $4, $1, 0 [A] """""""#[00400034] 3c011001""lui $1, 4097 [n]"""""""""; 19: la $a1,n # address of n #[00400038] 34250028""ori $5, $1, 40 [n] """"""#[0040003c] 8ca50000""lw $5, 0($5)"""""""""""""; 20: lw $a1,0($a1) # load value of n #[00400040] 0c10001c""jal 0x00400070 [sumr]""""; 21: jal sumr # call sumr function #[00400044] afa20004""sw $2, 4($29)""""""""""""; 22: sw $v0,4($sp) # save return result #[00400048] 34020004""ori $2, $0, 4""""""""""""; 24: li $v0,4 #[0040004c] 3c011001""lui $1, 4097 [str]"""""""; 25: la $a0,str #[00400050] 3424002c""ori $4, $1, 44 [str] """"#[00400054] 0000000c""syscall""""""""""""""""""; 26: syscall #[00400058] 34020001""ori $2, $0, 1""""""""""""; 28: li $v0,1 #[0040005c] 8fa40004""lw $4, 4($29)""""""""""""; 29: lw $a0,4($sp) #[00400060] 0000000c""syscall""""""""""""""""""; 30: syscall #[00400064] 8fbf0000""lw $31, 0($29)"""""""""""; 32: lw $ra,0($sp) # restore return addr#[00400068] 23bd0008""addi $29, $29, 8"""""""""; 33: add $sp,$sp,8 # pop stack #[0040006c] 03e00008""jr $31"""""""""""""""""""; 34: jr $ra #[00400070] 34080001""ori $8, $0, 1""""""""""""; 53: li $t0, 1 #[00400074] 14a80004""bne $5, $8, 16 [rec-0x00400074]; 54: bne $a1, $t0, rec # if (n > 1) then recurse #[00400078] 8c880000""lw $8, 0($4)"""""""""""""; 55: lw $t0,0($a0) # Base case: #[0040007c] 00081021""addu $2, $0, $8""""""""""; 56: move $v0,$t0 # return s=A[0] #[00400080] 03e00008""jr $31"""""""""""""""""""; 57: jr $ra #[00400084] 23bdfff4""addi $29, $29, -12"""""""; 60: sub $sp,$sp,12 # push stack #[00400088] afbf0000""sw $31, 0($29)"""""""""""; 61: sw $ra,0($sp) # save $ra #[0040008c] afa40004""sw $4, 4($29)""""""""""""; 62: sw $a0,4($sp) # save argument registers #[00400090] afa50008""sw $5, 8($29)""""""""""""; 63: sw $a1,8($sp) #[00400094] 20840004""addi $4, $4, 4"""""""""""; 65: addi $a0,$a0,4 # point to next element in array #[00400098] 20a5ffff""addi $5, $5, -1""""""""""; 66: addi $a1,$a1,-1 # n = n - 1 #[0040009c] 0c10001c""jal 0x00400070 [sumr]""""; 67: jal sumr # recursively call sumr #[004000a0] 8fa40004""lw $4, 4($29)""""""""""""; 69: lw $a0, 4($sp) # restore $a0 to original address of A #[004000a4] 8c880000""lw $8, 0($4)"""""""""""""; 70: lw $t0, 0($a0) # load A[0] #[004000a8] 00481020""add $2, $2, $8"""""""""""; 71: add $v0,$v0,$t0 # s = s + sumr(A+4,n-1) #[004000ac] 8fbf0000""lw $31, 0($29)"""""""""""; 73: lw $ra, 0($sp) # restore $ra #[004000b0] 8fa50008""lw $5, 8($29)""""""""""""; 74: lw $a1, 8($sp) #[004000b4] 23bd000c""addi $29, $29, 12""""""""; 75: add $sp, $sp, 12 # pop stack #[004000b8] 03e00008""jr $31"""""""""""""""""""; 76: jr $ra # return to calling function 400004!400008!40000c!Jump to 400010!400014!Jump to 400024!400028!40002c!400030!400034!400038!40003c!400040!Jump to 400070 400074!Jump to 400084!400088!40008c!400090!400094!400098!40009c!Jump to 400070 400074!Jump to 400084!400088!40008c!400090!400094!400098!40009c!Jump to 400070 400074!Jump to 400084!400088!40008c!400090!400094!400098!40009c!Jump to 400070 400074!Jump to 400084!400088R6[a2] = 7ffffe18!R2[v0] = 4!R6[a2] = 7ffffe1c!R31[ra] = 400018!R29[sp] = 7ffffe08!R1""[at]"="10010000!R4[a0] = 10010000!R5[a1] =10010028!R5[a1] = a!R31[ra] = 400044!R8[t0] = 1!R29[sp] = 7ffffdfc!R4[a0] = 10010004!R5[a1] = 9!R31[ra] = 4000a0!R29[sp] = 7ffffdf0!R4[a0] = 10010008!R5[a1] = 8!R29[sp] = 7ffffde4!R4[a0] = 1001000c!R5[a1] = 7!R29[sp] = 7ffffdd8!40008c !400090!400094!400098 - R4 [a0] = 10010010!40009c - R5 [a1] = 6!jump to 400070 400074!jump to 400084!400088 - R29 [sp] = 7ffffdcc!40008c!400090!400094!400098 - R4 [a0] = 10010014!40009c - R5 [a1] = 5!Jump to 400070 400074!j 400084!400088 - R29 [sp] = 7ffffdc0!40008c!400090!400094!400098 R4 [a0] = 10010018!40009c - R5 [a1] = 4!Jump to 400070 400074!jump to 400084!400088 - R29 [sp] = 7ffffdb4!40008c!400090!400094!400098 - R4 [a0] = 1001001c!40009c - R5 [a1] = 3!Jump to 400070 400074!jump to 400084!400088 - R29 [sp] = 7ffffda8!40008c!400090!400094!400098 R4 [a0] = 10010020!40009c - R5 [a1] = 2!jump to 400070 400074!jump to 400084!400088 - R29 [sp] = 7ffffdb4!40008c!400090!400094!400098 - R4 [a0] = 10010024!40009c - R5 [a1] = 1!jump to 400070 400074!EXIT LOOP/RECURSION 400078!40007c - R8 [t0] = a!400080 - R2 [v0] = a!4000a0!4000a4 - R4 [a0] = 10010020!4000a8 - R8 [t0] = 9!4000ac - R2 [v0] = 13!—>!RECURSE/CALL THE FUNCTION SUMR 10 TIMES BY GOING TO MEM ADDRESS 400070(IF N==1) THEN 9 REPETITIVE VISITS OF ADDRESS 4000bO TO STORE THE VALUE OF THE RECURSIVE CALL AND SEND IT BACK UP TO THE ORIGINAL BRANCH OF RECURSIVE TREE 4000b0 4000b4 - R5 [a1] = 2!4000b8 - R29 [sp] = 7ffffda8!4000a0!4000a4 - R4 [a0] = 1001001c!4000a8 - R8 [t0] = 8!4000ac - R2 [v0] = 1b!4000b0 4000b4 - R5 [a1] = 3!4000b8 - R29 [sp] = 7ffffdb4!4000a0!4000a4 - R4 [a0] = 10010018!4000a8 R8 [t0] = 7!4000ac R2 [v0] = 22!4000b0 4000b4 - R5 [a1] = 4!4000b8 - R29 [sp] = 7ffffdc0!4000a0!4000a4 - R4 [a0] = 10010014!4000a8 - R8 [t0] = 6!4000ac - R2 [v0] = 28!4000b0 4000b4 - R5 [a1] = 5!4000b8 - R29 [sp] = 7ffffdcc!4000a0!4000a4 - R4 [a0] = 10010010!4000a8 R8 [t0] = 5!4000ac R2 [v0] = 2d!4000b0 4000b4 - R5 [a1] = 6!4000b8 - R29


View Full Document

DREXEL CS 281 - LAB 2

Documents in this Course
Load more
Download LAB 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LAB 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LAB 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?