Tasks & Solution of Computer Architecture & Logic Design
1. Introduction to Visible Virtual Machine
1) Write a program in which take input from user and add from input value.
Output:
Source Code:
IN INPUT
ADD 96
OUT OUTPUT
HLT HALT
*96
DAT 001
2) Write a program in which take input from user and subtract from user input value.
Output:
Source Code:
IN INPUT
SUB 96
OUT OUTPUT
HLT HALT
*96
DAT 005
3) Write a program in which take two inputs from user and add both and check the answer.
Output:
Source Code:
IN INPUT
STO 96
IN INPUT
ADD 96
OUT OUTPUT
HLT HALT
4) Write a program in which two constant numbers and add them.
Output:
Source Code:
ADD 96
ADD 97
OUT OUTPUT
HLT HALT
*96
DAT 005
*97
DAT 010
2. Arithmetic Operations
1) Write a program in which take input from user and add from input value.
Output:
Source Code:
IN INPUT
ADD 96
OUT OUTPUT
HLT HALT
*96
DAT 001
2) Write a program in which take input from user and subtract from user input value.
Output:
Source Code:
IN INPUT
SUB 96
OUT OUTPUT
HLT HALT
*96
DAT 005
3) Write a program in which take two inputs from user and add both and check the answer.
Output:
Source Code:
IN INPUT
STO 96
IN INPUT
ADD 96
OUT OUTPUT
HLT HALT
4) Write a program in which two constant numbers and add them.
Output:
Source Code:
ADD 96
ADD 97
OUT OUTPUT
HLT HALT
*96
DAT 005
*97
DAT 010
3. Loop
1) Write a program in which take input if input is equal to 0 then
print output otherwise again take input.Hint This is used by while loop.
Output:
Source Code:
IN
STO 95
BRZ 05
OUT
BR 00
OUT
HLT
2) Write a program in which take input and if input is less than 10 then add otherwise terminate the program.
Output:
Source Code:
IN
STO 99
SUB 98
BRP 08
LDA 99
ADD 99
OUT
BR 00
HLT
*98
DAT 010
3) Write a program in which print table.
Output:
Source Code:
IN
STO 95
lda 97
add 96
sto 97
lda 98
add 95
sto 98
out
lda 94
sub 97
brz 13
br 02
hlt
*94
dat 010
*96
dat 001
*97
dat 000
*98
dat 000
Practice of MIPs Architecture
4. Introduction to MIPs Architecture
1) Write a program to take three integers and add and display them.
Output:
Source Code:
.data
prompt: .asciiz "Enter three integers \n"
sum_msg: .asciiz "The sum is "
.text
.globl main
main:
la $a0,prompt
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
li $v0,5
syscall
move $t2,$v0
li $v0,5
syscall
move $t3,$v0
addu $t1,$t1,$t2
addu $t1,$t1,$t3
la $a0,sum_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
2) Write a program in which take three inputs of two times from user and add them.
Output:
Source Code:
.data
first: .asciiz "Enter three integers for 1st Addition\n"
second: .asciiz "Enter three integers for 2nd Addition\n"
first_msg: .asciiz "The sum of 1st Addition is "
second_msg: .asciiz "The sum of 2nd Addition is "
.text
.globl main
main:
la $a0,first
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
li $v0,5
syscall
move $t2,$v0
li $v0,5
syscall
move $t3,$v0
addu $t1,$t1,$t2
addu $t1,$t1,$t3
la $a0,first_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
la $a0,second
li $v0,4
syscall
li $v0,5
syscall
move $t4,$v0
li $v0,5
syscall
move $t5,$v0
li $v0,5
syscall
move $t6,$v0
addu $t4,$t4,$t5
addu $t4,$t4,$t6
la $a0,second_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
3) Write a program in which take two inputs from user and multiply them.
Output:
Source Code:
.data
first: .asciiz "Enter first Number \n"
second: .asciiz "Enter second Number\n"
first_msg: .asciiz "The Multiply of two Numbers are "
.text
.globl main
main:
la $a0,first
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
la $a0,second
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
mul $t1,$t1,$t2
la $a0,first_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
4) Write a program in which take two inputs from user and add both and check the answer.
Output:
Source Code:
.data
first: .asciiz "Enter first Number \n"
second: .asciiz "Enter second Number\n"
first_msg: .asciiz "The Addition of two Numbers are "
.text
.globl main
main:
la $a0,first
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
la $a0,second
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
add $t1,$t1,$t2
la $a0,first_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
5) Write a program in which take two inputs from user and sub both and check the answer.
Output:
Source Code:
.data
first: .asciiz "Enter first Number \n"
second: .asciiz "Enter second Number\n"
first_msg: .asciiz "The Addition of two Numbers are "
.text
.globl main
main:
la $a0,first
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
la $a0,second
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
sub $t1,$t1,$t2
la $a0,first_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
6) Write an assembly program that Read and Print Hello world.
Output:
Source Code:
.data
question: .asciiz "Enter String: "
size: .space 25
.text
.globl main
main:
li $v0,4
la $a0,question
syscall
li $v0,8
la $a1,size
syscall
li $v0,4
syscall
5. Arithmetic Operation
1) Write a program to print string.
Output:
Source Code:
.data
prompt: .ascii "Shoaib \n"
.text
.globl main
main:
li $v0,4
la $a0,prompt
syscall
li $v0,10
syscall
2) Add two Numbers and display the output with prompt
Output:
Source Code:
.data
prompt: .ascii "The sum of Numbers are \n"
num1: .word 10
num2: .word 15
.text
.globl main
main:
lw $t0,num1
lw $t1,num2
add $t2,$t0,$t1
li $v0,4
la $a0,prompt
syscall
move $a0,$t2
li $v0,1
syscall
li $v0,10
syscall
3) Write the same program with small variation i.e. this time the program will ask for 3 integers twice and displays the result for each addition separately;
Output will look like as follows:
Enter 3 integers for 1st addition
2 2 2
Enter 3 integers for 2nd addition
3 3 3
The sum of 1st addition is 6
The sum of 2nd addition is 9
Output:
Source Code:
.data
first: .asciiz "Enter three integers for 1st Addition\n"
second: .asciiz "Enter three integers for 2nd Addition\n"
first_msg: .asciiz "The sum of 1st Addition is "
second_msg: .asciiz "The sum of 2nd Addition is "
.text
.globl main
main:
la $a0,first
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
li $v0,5
syscall
move $t2,$v0
li $v0,5
syscall
move $t3,$v0
addu $t1,$t1,$t2
addu $t1,$t1,$t3
la $a0,first_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
la $a0,second
li $v0,4
syscall
li $v0,5
syscall
move $t4,$v0
li $v0,5
syscall
move $t5,$v0
li $v0,5
syscall
move $t6,$v0
addu $t4,$t4,$t5
addu $t4,$t4,$t6
la $a0,second_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
4) Write an assembly program that Multiply two number within the range of 10.
Output:
Source Code:
.data
first: .asciiz "Enter first Number \n"
second: .asciiz "Enter second Number\n"
first_msg: .asciiz "The Multiply of two Numbers are "
.text
.globl main
main:
la $a0,first
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
la $a0,second
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
mul $t1,$t1,$t2
la $a0,first_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
5) Write an assembly program that Divide two number within the range of 10.
Output:
Source Code:
.data
first: .asciiz "Enter first Number \n"
second: .asciiz "Enter second Number\n"
first_msg: .asciiz "The Division of two Numbers are "
.text
.globl main
main:
la $a0,first
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
la $a0,second
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
div $t1,$t1,$t2
la $a0,first_msg
li $v0,4
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
6. BIT MANIPULATION INSTRUCTIONS
1) Add AND Operator by solving the bitwise instruction of all Logical gates.
Output:
When we write li $t0,0x00000000 instead of li $t0,0xffffffff then we will get:
Source Code:
.data
input: .asciiz "Enter integer Number: "
result: .asciiz "The result is: "
.text
.globl main
main:
li $t0,0xffffffff
la $a0,input
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
and $t2,$t1,$t0
la $a0,result
li $v0,4
syscall
move $a0,$t2
li $v0,1
syscall
li $v0,10
syscall
2) Add OR Operator by solving the bitwise instruction of all Logical gates.
Output:
When we write li $t0,0xffffffff instead of li $t0,0x00000000 then we will get:
Source Code:
.data
input: .asciiz "Enter integer Number: "
result: .asciiz "The result is: "
.text
.globl main
main:
li $t0,0x00000000
la $a0,input
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
or $t2,$t1,$t0
la $a0,result
li $v0,4
syscall
move $a0,$t2
li $v0,1
syscall
li $v0,10
syscall
3) Add XOR Operator by solving the bitwise instruction of all Logical gates.
Output:
When we write li $t0,0x00000000 instead of li $t0,0xffffffff then we will get:
Source Code:
.data
input: .asciiz "Enter integer Number: "
result: .asciiz "The result is: "
.text
.globl main
main:
li $t0,0xffffffff
la $a0,input
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
xor $t2,$t1,$t0
la $a0,result
li $v0,4
syscall
move $a0,$t2
li $v0,1
syscall
li $v0,10
syscall
4) Add NOT Operator by solving the bitwise instruction of all Logical gates.
Output:
Source Code:
.data
input: .asciiz "Enter Number "
result: .asciiz "The result is "
.text
.globl main
main:
li $v0,4
la $a0,input
syscall
li $v0,5
syscall
move $t0,$v0
not $t1,$t0
li $v0,4
la $a0,result
syscall
move $a0,$t1
li $v0,1
syscall
li $v0,10
syscall
5) Add XNOR Operator by solving the bitwise instruction of all Logical gates.
Output:
When we write li $t0,0x00000000 instead of li $t0,0xffffffff then we will get:
Source Code:
.data
input: .asciiz "Enter integer Number: "
result: .asciiz "The result is: "
.text
.globl main
main:
li $t0,0xffffffff
la $a0,input
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
xor $t2,$t1,$t0
la $a0,result
li $v0,4
syscall
not $t3,$t2
move $a0,$t3
li $v0,1
syscall
li $v0,10
syscall
6) Add NAND Operator by solving the bitwise instruction of all Logical gates.
Output:
When we write li $t0,0x00000000 instead of li $t0,0xffffffff then we will get:
Source Code:
.data
input: .asciiz "Enter Number "
result: .asciiz "The result is "
.text
.globl main
main:
li $t0,0xffffffff
li $v0,4
la $a0,input
syscall
li $v0,5
syscall
move $t1,$v0
nor $t2,$t0,$t1
li $v0,4
la $a0,result
syscall
move $a0,$t2
li $v0,1
syscall
li $v0,10
syscall
7) Add NAND Operator by solving the bitwise instruction of all Logical gates.
Output:
When we write li $t0,0x00000000 instead of li $t0,0xffffffff then we will get:
Source Code:
.data
input: .asciiz "Enter Number "
result: .asciiz "The result is "
.text
.globl main
main:
li $t0,0xffffffff
la $a0,input
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
and $t2,$t1,$t0
la $a0,result
li $v0,4
syscall
not $t3,$t2
move $a0,$t3
li $v0,1
syscall
li $v0,10
syscall
8) Add NOR Operator by solving the bitwise instruction of all Logical gates.
Output:
When we write li $t0,0x00000000 instead of li $t0,0xffffffff then we will get:
Source Code:
.data
input: .asciiz "Enter Number "
result: .asciiz "The result is "
.text
.globl main
main:
li $t0,0xffffffff
li $v0,4
la $a0,input
syscall
li $v0,5
syscall
move $t1,$v0
nor $t2,$t0,$t1
li $v0,4
la $a0,result
syscall
move $a0,$t2
li $v0,1
syscall
li $v0,10
syscall
All Result are shown here
7. IF THEN ELSE; CONTROL STRUCTURE
1) Write a program to take a and b as input and if a > 0 then add a +b else a-b.
Output:
Source Code:
.data
input1: .asciiz "Enter First Number "
input2: .asciiz "Enter Second Number "
add: .asciiz "The Addition is "
sub: .asciiz "\nThe Subtraction is "
.text
.globl main
main:
la $a0,input1
li $v0,4
syscall
li $v0,5
Syscall
Move $t1,$v0
la $a0,input2
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
bgtz $t1,True
sub $t3,$t1,$t2
move $a0,$t3
li $v0,1
b Exit
syscall
True:
add $t3,$t1,$t2
move $a0,$t3
li $v0,1
syscall
b Exit
Exit:
li $v0,10
syscall
2) Write a program to take a and b as input and if a < b then print “a is less than b” else print “a is greater than b”.
Output:
Source Code:
.data
input1: .asciiz "Enter First Number "
input2: .asciiz "Enter Second Number "
add: .asciiz "The Addition is "
sub: .asciiz "\nThe Subtraction is "
print1: .asciiz "The a is less than b"
print2: .asciiz "The a is greater than b"
.text
.globl main
main:
la $a0,input1
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
la $a0,input2
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
blt $t1,$t2,True
la $a0,print2
li $v0,4
syscall
b Exit
True:
la $a0,print1
li $v0,4
syscall
b Exit
Exit:
li $v0,10
syscall
3) Write a program to take a and b as input and if a != b then print “a” and increment a with 1 else print “b” with increment 1.
Output:
Source Code:
.data
input1: .asciiz "Enter First Number "
input2: .asciiz "Enter Second Number "
add: .asciiz "The Addition is "
sub: .asciiz "\nThe Subtraction is "
print1: .asciiz "The a is less than b"
print2: .asciiz "The a is greater than b"
.text
.globl main
main:
la $a0,input1
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
la $a0,input2
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
bne $t1,$t2,True
addi $t1,$t1,1
move $a0,$t1
li $v0,1
syscall
b Exit
True:
addi $t2,$t2,1
move $a0,$t2
li $v0,1
syscall
b Exit
Exit:
li $v0,10
syscall
4) Write a program to check whether the input taken from user is greater than 0 or not. If the input is greater than 0, Do logical right shift operation on the input else do logical left shift operation on input.
Output:
Source Code:
.data
input1: .asciiz "Enter First Number "
output1: .asciiz "Right shift "
output2: .asciiz "left shift "
.text
.globl main
main:
la $a0,input1
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
bgt $t1,$zero,True
sll $t2,$t1,2
la $a0,output1
li $v0,4
syscall
move $a0,$t2
li $v0,1
syscall
True:
srl $t2,$t1,2
la $a0,output1
li $v0,4
syscall
move $a0,$t2
li $v0,1
syscall
b Exit
Exit:
li $v0,10
syscall
6) Write a program in MIPS assembly language that takes input from user and print whether the input is greater or less than 10 and also shift input left and right 4 bits.
Output:
Source Code:
data
greater: .asciiz "The Number is
greater than 10 \n"
less: .asciiz "The Number is
less than 10 \n"
num: .asciiz "Enter First Number"
const: .word 10
right: .asciiz "The Right Shift "
left: .asciiz "The Left Shift "
.text
.globl main
main:
lw $t4,const
la $a0,num
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
bgt $t1,$t4,True
la $a0,less
li $v0,4
syscall
la $a0,left
li $v0,4
syscall
sll $t2,$t1,4
move $a0,$t2
li $v0,1
syscall
b Exit
True:
la $a0,greater
li $v0,4
syscall
la $a0,right
li $v0,4
syscall
sll $t2,$t1,4
move $a0,$t2
li $v0,1
syscall
b Exit
Exit:
li $v0,10
syscall
8. FOR LOOP
1) Write a program to print table of 2
Output:
Source Code:
.data
prompt: .asciiz "Enter lenght for table: "
prompt1: .asciiz "Enter table : "
tab: .asciiz " : "
tab1: .asciiz "\n"
.text
.globl main
main:
la $a0,prompt
li $v0,4
syscall
li $v0,5
syscall
move $t0,$v0
la $a0,prompt1
li $v0,4
syscall
li $v0,5
syscall
move $t5,$v0
true:
bgtz $t0,lop
b exit
lop:
addi $t1,$t1,1
mul $t3,$t1,$t5
move $a0,$t1
li $v0,1
syscall
la $a0,tab # print tab
li $v0,4
syscall
move $a0,$t3
li $v0,1
syscall
la $a0,tab1 # print tab
li $v0,4
syscall
subi $t0,$t0,1
b true
exit:
li $v0,10
syscall
2) Write a program in MIPS assembly language that takes input and display whether number is prime or not.
Output:
Source Code:
.data
input: .asciiz "Enter any number = "
notPrime: .asciiz "This number is not prime number"
prime: .asciiz "This is Prime Number"
.text
.globl main
main:
li $s0,2 #s0 ==> i
la $a0,input
li $v0,4
syscall
li $v0,5
syscall
move $t0,$v0 #t0 user input(n)
blt $t0,$s0,notprime
div $t0,$s0
mflo $t1 #t1 ==> loop n/2
b condition
condition:
ble $s0,$t1,enterLoop
la $a0,prime # this prime number
li $v0,4
syscall
b exit
enterLoop:
div $t0,$s0 #n/i
mfhi $t2 #t2 ==> 0 or 1
beqz $t2,notprime
addi $s0,$s0,1
b condition
notprime:
la $a0,notPrime
li $v0,4
syscall
b exit
exit:
li $v0,10
syscall
3) Write a program in MIPS assembly language that provide the sum from 1 to 10 using for Loop.
Output:
Source Code:
.data
input: .asciiz "Adding of from 1 to 10 Numbers is \n"
.text
.globl main
main:
la $a0,input
li $v0,4
syscall
li $t0,10
la $a0,0
loop:
add $a0,$a0,$t0
subi $t0,$t0,1
bgez $t0,loop
li $v0,1
syscall
li $v0,10
syscall
4) Write a program in MIPS assembly language that provide the sum from 1 to 99 using for Loop.
Output:
Source Code:
.data
promt:.asciiz"Enter number : "
line:.asciiz"\n"
.text
.globl main
main:
li $v0,4
la $a0,promt
syscall
li $v0,5
syscall
move $t3,$v0
li $t0,0
li $t1,1
loop:
add $t0,$t0,$t1
move $a0,$t0
li $v0,1
syscall
li $v0,4
la $a0,line
syscall
addi $t1,$t1+1
blt $t1,$t3,loop
li $v0,10
syscall
9. MULTISIM AND LOGIC IMPLEMENTATION
1) Write a program in which take three inputs and do this logic:
(BC)(!A)
Output:
2) Implement the following equations on MultiSim.
ABCD + A ( B + C) + BCD + ABD+ BC
Output: