You are on page 1of 17

Instruction Set Archeatecture

Question 1: Given the following program.


.text
. globl main
main:
li $a0, 5
jal function
move $a0, $v0
li $v0, 1
syscall
li $v0, 10
syscall
function:
move $t0, $a0
li $t1, 0
loop: beq $t0, 0, end
add $t1, $t1, $t0
sub, $t0, $t0, 1
b loop
end: move $v0, $t1
jr $ra
a) What is the value printed by this program (first system call in the program).
b) If register $a0, used for argument passing, stores a value in one-complement, what is
the range of numbers that can be stored in this register?

Question 2: What is the sequence of MIPS instructions that allow implementing the following
sentence in C language? (a and b are int variables)
a = b + c + 100;

- Answer:
+/ C language code:
#include <stdio.h>
int main() // Main function
{
int a, b, c; //declaring integer variables
scanf("%d", &b); // Taking input from user
scanf("%d", &c); // Taking input from user
a=b+c+100; // adding b,c and 100 and store in a variable
printf("a=b+c+100 is a= %d + %d +100 is %d",b,c,a); //Print out final result
return 0; // return the value
}

Question 3: Consider the following fragment of C code:


for (i=0; i<=10; i=i+1)
a [i] = b[i] + c;
Assume that a and b are arrays of words and that the base address of a is in $t0 and the
base address of b is in $t1. Register $t2 is associated with variable i and register $s0 with
the value of c. You may also assume that any address constants you need are available to be
loaded from memory.
a. Write the code for MIPS.
b. How many instructions are executed during the running of this code if there are no array
out-of-bounds exceptions thrown?
c. How many memory data references will be made during execution?

- Answer:
- MIPS code for the C fragment:

addi $t6, $zero, 101 # the loop termination value

add $t0, $zero, $zero # i = 0

addi $t2, $a0, 0 # ptr to current A[i]


addi $t3, $a1, 0 # ptr to current B[i]

loop: lw $t4, 0($t3) # load B[i]

add $t4, $t4, $s0 # B[i] + c

sw $t4, 0($t2) # store in A[i]

addi $t0, $t0, 1 # i++

addi $t2, $t2, 4 # ptr to next A[i]

addi $t3, $t3, 4 # ptr to next B[i]

bne $t0, $t6, loop # if i < 101, goto loop

=> The loop is executed 101 times and the loop contains 7 statements. There are 4 statements
outside the loop. Therefore, the total number of instructions executed is 4 + (7*101) = 711.

=? The total number of memory data reference: 101 * 2 = 202.


Question 4: Write a program, using the MIPS 32 assembly language,
a. To calculate the sum of the first 100 numbers. The result must be stored in register $v0.
b. Print the result.

- Answer:

.data
prompt: .asciiz "\nSum of first 100 number is : "
.text
li $v0,4
la $a0,prompt #it will print prompt
syscall
li $t0,0
li $s0,0 #store sum
loop:
bge $t0,100,exit

add $s0,$s0,$t0 #add number


add $t0,$t0,1 #i++
j loop
exit:

li $v0,1
move $a0,$s0
syscall

Question 5: Determine if the number stored in $t2 is even. If $t2 is even the program stores 1 in
$t1, else stores 0 in $t1
Print the result.

- Answer:
+/ To determine if the number stored in $t2 is even and store the result in $t1, you can use the following
MIPS assembly code:

# Check if $t2 is even


andi $t3, $t2, 1 # Perform bitwise AND with 1

# Store result in $t1


beqz $t3, even # If the result is 0, jump to the "even" label
li $t1, 0 # $t2 is odd, so store 0 in $t1
j end # Jump to the "end" label

even:
li $t1, 1 # $t2 is even, so store 1 in $t1

end:
# Print the result
move $a0, $t1 # Move the result to $a0 for printing
li $v0, 1 # Set the syscall code for printing an integer
syscall # Perform the syscall

+/ To determine if the number stored in register $t2 is even or odd in MIPS assembly, you can
use the following code:

