assembler assembler
play

Assembler Assembler Ein greres Beispiel Prof. Dr. Peter Trommler - PowerPoint PPT Presentation

Assembler Assembler Ein greres Beispiel Prof. Dr. Peter Trommler Prozedur zum Tauschen Prozedur zum Tauschen void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; return; } 2 Prof. Dr. P. Trommler Bubblesort Bubblesort void


  1. Assembler Assembler Ein größeres Beispiel Prof. Dr. Peter Trommler

  2. Prozedur zum Tauschen Prozedur zum Tauschen void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; return; } 2 Prof. Dr. P. Trommler

  3. Bubblesort Bubblesort void sort(int *a, int size) { int swapped, i; do { swapped = 0; for(i=0; i<size; i++) if (a[i] > a[i+1]) { swap(&a[i], &a[i+1]); swapped = 1; } } while (swapped); return; } 3 Prof. Dr. P. Trommler

  4. Do-While-Schleife Do-While-Schleife or $s2,$a0,$zero # save a or $s3,$a1,$zero # save size doloop: or $s0,$zero,$zero # swapped = false ... bne $s0,$zero,doloop# end do while 4 Prof. Dr. P. Trommler

  5. For-Schleife For-Schleife or $s2,$a0,$zero # save a or $s3,$a1,$zero # save size doloop: or $s0,$zero,$zero # swapped = false or $s1,$zero,$zero # i=0 (init for) for: slt $t2,$s1,$s3 # i<size? beq $t2,$zero,endfor # for ends ... addiu $s1,$s1,1 # i++ j for # end of for loop endfor: bne $s0,$zero,doloop # end do while 5 Prof. Dr. P. Trommler

  6. If-Abfrage If-Abfrage or $s2,$a0,$zero # save a or $s3,$a1,$zero # save size doloop: or $s0,$zero,$zero # swapped = false or $s1,$zero,$zero # i=0 (init for) for: slt $t2,$s1,$s3 # i<size? beq $t2,$zero,endfor # for ends addu $a0,$zero,$s1 sll $a0,$a0,2 # offset (i*4) addu $a0,$a0,$s2 # address of a[i] addi $a1,$a0,4 # address of a[i+1] lw $t0,0($a0) # a[i] lw $t1,0($a1) # a[i+1] slt $t2,$t1,$t0 # a[i+1]<a[i]? bne $t2,$zero,endif # on false skip ... endif: addiu $s1,$s1,1 # i++ j for # end of for loop endfor: bne $s0,$zero,doloop # end do while 6 Prof. Dr. P. Trommler

  7. Funktionsaufruf Funktionsaufruf or $s2,$a0,$zero # save a or $s3,$a1,$zero # save size doloop: or $s0,$zero,$zero # swapped = false or $s1,$zero,$zero # i=0 (init for) for: slt $t2,$s1,$s3 # i<size? beq $t2,$zero,endfor # for ends addu $a0,$zero,$s1 sll $a0,$a0,2 # offset (i*4) addu $a0,$a0,$s2 # address of a[i] addi $a1,$a0,4 # address of a[i+1] lw $t0,0($a0) # a[i] lw $t1,0($a1) # a[i+1] slt $t2,$t1,$t0 # a[i+1]<a[i]? bne $t2,$zero,endif # on false skip jal swap # swap elements ori $s0,$zero,1 # swapped = true endif: addiu $s1,$s1,1 # i++ j for # end of for loop bne $s0,$zero,doloop # end do while 7 Prof. Dr. P. Trommler

  8. Stackbehandlung Stackbehandlung addi $sp,$sp,-20 # 5 registers (s0-s3 and ra) sw $s0,0($sp) #save registers on stack sw $s1,4($sp) sw $s2,8($sp) sw $s3,12($sp) sw $ra,16($sp) ... # bubblesort code lw $s0,0($sp) # restore registers lw $s1,4($sp) lw $s2,8($sp) lw $s3,12($sp) lw $ra,16($sp) addi $sp,$sp,20 jr $ra # return 8 Prof. Dr. P. Trommler

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend