Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore 2019kuc1045_COA_Lab_File

2019kuc1045_COA_Lab_File

Published by 2019kucp1054, 2021-06-27 16:23:16

Description: 2019kuc1045_COA_Lab_File

Search

Read the Text Version

COMPUTER ORGANIZATION AND ARCHITECTURE (LAB FILE) Sujeet Raj Chaudhari 2019KUCP1045

Program 1:- Addition, Subtraction, Division, Multiplication #PROGRAM for Arithmetic Operations # data declarations .data # input prompts \"Enter a: \" prompt1: .asciiz \"Enter b: \" prompt2: .asciiz # output prompts result1: .asciiz \"\\n a + b = \" result2: .asciiz \"\\n a - b = \" result3: .asciiz \"\\n a * b = \" result4: .asciiz \"\\n a / b = \" result5: .asciiz \"\\n a % b = \" # program code .text .globl main main: # ============================== INPUT ============================== #

# prompt user to enter a # print string li $v0, 4 # $a0 = address of prompt1 la $a0, prompt1 syscall # get user input # read integer li $v0, 5 # move integer input (a) to $t0 syscall move $t0, $v0 # prompt user to enter b # print string li $v0, 4 # $a0 = address of prompt2 la $a0, prompt2 syscall # get user input # read integer li $v0, 5 # move integer input (b) to $t1 syscall move $t1, $v0 # =============================== SUM =============================== # add $t2, $t0, $t1 # $t2 = $t0 + $t1 li $v0, 4 # print string

la $a0, result1 # $a0 = address of result1 syscall li $v0, 1 # print integer move $a0, $t2 # $a0 = $t2 syscall # ============================ DIFFERENCE ============================ # sub $t3, $t0, $t1 # $t3 = $t0 - $t1 li $v0, 4 # print string la $a0, result2 # $a0 = address of result2 syscall li $v0, 1 # print integer move $a0, $t3 # $a0 = $t3 syscall # ============================= PRODUCT ============================= # mul $t4, $t0, $t1 # $t4 = $t0 * $t1 li $v0, 4 # print string la $a0, result3 # $a0 = address of result3

syscall li $v0, 1 # print integer move $a0, $t4 # $a0 = $t4 syscall # ============================= QUOTIENT ============================= # div $t5, $t0, $t1 # $t5 = $t0 / $t1 li $v0, 4 # print string la $a0, result4 # $a0 = address of result4 syscall li $v0, 1 # print integer move $a0, $t5 # $a0 = $t5 syscall # ============================ REMAINDER ============================ # rem $t6, $t0, $t1 # $t6 = $t0 * $t1 li $v0, 4 # print string la $a0, result5 # $a0 = address of result5 syscall

li $v0, 1 # print integer move $a0, $t6 # $a0 = $t6 syscall # terminate li $v0, 10 syscall OUTPUT-

PROGRAM 2:- Volume and surface area of the parallelepiped #Program for Surface Area and Volume of Parallelpiped .data a_side: .word 10 b_side: .word 10 c_side: .word 10 volume: .word 0 surface_area: .word 0 #Text/Code Section .text .globl main main: #-----------------INPUT------------------# #Load variable into registers lw $t0, a_side lw $t1, b_side lw $t2, c_side #----------------SOLUTION---------------# #Calculated Volume mul $t3, $t0, $t1 #lb

mul $t4, $t3, $t2 #lbh sw $t4, volume #Calculated Surface Area mul $t3, $t0, $t1 #lb mul $t4, $t0, $t2 #lh mul $t5, $t1, $t2 #bh add $t6, $t3, $t4 #lb+lh add $t7, $t6, $t5 #lb+lh+bh mul $t7, $t7, 2 #2(lb+lh+bh) sw $t7, surface_area li $v0, 10 syscall .endmain

OUTPUT:-

PROGRAM 3:- Interacting with console to read and print string and integers #PROGRAM for interaction with console to read and print string and integers .data buffer: .space 20 prompt: .asciiz \"\\n Enter number for integer \\n\" prompt1: .asciiz \"\\n Sum result of Integer part \\n\" prompt2: .asciiz \"\\n Enter string with max 20 chars \\n\" prompt3: .asciiz \"\\n The string you wrote : \\n\" .text .globl main main: #------------------------INTEGER------------------# li $v0, 4 #print string la $a0, prompt #a0 = address of prompt syscall li $v0, 5 #read int move $t0, $v0 #$t0 = $v0 syscall li $t1, 12 # $t1 = 12

add $t3, $t0, $t1 #t3 = $t0+$t1 li $v0, 4 #print string la $a0, prompt1 syscall li $v0, 1 #print int move $a0, $t3 syscall #------------------------String-------------------# li $v0, 4 #print string la $a0, prompt2 #load address of prompt2 syscall li $v0, 8 #read string la $a0, buffer #load byte space to primary address li $a1, 20 #load the byte space for string move $t0, $a0 #save string to t0 syscall li $v0, 4 #print string la $a0, prompt3 #$a0 = address of prompt3 syscall li $v0, 4 #print string la $a0, buffer #reload byte space to primary address move $a0, $t0 #primary address = t0 address syscall

#Terminate li $v0, 10 syscall OUTPUT :-

PROGRAM 4:- To find sum of squares of n numbers. #PROGRAM for Sum of Squares of n integers # data declarations .data # input prompts prompt: .asciiz \"Enter n: \" # output prompts result: .asciiz \"\\nsum = \" # program code .text .globl main main: # ============================== INPUT ============================== # # prompt user to enter n # print string li $v0, 4 # $a0 = address of prompt1 la $a0, prompt syscall

# get user input # read integer li $v0, 5 # move integer input (a) to $t0 syscall move $t0, $v0 # ============================ SOLUTION ============================ # loop_start: $t1, $t0, loop_end # if $t1 > $t0 then loop_end bgt mul $t2, $t1, $t1 # $t2 = $t1 * $t1 add addi $t3, $t3, $t2 # $t3 = $t3 + $t2 j $t1, $t1, 1 # $t1 = $t1 + 1 loop_start # jump to loop_start loop_end: $v0, 4 # print string li $a0, result # $a0 = address of result la syscall li $v0, 1 # print integer move $a0, $t3 # $a0 = $t3 syscall # terminate li $v0, 10

syscall OUTPUT:-

PROGRAM 5 :- To read two numbers and print in run time to determine the greater than, less than or equal number. #PROGRAM To print Greater Smaller and Equal .data #input prompts line1: .asciiz \"\\n Enter integer a: \\n\" line2: .asciiz \"\\n Enter integer b: \\n\" #output prompt line3: .asciiz \"\\n a is greater number = \\n\" line4: .asciiz \"\\n b is smaller number = \\n\" line5: .asciiz \"\\n a is smaller number = \\n\" line6: .asciiz \"\\n b is greater number = \\n\" result1: .asciiz \"\\n a is greater than b \\n\" result2: .asciiz \"\\n a is less than b \\n\" result3: .asciiz \"\\n a is equal to b \\n\" .text

.globl main main: #-------------------------------INPUT----------------------------# #prompt user to enter number 1 li $v0, 4 #print string la $a0, line1 #a0 = address of line1 syscall li $v0, 5 #read integer syscall #move integer input(a) to $t0 move $t0, $v0 #prompt user to enter number 2 li $v0, 4 #print string la $a0, line2 #a0 = address of line2 syscall li $v0, 5 #read integer syscall #move integer input(b) to $t1 move $t1, $v0 #------------------------------SOLUTION------------------------# beq $t0, $t1, print_equal #if($t0 == $t1) then result3

slt $s0, $t0, $t1 #checks if($t0 < $t1) beq $s0, 1, print_smaller #if($t0 < $t1) then result2 beq $s0, $zero, print_greater #check if($t0 > $t1) then result1 print_greater: li $v0, 4 la $a0, result1 syscall li $v0, 4 la $a0, line3 syscall li $v0, 1 move $a0, $t0 syscall li $v0, 4 la $a0, line4 syscall li $v0, 1

move $a0, $t1 syscall li $v0, 10 syscall print_smaller: li $v0, 4 la $a0, result2 syscall li $v0, 4 la $a0, line5 syscall li $v0, 1 move $a0, $t0 syscall li $v0, 4 la $a0, line6 syscall li $v0, 1 move $a0, $t1

syscall li $v0, 10 syscall print_equal: li $v0, 4 la $a0, result3 syscall li $v0, 10 syscall li $v0, 10 #system call for exit syscall

OUTPUT:-

PROGRAM 6:- To determine minimum and maximum number from an array of integers stored and print them back on console. # PROGRAM for Max and Min in an array .data array: .word 3, 8, 12, 16, 20, 25, 1, 2, 29, 5 length: .word 10 max: .asciiz \"\\n Max element value : \\n\" min: .asciiz \"\\n Min element value : \\n\" .text .globl main main: #--------------------SOLUTION-------------------# la $a0, array #load base address lw $a1, length #copy length of array lw $t2, ($a0) #max value lw $t3, ($a0) #min value loop: #if(length = 0) go to print beq $a1, $zero, print lw $t0, ($a0)