andi $t1, $t2, 1 #Perform a bitwise AND operation between $t2 and
beqz $t1, even #Branch to “even” if the results is zero (number is enven)
li $t1, 0 #Load 0 into $t1 (number is odd)
j print result #Jump to “print_result”

even:
li $t1, 1 #Load 1 into $t1 (number is even)
move $a0, $t1 #Move the result in $t1 to the argument register $a0
syscall #Perform the system call to print the result

Question 6: Write a program in MIPS assembly instructions that reads two numbers. The
program must print what number is the largest.

- Answer:
+/ MIPS assembly code tha reads two numbers and prints the largest number:

# Program to find the largest number among two input numbers

.data
prompt1: .asciiz "Enter the first number: "
prompt2: .asciiz "Enter the second number: "
output: .asciiz "The largest number is: "

.text
.globl main

main:
# Display prompt to enter the first number
li $v0, 4
la $a0, prompt1
syscall

# Read the first number


li $v0, 5
syscall
move $t0, $v0 # Store the first number in $t0

# Display prompt to enter the second number


li $v0, 4
la $a0, prompt2
syscall

# Read the second number


li $v0, 5
syscall
move $t1, $v0 # Store the second number in $t1
# Compare the two numbers
bgt $t0, $t1, first_greater # If first number > second number, jump to first_greater

# Second number is greater or equal


move $t2, $t1 # Store the second number in $t2
j print_result

# First number is greater


first_greater:
move $t2, $t0 # Store the first number in $t2

print_result:
# Print the result
li $v0, 4
la $a0, output
syscall

li $v0, 1
move $a0, $t2
syscall

# Exit the program


li $v0, 10
syscall

Question 7: Write a program in MIPS assembly instructions to read a number. The program
must indicate if the number is odd or even.

- Answer:
+/ MIPS assembly code that reads a number and indicates whether it is odd or even:

# Program to determine whether a number is odd or even

.data
prompt: .asciiz "Enter a number: "
output_odd: .asciiz "The number is odd\n"
output_even: .asciiz "The number is even\n"

.text
.globl main

main:
# Display prompt to enter a number
li $v0, 4
la $a0, prompt
syscall

# Read the number


li $v0, 5
syscall
move $t0, $v0 # Store the number in $t0

# Determine if the number is odd or even


andi $t1, $t0, 1 # Perform bitwise AND with 1
beqz $t1, even # If the result is 0, jump to the "even" label

# The number is odd


li $v0, 4
la $a0, output_odd
syscall
j end

# The number is even


even:
li $v0, 4
la $a0, output_even
syscall

end:
# Exit the program
li $v0, 10
syscall

Question 8: Write a program that reads two integer numbers A and B. The program must
indicate if one of these numbers is multiple of the other one.

- Answer:
+/ MIPS assembly code that reads two interger numbers, A and B, and indicates if one of these
numbers is a multiple of the other:

# Program to check if one number is a multiple of the other

.data
prompt1: .asciiz "Enter number A: "
prompt2: .asciiz "Enter number B: "
output_multiple: .asciiz "One number is a multiple of the other\n"
output_not_multiple: .asciiz "The numbers are not multiples of each other\n"

.text
.globl main

main:
# Display prompt to enter number A
li $v0, 4
la $a0, prompt1
syscall

# Read number A
li $v0, 5
syscall
move $t0, $v0 # Store number A in $t0

# Display prompt to enter number B


li $v0, 4
la $a0, prompt2
syscall

# Read number B
li $v0, 5
syscall
move $t1, $v0 # Store number B in $t1

# Check if one number is a multiple of the other


div $t0, $t1 # Divide A by B
mfhi $t2 # Remainder stored in $t2

# If remainder is 0, one number is a multiple of the other


beqz $t2, multiples

# Numbers are not multiples of each other


li $v0, 4
la $a0, output_not_multiple
syscall
j end

multiples:
li $v0, 4
la $a0, output_multiple
syscall
end:
# Exit the program
li $v0, 10
syscall
Question 9: Write a program to print on the screen number from 1-9.

- Answer:
+/ MIPS assembly code that prints the number from 1 to 9 on the screen:

.data
newline: .asciiz "\n"

