Instruction Set Archeatecture
Instruction Set Archeatecture
Instruction Set Archeatecture
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
}
- Answer:
- MIPS code for the C fragment:
=> 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.
- 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
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:
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:
.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
print_result:
# Print the result
li $v0, 4
la $a0, output
syscall
li $v0, 1
move $a0, $t2
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:
.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
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:
.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
# Read number B
li $v0, 5
syscall
move $t1, $v0 # Store number B in $t1
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
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
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
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:
replace_character:
# Save registers on the stack
addiu $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
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
replace:
sb $s1, 0($a0) # Replace the character with the replacement character
end:
# Restore registers from the stack
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addiu $sp, $sp, 12
+/ 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
- 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
# 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
li $t0, 0 # Initialize i to 0
loop1:
bge $t0, 1024, end_loop1 # If i >= 1024, exit the loop
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
# 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
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
end_loop2:
move $v0, $s1 # Move the sum (s) to $v0
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
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
loop2:
bge $s0, $a0, end_loop2 # If i >= k, exit the loop
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
# Function g
g:
addiu $sp, $sp, -4 # Adjust stack pointer for local variables
sw $ra, 0($sp) # Save the return address on the stack
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
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?