0% found this document useful (0 votes)
11 views

CENG400 Assignment 2-Sol-V1

Uploaded by

11832842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

CENG400 Assignment 2-Sol-V1

Uploaded by

11832842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter II

Problem 1:
Add comments and compile the following program (ignore the last line)
Address Label Instruction Comments HEX
0x0040000C add $t0, $zero, Set t0 to 0 0x00004020
$zero
loop: beq $a0, $zero, end If a0 equals 0, jump to 0x10800003
end
addi $t0, $t0, 1 Increment t0 0x21080001
addi $a0, $a0, -1 Decrement a0 0x2084ffff
j loop Jump to loop 0x08100004
end: nop Ends program 0x00000000
Problem 2:
Write a MIPS program that counts, in $v0, the number of occurrences of 01 in the value stored
inregister $a0.
Example:
If the value stored in $a0 is 0x057A0152 then $v0 should be set to 8 because
0x057A0152 = 0000 0101 0111 1010 0000 0001 0101 0010
Solution:
# counter $v0

# $t1 mask of 1 moving left

# $t2 result of masking $a0 and $t1

add $v0,$zero,$zero # Set the counter to 0

addi $t1,$zero,1 # Set a mask to 1

loop: and $t2,$a0,$t1 # mask with t1

bne $t2, $zero, itsaone # one is detected

# we reach this line if we have a zero

sll $t1,$t1,1 # shift the mask

# if $t1 (the mask) is negative, the 1 is at the end so stop

continue: slt $t2,$t1,$zero # t2=1 if t1 < 0

bne $t2,$zero,end # if t2!=0 (t1<0) stop

j loop

itsaone: #check if followed by 0

sll $t1,$t1,1 # shift the mask

and $t2,$a0,$t1 # mask with t1

bne $t2,$zero,continue # it is not followed by 0

addi $v0,$v0,1 # followed by 0 add 1

j loop
end: j end
Problem 3:
Consider the following MIPS program

a. What does the above program do?


The above program loads what is found in memory location 0x1040 and stores it in $s1 It
mirrors (reverse the bits) the bit value in register $s1 and store it in $s0 which is thentransferred
to memory location 0x1040.
b. Assemble (convert to hexadecimal) the instructions at lines 1, 6, 7, 15, 16
1 0000 0000 0000 0000 1000 0000 0010 0000 hex: 0x00008020
6 1000 1110 0111 0001 0000 0000 0100 0000 hex: 0x8e710040
7 0000 0000 0001 0000 1000 0000 0100 0000 hex: 0x00108040
15 0001 0101 1000 0000 1111 1111 1111 0111 hex: 0x1580fff7
16 1010 1110 0111 0000 0000 0000 0100 0000 hex: 0xae700040
Problem 4:
Write a MIPS program that stores two arrays A and B in memory based on the rules below:
- Array A starts at memory location 0x0100
- Array B starts at memory location 0x0200
- The contents of each array element is its index
o i.e., A[i] = i& B[j] = j
- Array A advances forward in memory
- Array B advances backwards in memory
- Keep both arrays moving forward until all locations between the arrays are filled
o Without overwriting each other
Hint: indices in MIPS can be negatives
The figure below demonstrates the description in the rules:

Solution:
addi $s0, $zero, 0x10010000 # store A address into s0
addi $s1, $zero, 0x10010100 # store B address into s1
addi $t0, $zero, 0 # i=0
addi $t1, $zero, 0 # j=0
add $t2, $zero, $s0 # t2=A used for first iteration
add $t3, $zero, $s1 # t3=B
loop: sw $t0, 0($t2) # A[i] = i
sw $t1, 0($t3) # B[j] = j
addi $t0, $t0, 1 # i=i+1
addi $t1, $t1, -1 # j=j-1, no subi
# shift should be done next so that we don't overwrite in the last iteration
sll $t2, $t0, 2 # t2 = i*4
sll $t3, $t1, 2 # t3 = j*4
add $t2, $s0, $t2 # t2 = A+i*4
add $t3, $s1, $t3 # t3 = B+j*4
bne $t2, $t3, loop # stopping criteria
nop

You might also like