.text
.globl main

main:
li $t0, 1 # Initialize $t0 with the starting number

loop:
# Print the current number
move $a0, $t0 # Move the number to be printed to $a0
li $v0, 1 # Set the syscall code for printing an integer
syscall # Perform the syscall

# Print a newline character


li $v0, 4 # Set the syscall code for printing a string
la $a0, newline # Load the address of the newline string
syscall # Perform the syscall

addi $t0, $t0, 1 # Increment the number by 1

# Check if the number is greater than 9


bgt $t0, 9, end # If the number is greater than 9, exit the loop

j loop # Jump back to the "loop" label

end:
# Exit the program
li $v0, 10 # Set the syscall code for program exit
syscall # Perform the syscall

Question 10: Write a program, using the MIPS32 assembly language that reads a number N and
prints the following:
1
12
123
1234
…..
1 2 3 4 5 …. N

- Answer:
+/ MIPS assembly code that reads a number N and prints the pattern you described:

.data
prompt: .asciiz "Enter a number N: "
output: .asciiz "\n"

.text
.globl main

main:
# Display prompt to enter N
li $v0, 4
la $a0, prompt
syscall

# Read N
li $v0, 5
syscall
move $t0, $v0 # Store N in $t0

li $t1, 1 # Initialize the counter to 1

loop_outer:
move $t2, $t1 # Store the current counter value in $t2

loop_inner:
move $a0, $t2 # Move the value to be printed to $a0
li $v0, 1 # Set the syscall code for printing an integer
syscall # Perform the syscall

addi $t2, $t2, 1 # Increment the value by 1

# Check if the inner loop should continue


bne $t2, $t1, loop_inner # If $t2 is not equal to $t1, repeat the inner loop

# Print a newline character


li $v0, 4 # Set the syscall code for printing a string
la $a0, output # Load the address of the output string
syscall # Perform the syscall
addi $t1, $t1, 1 # Increment the counter by 1

# Check if the outer loop should continue


ble $t1, $t0, loop_outer # If $t1 is less than or equal to $t0, repeat the outer loop

# Exit the program


li $v0, 10 # Set the syscall code for program exit
syscall # Perform the syscall

Question 11: Write a function with three arguments. In register $a0 receives the init address of
a string, in register $a1 receives the ASCII code of a char, and in register $a2 receives the ASCII
code of other char. The function must replace in the string any occurrence of the char stored in
$a1 by the char stored in register $a2.

- Answer:
+/ MIPS assembly code that defines a function with three arguments. The function takes an
initial address of a string, an ASCII code of a character to be replaced, and an ASCII code of the
replacement character. It replaces any occurrence of the character in the string with the
replacement character:

# Function to replace a character in a string


# Arguments: $a0 - initial address of the string
# $a1 - ASCII code of the character to be replaced
# $a2 - ASCII code of the replacement character

replace_character:
# Save registers on the stack
addiu $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)

move $s0, $a0 # Save initial address of the string


move $s1, $a2 # Save ASCII code of the replacement character

loop:
lb $t0, 0($a0) # Load a character from the string
beqz $t0, end # If the character is null, exit the loop

beq $t0, $a1, replace # If the character matches the one to be replaced, go to the replace
label

addiu $a0, $a0, 1 # Move to the next character in the string


j loop # Jump back to the loop label

replace:
sb $s1, 0($a0) # Replace the character with the replacement character

addiu $a0, $a0, 1 # Move to the next character in the string


j loop # Jump back to the loop label

end:
# Restore registers from the stack
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addiu $sp, $sp, 12

jr $ra # Return from the function

+/ To call this function from the main program, you can use the following code:

.data
str: .asciiz "Hello, World!" # Example string

.text
.globl main

main:
# Print the original string
li $v0, 4
la $a0, str
syscall

# Call the replace_character function


li $a1, 108 # ASCII code for 'l'
li $a2, 120 # ASCII code for 'x'
jal replace_character

# Print the modified string


li $v0, 4
la $a0, str
syscall

# Exit the program


