Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- .align 2 # Let's make sure that it's aligned
- Z: .word 10, 11, 4, 10, 24, 1, 16, 17, 18, 19, 10
- X: .word 0
- # above we allocated and initialized
- # twenty elements of array A
- .space 320 # here we allocated space here for...
- # ...how many more elements?
- message2: .asciiz "Enter k value above 0 and less than 9\nK = " # string to print
- message3: .asciiz "\nM = " # string to print
- message4: .asciiz "\nTotal = " # string to print
- .text
- .globl main
- main: # main has to be a global label
- addu $s7, $0, $ra # save the return address
- # in a global register
- #la $s2, X # set h to 40 for the examples
- #la $s3, Z # $s3 has the starting address of A
- .text
- li $v0, 4 # print_str (system call 4)
- la $a0, message2 # takes the address of string as an argument
- syscall
- li $v0, 5 # read_int (system call 5)
- syscall
- # STORE VALUE
- add $s1, $0, $v0 # s1 = k
- li $v0, 4 # print_str (system call 4)
- la $a0, message3 # takes the address of string as an argument
- syscall
- li $v0, 5 # read_int (system call 5)
- syscall
- # STORE VALUE
- add $s4, $0, $v0 # s4 = m
- # LOOPS
- la $a0, Z # $a0 has the starting address of Z
- move $t0, $zero # i = 0
- #la $s5, Z # $s5 has the starting address of Z
- loop1:
- add $t1,$t0,$t0 # $t1 = 2i
- add $t1,$t1,$t1 # $t1 = 4i
- add $s5,$a0,$t1 # $s5 = array[i]
- addi $t0,$t0,1 # i = i + 1
- slt $t3, $t0, $s1 # $t3 = (i < size0)
- bne $t3, $zero, loop1
- lw $t4, 0($s5) #t0 = Z[k]
- #la $s5, Z # $s5 has the starting address of Z
- add $s4, $s4, $s1 # s4 = m + k
- loop2:
- add $t1,$t0,$t0 # $t1 = 2i
- add $t1,$t1,$t1 # $t1 = 4i
- add $s5,$a0,$t1 # $s5 = array[i]
- addi $t0,$t0,1 # i = i + 1
- slt $t3, $t0, $s4 # $t3 = (i < size0)
- bne $t3, $zero, loop2
- lw $t0, 0($s5) # t0 = Z[m+k]
- add $t0, $t0, $t4 # t0 = Z[k] + Z[m+k]
- li $v0, 4 # print_str (system call 4)
- la $a0, message4 # message
- syscall
- li $v0, 1 # print_int (system call 1)
- add $a0, $0, $t0 # put value g in $a0 for printing
- syscall
- #--------------------------------------Usual stuff at the end of the main
- addu $ra, $0, $s7 # restore the return address
- jr $ra # return to the main program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement