I have these questions for a homework assignment and have to show work. This wor
ID: 3787021 • Letter: I
Question
I have these questions for a homework assignment and have to show work. This works with MIPS coding language and is the class Introduction to Computer Architecture.
1. Compile the following C code into MIPS code.
a). A[2]=g+A[1]-3, where g in $s1 and the base address of A in $s0.
b). for (i=0; i++; i<10) {A[i]=i}, where i in $s1 and the base address of A in $s0.
2. Given $t0=0x55555555 and $t1=0x12345678, what is the value of $t2 for the following sequence of instructions:
sll $t2, $t0, 4
or $t2, $t2, $t1
3. Given $t0=0xBEADFEED, what is the value of $t2 for the following sequence of instructions:
sll $t2, $t0, 4
andi $t2, $t2, -1
Explanation / Answer
1) a) w $t0, 4($s0) # A[1] is loaded to $t1
lw $t1, 0($s1) #g is brought into $t1
add $t0, $t0, $t1 #Addition of g+A[1] and put result in $t0
li $t2,-3 #load immediate value -3 into $t2
add $t3,$t0,t2 #Add -3 to value in $0 and put result in $t3
sw $t3,8($s0) # store $t3 in A[2], in MIPS word length is 32 bits so offset(for A[2] ie offset 2) should multiplied by 4(2*4=8 )
b)
li $s1, 0 # initialize the counter to 0
li $a1, 0 # initialize the $a1 to 10
loop:
beq $s1, $a1, exit # exit if we reach the end of the array
sw $s1, ($s0)
addi $s0, $s0, 4 # increment the pointer by one word
addi $s1, $s1, 1 # increment the loop counter
j loop # repeat the loop
exit:
2) pseudo code
t2 = (t0 << 44) || t1
t2= (0x55555555 << 4) || 0x12345678
Perform 4 left Shift on 0x55555555.
(0101 0101 0101 0101 0101 0101 0101 0101)<<4 gives 0101 0101 0101 0101 0101 0101 0101 0000
Then perform OR operation ( 0101 0101 0101 0101 0101 0101 0101 0000 ) OR 0x12345678
(0101 0101 0101 0101 0101 0101 0101 0000) OR 0001 0010 0011 0100 0101 0110 0111 1000
gives 0101 0111 0111 0101 0101 0111 0111 1000
ie 0x57755758
3)
pseudo code
sll $t2,$t0,4 #means perform 4 left shift on $t2
Perform 4 left Shift on 0x55555555.
(0101 0101 0101 0101 0101 0101 0101 0101)<<4 gives 0101 0101 0101 0101 0101 0101 0101 0000
andi $t2, $t2, -1 # It ANDs the contents of $t2 (all zeros) with the immediate operand (-1) and puts the result in register $t2
2's complement of -1 = 1111 1111 1111 1111 1111 1111 1111 1111
(0101 0101 0101 0101 0101 0101 0101 0000) AND (1111 1111 1111 1111 1111 1111 1111 1111)
= (0101 0101 0101 0101 0101 0101 0101 0000) = 0x55555550