li $v0, 10
syscall
Question 12: Consider the following program. Write an equivalent program, using the MIPS 32
assembly language.
void function1 ()
{
int z;
int array[1024]; // local variable
for (i = 0; i < 1024; i++)
array[i] = i;
z = sum(array, 1024);
print_int(z);
}
int sum(int array[], int n)
{
int s = 0;
int i;
for (i = 0; i < n; i ++)
s = s + array[i];
return s;
}

- Answer:
+/ The equivalent MIPS assembly code for the given C program:

.data
array: .space 4096 # Allocate space for the array

.text
.globl main

main:
jal function1 # Call function1

# Exit the program


li $v0, 10
syscall

# function1
function1:
addiu $sp, $sp, -8 # Adjust stack pointer for local variables
sw $ra, 0($sp) # Save the return address on the stack
sw $s0, 4($sp) # Save $s0 on the stack

la $s0, array # Load the address of the array into $s0

li $t0, 0 # Initialize i to 0

loop1:
bge $t0, 1024, end_loop1 # If i >= 1024, exit the loop

sw $t0, ($s0) # Store the value of i at the current array index

addiu $s0, $s0, 4 # Move to the next array element

addiu $t0, $t0, 1 # Increment i by 1

j loop1 # Jump back to loop1

end_loop1:
move $a0, $s0 # Pass the address of the array to sum
li $a1, 1024 # Pass 1024 as the value of n
jal sum # Call sum

move $a0, $v0 # Move the result of sum (z) to $a0


li $v0, 1 # Set the syscall code for printing an integer
syscall # Print the value of z

lw $ra, 0($sp) # Restore $ra from the stack


lw $s0, 4($sp) # Restore $s0 from the stack
addiu $sp, $sp, 8 # Restore the stack pointer

jr $ra # Return from the function

# sum
sum:
addiu $sp, $sp, -12 # Adjust stack pointer for local variables
sw $ra, 0($sp) # Save the return address on the stack
sw $s0, 4($sp) # Save $s0 on the stack

move $s0, $a0 # Move the address of the array to $s0


move $t1, $a1 # Move the value of n to $t1

li $t0, 0 # Initialize i to 0
li $s1, 0 # Initialize s to 0

loop2:
bge $t0, $t1, end_loop2 # If i >= n, exit the loop

lw $t2, ($s0) # Load the value at the current array index

add $s1, $s1, $t2 # Add the value to s

addiu $s0, $s0, 4 # Move to the next array element

addiu $t0, $t0, 1 # Increment i by 1


j loop2 # Jump back to loop2

end_loop2:
move $v0, $s1 # Move the sum (s) to $v0

lw $ra, 0($sp) # Restore $ra from the stack

lw $s0, 4($sp) # Restore $s0 from the stack


addiu $sp, $sp, 12 # Restore the stack pointer

jr $ra # Return from the function

Question 13: Translate to assembly the following functions:


inf f (int k)
{
int v[100];
int i = 0;
int s = 0;
for (i = 0; i < k; i = i +2)
v[i] = g(k + 2);
for (i = 0; i < k; i = i + 2)
s = s + v[i];
return (s);
}

int g(int k)
{
if (k % 2 == 0)
return (k * k + 2);
else
return k * (-1);
}

- Answer:
+/ MIPS assembly code translation for the given C functions:

# Function prototypes
.globl f
.globl g

# Data segment
.data
v: .space 400 # Allocate space for the array v

# Text segment
.text

# Function f
f:
addiu $sp, $sp, -16 # Adjust stack pointer for local variables
sw $ra, 0($sp) # Save the return address on the stack
sw $s0, 4($sp) # Save $s0 on the stack
sw $s1, 8($sp) # Save $s1 on the stack

li $s0, 0 # Initialize i to 0
li $s1, 0 # Initialize s to 0

loop1:
bge $s0, $a0, end_loop1 # If i >= k, exit the loop

jal g # Call function g

sw $v0, ($s0) # Store the result of g in v[i]

addiu $s0, $s0, 4 # Move to the next array element

addiu $s0, $s0, 4 # Increment i by 2

j loop1 # Jump back to loop1