bge $t0, $t3, not_min #if(curr_value >= curr_min) move $t3, $t0 #else update min not_min: ble $t0, $t2, not_max # if (curr_value <= curr_max) {no need to update max value} move $t2, $t0 #else update max not_max: # length = length -1; addi $a1, $a1, -1 # $a0 = $a0 +4; go to next address addi $a0, $a0, 4 #unconditional jump; j loop print: #Print Max Value li $v0, 4 la $a0, max syscall li $v0, 1 move $a0, $t2 syscall #Print min Value li $v0, 4 la $a0, min syscall

li $v0, 1 move $a0, $t3 syscall #Terminate li $v0, 10 syscall OUTPUT:-

PROGRAM 7:- WAP to implement the following: (i) If - ELSE (ii) D0-While loop (iii) While loop (iv) For Loop (v) Array operation (vi) Summing contents of an array. (i) If else #PROGRAM for implementing if else .data num: .asciiz \"\\n Enter a number \\n\" positive: .asciiz \"\\n Entered value is positive \\n\" negative: .asciiz \"\\n Entered value is negative \\n\" .text .globl main main: #---------------------INPUT-------------------------------# li $v0, 4 #print string

la $a0, num #$a0 = address of num syscall li $v0, 5 #read int value syscall move $t0, $v0 #----------------------SOLUTION--------------------------# move $t1, $zero li $t2, 1 blt $t0, $t1, true #if num < 0 go to true part li $v0, 4 #print string positive la $a0, positive syscall j Exit #jump to Exit true: li $v0, 4 #print negative string la $a0, negative syscall Exit: li $v0, 10 syscall

OUTPUT:-

(II)Do While Loop #PROGRAM for implementation of Do While Loop .data end: .asciiz \"\\n\" msg: .asciiz \"\\n Loop ends\" .text .globl main main: #counter i li $t0, 0 li $t1, 10 loop: #$t0 = #t0 + 1 addi $t0, $t0, 1 #$a0 = $t0 li $v0, 1 move $a0, $t0 syscall li $v0, 4 #print string la $a0, end syscall blt $t0, $t1, loop #if $t0< $t1 go to loop

exit: li $v0, 4 la $a0, msg syscall li $v0, 10 syscall OUTPUT:-

(iii) While Loop #PROGRAM for implementation of While Loop .data .text .globl main main: #$t0 = 0 li $t0, 0 #$t0 = 0 li $t1, 0 while: #if($t0 = 9) go to exit beq $t0, 9, exit # else $t1 = $t1 + $t0 add $t1, $t1, $t0 # $t0 = $t0 + 1 addi $t0, $t0, 1 j while #jump to while exit: #$a0 = $t1 move $a0, $t1 li $v0, 1 syscall #Terminate li $v0, 10 syscall

OUTPUT:- (iv) Summing of elements of array #PROGRAM for summing of contents in array .data array: .word 5, 4, 14, -3, -1, -5, 2, -16 length: .word 8 .text .globl main main: #load array la $a0, array #load the first array value in $t0 lw $t0, ($a0) li $t1, 1 #initialize the counter to one

lw $a1, length #load size in a1 loop: #if $t1 = $a1(i =size) then go to exit beq $t1, $a1, exit #increment the pointer addi $a0, $a0, 4 addi $t1, $t1, 1 #increment the loop counter lw $t2, ($a0) #store the next array value in $t2 add $t0, $t0, $t2 #$t0 = $t0 + $t2 j loop #jump to loop exit: #print int move $a0, $t0 li $v0, 1 syscall #Terminate li $v0, 10 syscall

OUTPUT:- PROGRAM 8:- WAP to arrange 10 numbers in ascending order. #PROGRAM for sorting array elemets in ascending order .data space: .asciiz \" \" arr : .word 7, 5, 39, 13, 943, 783, 300, 57 N: .word 8 .text main: #---------------------SOLUTION------------------------#

