80 likes | 226 Views
Lecture 5: Assembly Language. Computer Engineering 211 Spring 2002. iteration. Initialization. termination. For Loops. Version 1 move $r1, $r0 #i=0 move $r5, 400 Loop: add $r3, $r1, $r2 # $r3 Addr[A[i]] lw $r4, 0($r3) #$r4 A[i] addi $r4, $r4, 5
E N D
Lecture 5: Assembly Language Computer Engineering 211 Spring 2002
iteration Initialization termination For Loops Version 1 move $r1, $r0 #i=0 move $r5, 400 Loop: add $r3, $r1, $r2 # $r3 Addr[A[i]] lw $r4, 0($r3) #$r4 A[i] addi $r4, $r4, 5 # A[i] A[i]+5 sw $r4, 0($r3) # write A[i] addi $r1, $r1, 4 #i i+1 sub $r6, $r5, $r1 bne $r6, $r0, Loop for (i=0; i<100; i=i+1) A[i] = A[i] + 5; int A[100]; Word sized i $r1; Array A: AddrA $r2 Addr of A[i] $r3 A[i] $r4
For Loops Contd. Version 1 move $r1, $r0 #i=0 move $r5, 400 Loop: add $r3, $r1, $r2 # $r3 Addr[A[i]] lw $r4, 0($r3) #$r4 A[i] addi $r4, $r4, 5 # A[i] A[i]+5 sw $r4, 0($r3) # write A[i] addi $r1, $r1, 4 #i i+1 sub $r6, $r5, $r1 bne $r6, $r0, Loop #Instructions: Static: 9 Dynamic: 2 + 100*7 = 702 #registers: 6 • Inefficiencies: • Extra register for 100 and • condition. • (2) Two adds for address • maintenance.
0($r2) 0($r2) addi $r2, $r2, 4 addi $r1, $r1, 1 subi $r6, $r1, 100 For Loops Contd. Version 2 move $r1, $r0 #i=0 move $r5, 400 Loop: add $r3, $r1, $r2 # $r3 Addr[A[i]] lw $r4, 0($r3) #$r4 A[i] addi $r4, $r4, 5 # A[i] A[i]+5 sw $r4, 0($r3) # write A[i] addi $r1, $r1, 4 #i i+1 sub $r6, $r5, $r1 bne $r6, $r0, Loop #Instructions: 701 (static: 8) #registers: 4 ($r1, $r2, $r4, $r6)
For Loops Contd. Version 3 move $r1, $r0 #i=0 addi $r3, $r2, 400 Loop: lw $r4, 0($r2) #$r4 A[i] addi $r4, $r4, 5 # A[i] A[i]+5 sw $r4, 0($r2) # write A[i] addi $r2, $r2, 4 addi $r1, $r1, 1 #i i+1 subi $r6, $r1, 100 bne $r6, $r0, Loop Version 2: #Instructions: 701 (static: 8) #registers: 4 ($r1, $r2, $r4, $r6) Version 3: #Instructions: 601 (static: 7) #registers: 4 ($r2, $r3, $r4, $r6) sub $r6, $r3, $r2
For Loops Contd. Version 3 addi $r3, $r2, 400 Loop: lw $r4, 0($r2) #$r4 A[i] addi $r4, $r4, 5 # A[i] A[i]+5 sw $r4, 0($r2) # write A[i] addi $r2, $r2, 4 addi $r1, $r1, 1 #i i+1 sub $r6, $r3, $r2 bne $r6, $r0, Loop This version executes the loop body at least once! What if the termination condition is not true right in the beginning? for (i=0; i<N; i=i+1) A[i] = A[i] + 5;
sll $r5, $r1, 4 Loop: Version 4 addi $r3, $r2, 400 move $r1, N mult $r5, $r1, 4 add $r3, $r2, $r5 Loop: slt $r6, $r3, $r2 bne $r6, $r0, Exit lw $r4, 0($r2) #$r4 A[i] addi $r4, $r4, 5 # A[i] A[i]+5 sw $r4, 0($r2) # write A[i] addi $r2, $r2, 4 #i i+1 sub $r6, $r3, $r2 bne $r6, $r0, Loop Exit: For Loops Contd. #instructions: 605
i $r1 AddrA $r2 A[i] $r4 While Loops i=0; while (A[i]!=0) { A[i] = A[i] + 5; i=i+1; } Version 1 move $r1, $r0 #i=0 Loop: add $r3, $r1, $r2 # $r3 Addr[A[i]] lw $r4, 0($r3) #$r4 A[i] beq $r4, $r0, Exit addi $r4, $r4, 5 # A[i] A[i]+5 sw $r4, 0($r3) # write A[i] addi $r1, $r1, 4 #i i+1 j Loop Instructions:1+7*N +3 Registers:4