end_loop1:
move $t0, $a0 # Move the value of k to $t0
sll $t0, $t0, 2 # Multiply k by 4 to get the array index

lw $s0, 0($sp) # Restore $s0 from the stack


move $s1, $zero # Reset s to 0

loop2:
bge $s0, $a0, end_loop2 # If i >= k, exit the loop

lw $t1, v($t0) # Load the value at the current array index

add $s1, $s1, $t1 # Add the value to s

addiu $t0, $t0, 8 # Move to the next array element

addiu $s0, $s0, 2 # Increment i by 2

j loop2 # Jump back to loop2

end_loop2:
lw $ra, 0($sp) # Restore $ra from the stack
lw $s0, 4($sp) # Restore $s0 from the stack
lw $s1, 8($sp) # Restore $s1 from the stack
addiu $sp, $sp, 16 # Restore the stack pointer

move $v0, $s1 # Move the sum (s) to $v0

jr $ra # Return from the function

# Function g
g:
addiu $sp, $sp, -4 # Adjust stack pointer for local variables
sw $ra, 0($sp) # Save the return address on the stack

rem $t0, $a0, 2 # Remainder of k divided by 2

beqz $t0, even # If remainder is zero, go to even

neg $v0, $a0 # k * (-1)


j end_g # Jump to end_g

even:
mul $v0, $a0, $a0 #k*k
addiu $v0, $v0,
li $t0, 2
addu $v0, $v0, $t0 #k*k+2

end_g:
lw $ra, 0($sp) # Restore $ra from the stack
addiu $sp, $sp, 4 # Restore the stack pointer

jr $ra # Return from the function

Question 14: Let be a 16 bit computer, with byte addressing and a set of 60 machine
instructions. The computer has 8 registers. Show the instruction format for the following
hypothetical instruction ADDV R1, R2, M, where R1 and R2 are registers, and M is memory
address.

- Answer:
+/ For the given hypothetical instruction ADDV R1, R2, M, where R1 and R2 are registers, and M
is a memory address, we can define the following instruction format for a 16-bit computer with
byte addressing:
1/ Opcode (6-bits): This field represents the opcode of the instruction. It specifies the operation
to be performed. Since we have 60 machine instructions, we need 6-bits to represent the
opcode uniquely.
2/ Register R1 (3-bits): This field represents the destination register R1 where the result of the
operation will be stored. We have 8 registers available, so 3-bits are sufficient to represent the
register number.
3/ Register R2 (3-bits): This field represents the destination register R2 where the value will be
fetched for the operation. Again, 3 bits are sufficient to represent the register number.
4/ Memory Address M (4 bits): This field represents the memory address M from which data
will be fetched for the operation. As we have byte addressing, we need 4 bits to represent the
memory address.

Question 15: A 32 bit computer with byte addressing, has 64 machine instructions, and 128
registers. Given the instruction SWAPM addr1, addr2 that swaps the content of the memory
address addr1 and addr2.
a) What is the memory space addressable by this computer?
b) What is the instruction format?
c) Write a program fragment, using the MIPS 32 assembly language, equivalent to the
previous instruction.
d) If the above instruction must occupy one word, what is the range of addresses that
can be addressed by addr1 and addr2?

Question 16: A 32-bit computer with memory addressed by bytes has 64 registers. We want to
design the format for the instructions of this computer, assuming:
- The computer has 115 instructions.
- The execution model in register-register.
- The computer has the following types of instructions:
· Instructions for transferring data between memory and registers. These instructions
use two types of addressing modes: direct, and base-register addressing
· Arithmetic and logic instructions.
· Branch instructions. There are two types of instructions: unconditional to a memory
address, and conditional branches. For conditional branches, the condition is expressed
in a register, and the branch is indicated with PC-relative addressing.
a) Design the format for the instructions of this computer, having into account that all
instructions occupy one word.
b) If the displacement used in the instructions with base-register addressing uses two-
complement, what is the maximum value for these displacements?
c) If the address used in the unconditional branch instructions use binary code
(unsigned), what is the range of addresses for the branch?
d) What is the model of this computer, RISC or CISC?

You might also like