Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Code Example Selection Sort. Swap Function in C Swap(int & num1, int & num2) // post: values of num1 and num2 have // been swapped { int temp;

Similar presentations


Presentation on theme: "Assembly Code Example Selection Sort. Swap Function in C Swap(int & num1, int & num2) // post: values of num1 and num2 have // been swapped { int temp;"— Presentation transcript:

1 Assembly Code Example Selection Sort

2 Swap Function in C Swap(int & num1, int & num2) // post: values of num1 and num2 have // been swapped { int temp; temp = num1; num1 = num2; num2 = temp; }

3 Swap Function in Assembly Code Swap:lw $t0, 0($a0)# t0 gets num1 lw $t1, 0($a1)# t1 gets num2 sw $t0, 0($a1)# num2 gets t0 sw $t1, 0($a0)# num1 gets t1 jr $ra

4 Selection Sort function in C void SelSort(int v[], int length) { int indexMin; for(int s = 0; s < length-1; s++){ indexMin = s; for(int k = s+1; k < length; k++){ if(v[k] < v[indexMin]) indexMin = k; } swap(v[s], v[indexMin]); } }

5 Selection Sort in Assembly Code Do we make a subsidiary function call? –Yes, must save state, use s registers What must go into s registers? –Variables whose values must be retained across function calls v base, length, length-1, s –Other variables can use t registers k, minIndex, temps for address/branch computations Construct code by units (if, for, for)

6 Selection Sort in Assembly Code # register assignments # v base in $s0 (move from $a0) # length in $s1 (move from $a1) # length-1 in $s2 (compute from n) # s in $s3 (initialize to 0) # minIndex in $t0 # k in $t1

7 # if(v[k] < v[minIndex]) { minIndex = k } # sll $t3, $t1, 2 # t3 = 4 * k add $t3, $s0, $t3 # t3 = address of v[k] sll $t4, $t0, 2 # t4 = 4 * minIndex add $t4, $s0, $t4 # t4 = addr of v[minIndex] lw $t5, 0($t3) # t5 = v[k] lw $t6, 0($t4) # t6 = v[minIndex] slt $t2, $t5, $t6 # v[k] < v[minIndex]? beq $t2, $zero, endif # if not skip if part add $t0, $t1, $zero # minIndex = k endif: Selection Sort in Assembly Code

8 # for(k = s+1; k < length; k++) # { if …} # addi $t1, $s3, 1 # k = s + 1 fork: slt $t2, $t1, $s1 # k < length ? beq $t2, $zero, endfork # if not, exit # { if…} addi $t1, $t1, 1 # k++ j fork endfork: Selection Sort in Assembly Code

9 # for(s = 0; s < length-1; s++) # minIndex = s; for k …; swap(v[s], v[minIndex]); # add $s3, $zero, $zero # s = 0 fors: slt $t2, $s3, $s2 # s < length-1 ? beq $t2, $zero, endfors # if not, exit loop add $t0, $s3, $zero # minIndex = s # for k... sll $t3, $s3, 2 # compute array addresses add $a0, $s0, $t3 # and store as parameters sll $t3, $t0, 2 add $a1, $s0, $t3 jal Swap # call swap function addi $s3, $s3, 1 # s++ j fors endfors:

10 # save state # restore state addi $sp, $sp, -20 sw $ra, 16($sp) lw $ra, 16($sp) sw $s3, 12($sp) lw $s3, 12($sp) sw $s2, 8($sp) lw $s2, 8($sp) sw $s1, 4($sp) lw $s1, 4($sp) sw $s0, 0($sp) lw $s0, 0($sp) addi $sp, $sp, 20 # initialize add $s0, $a0, $zero # return add $s1, $a1, $zero jr $ra addi $s2, $s1, -1 Selection Sort in Assembly Code

11 Assemble / Link / Load / Run Assemble –translates assembly code to object code (machine lang) pseudo instructions are replaced by actual machine instructions e.g. move $t0, $s1 becomes add $t0, $s1, $zero –addresses are set relative to start of code segment relocation done by linker –internal addresses which need to be resolved are kept in a symbol table –external addresses which are needed are listed e.g. function calls not part of this segment

12 Link puts together different object code segments –resolves addresses with function calls across segments –resolves final addresses of labels –places data in data segment and resolves addresses Load place code into main memory Run start at label main Assemble / Link / Load / Run


Download ppt "Assembly Code Example Selection Sort. Swap Function in C Swap(int & num1, int & num2) // post: values of num1 and num2 have // been swapped { int temp;"

Similar presentations


Ads by Google