McGill University Department of Electrical and Computer Engineering ECSE 221 Introduction to Computer Engineering I Assignment 4 – Sequential Logic Due: Monday, March 28th 2012 at 5:00 pm Instructions: Please electronically submit assignment # 4 by 5:00 pm on the due date above via WebCT. Late assignments will be penalized by 5% for submission on Wednesday 16th March after 5pm, and by 10% for submission on Tuesday 29th March, 2011 after 5pm. Keep in mind that assignment submitted on Wednesday March 30th (5pm) or after will not be accepted (100% penalty). Please submit all your files along with a completed word document/pdf in a zipped folder. Marks will be deducted otherwise. Question 1 Transform the following code into MIPS assembly language. Assume that the variable a is stored in register $4, b in $5, c in $6. Show the values in the registers following each line of assembly code. (ii)
(i) a=4 b=4 if ( a > b) c=3; else c=6;
a=4 b=4 if ( a ≥ b) c=3; else c=6;
(iii) a=4 b=4 c = ( a == b)
(i) li $4, 4 #register $4 has the value 4 li $5, 4 #register $5 has the value 4 slt $1, $5, $4 #b is not less than a , so $1 has the value 0 beq $1, $0, ifstsmnt li $6, 3 #b not < a. c=6, register $6 has the value 6 j Exit ifstsmnt: li $6, 3 #c=3, register $6 has the value 3
Exit: (ii) li $4, 4 #register $4 has the value 4 li $5, 4 #register $5 has the value 4 slt $1, $4, $5 #checking if a< b. if so branch to else. a is not less than b , so $1 has the value 0 beq $1, $0, ifstsmnt li $6, 6 #b not < a. c=6, else statement, register $6 has the value 6 j Exit ifstsmnt : li $6, 3 #c=6, register $6 has the value 3
Exit: (iii) li $4, 4 #register $4 has the value 4 li $5, 4 #register $5 has the value 4
beq $4, $5, L1 # if a and b are equal, branch to L1 ori $4,$0, 0 # if they aren’t equal, a($4) OR with zero to equal 0 beq $0. $0, Exit # branch to exit L1: ori $4, $0, 1 # a($4) is set to 1 because anything OR with 1 = 1 Exit: Question 2 Part a Transform the following MIPS assembly language code into c code. Assume that register $4 points to v[0], register $5 contains the variable b, and register $17 contains the variable i. Addi $17 $0 1 //i=1 Loop: sll $20, $17, 2 // $20 = 4 (transformed to index) add $20, $20, $4 // $20 has memory location for v[1] (base index + shift) lw $21, 0($20) // $21 contains value of v[1] slti $22, $17, 10 //if i < 10 (which it always is), then $22 = 1 beq $22, $0, Exit // if i is not < 10 then exit loop beq $21, $5, Exit // if v[i] == b then exit loop j Loop // if i