la $t0, arr # Copy the base address of array into $t0 add $t0, $t0, 40 # 4 bytes per int * 10 ints = 40 bytes (end address of the array) outterLoop: # Used to determine when we are done iterating over the Array li $t1,0; # $t1 used determine when the list is sorted la $a0, arr # Set $a0 to the base address of the Array innerLoop: # The inner loop will iterate over the Array checking if a swap is needed lw $t2, 0($a0) # set $t2 to the current element in array lw $t3, 4($a0) # set $t3 to the next element in array slt $t5, $t2, $t3 # $t5 = 1 if $t2 < $t3 else $t5 =0 beq $t5, $0, continue # if $t5 = 1, then swap them add $t1, $0, 1 # if we need to swap, we need to check the list again sw $t2, 4($a0) # store the greater numbers in the higher position in array sw $t3, 0($a0) # store the lesser numbers in the lower position in array continue: addi $a0, $a0, 4 # array to start at the next location from last time bne $a0, $t0, innerLoop # If $a0 != the end of Array, jump back to innerLoop bne $t1, $0, outterLoop # If $t1 != 1, loop again

li $t6, 36 #location of array = 36 #----------------Print Sorted array---------------------# loop: blt $t6, 0, finish #if location <= 0 then finish lw $a0, arr($t6) #load current element of array to be printed li $v0, 1 #print integer syscall #execute la $a0, space #load a space: \" \" li $v0, 4 #print string syscall sub $t6, $t6, 4 #Every 4 bytes there is an integer in the array b loop #goto loop finish: #exit program li $v0, 10 syscall

OUTPUT:-

PROGRAM 9:- WAP to determine the factorial. #PROGRAM for Factorial of a number #data declarations .data #input prompts prompt1: .asciiz \"\\n Enter the number n: \\n\" #output prompts result: .asciiz \"\\n Factorial of entered number : \\n\" #program code .text .globl main main: #----------------------INPUT--------------------# #prompt user to enter number li $v0, 4 # print string la $a0, prompt1 #$a0 = address of prompt1 syscall

#user input li $v0, 5 # read integer syscall move $t0, $v0 # move integer input to $t0 #----------------------SOLUTION-----------------# li $t1, 1 loop_start: #if $t1 < 0 then loo _end blt $t0, 1, loop_end # $t1 = $t0*$t1 mul $t1, $t0, $t1 sub $t0, $t0, 1 # $t0 = $t0-1 j loop_start loop_end: #print string li $v0, 4 #a0 = address of result prompt la $a0, result syscall li $v0, 1 #print integer move $a0, $t1 #$a0 = $t1 syscall #terminate li $v0, 10

syscall OUTPUT:-

PROGRAM 10:- WAP to generate Fibonacci series. #PROGRAM to generate Fibonacci Series .data description: .asciiz \"\\n First 10 fibonacci numbers : \\n\" delim: .asciiz \",\" size: .word 10 .text .globl main main: #----------------------------SOLUTION------------------# li $v0, 4 #print string la $a0, description syscall #----loop from $t0 = 0 until 10-----# move $t0, $zero # $t0 = 0 loop: # if($t0 = 10) then go to exit beq $t0, 10, exit

jal fibonacci # go to address of fibonacci jal print_output # go to address of print_output addi $t0, $t0, 1 #$t0 = $t0+1 j loop #Terminate exit: li $v0, 10 syscall #-------------Use Stack--------------------------# #-----------Push----------------# addi $sp, $sp, -4 #decrement $sp by 4 sw $t0, 0($sp) #store word to 0($sp) #--------------Pop--------------# lw $t1, 0($sp) #load word from top of stack addi $sp, $sp, 4 #increment $sp by 4 #-------------Output and Fibonacci----------------# fibonacci:

move $t1, $zero #$t1 = 0 move $t2, $sp # push initial $t0 on stack li $t3, 1 addi $sp, $sp, -4 sw $t0, 0($sp) recursive_call: # if stack is empty, exit beq $sp, $t2, fib_exit lw $t4, 0($sp) # pop next $t4 off stack addi $sp, $sp, 4 bleu $t4, $t3, early_return #Label branch on less than equal unsigned sub $t4, $t4, 1 # push $t4 - 1 on stack addi $sp, $sp, -4 sw $t4, 0($sp) sub $t4, $t4, 1 # push $t4 - 2 on stack addi $sp, $sp, -4 sw $t4, 0($sp) j recursive_call early_return: add $t1, $t1, $t4

j recursive_call fib_exit: #go to address stored in $31 jr $31 print_output: li $v0, 1 #print int value move $a0, $t1 syscall li $v0, 4 #print string la $a0, delim syscall jr $31 #go to address stored in $31

OUTPUT:-


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook