Here is my code that calculates the sum of two arrays and then prints them in a message to the the console. I would now like to compare my two array sums and return the correct message prompt depending on which array is larger. I feel like I'm very close because I tried to follow the logic from the first printed statements but how can I compare register $s7 & $s8 and then declare one the larger array through my message prompt I have set up? I'm using this emulator: https://cpulator.01xz.net/?sys=mipsr5-spim
.data
x: .word 1,2,3,4,5,6,7,8,9,10
iterator: .word 0
size: .word 9
prompt: .asciiz "The total sum of the array is equal to: "
.text
main:
la $s0, prompt
la $t0, x
lw $t1, iterator
lw $t2, size
begin_loop:
bgt $t1, $t2, exit_loop
sll $t3, $t1, 2
addu $t3, $t3, $t0
lw $t6, 0($t3)
addu $s7, $s7, $t6
addi $t1, $t1, 1
j begin_loop
exit_loop:
li $v0, 4
la $a0, prompt
syscall
li $v0, 1
la $a0, ($s7)
syscall
.data
x2: .word 5,6,7,8,9,10,11,12,13,14
iterator2: .word 0
size2: .word 9
prompt2: .asciiz "The total sum of array 2 is equal to: "
prompt3: .asciiz "The larger array is array 1"
prompt4: .asciiz "The larger array is array 2"
.text
main2:
la $s0, prompt2
la $t0, x2
lw $t1, iterator2
lw $t2, size2
begin_loop2:
bgt $t1, $t2, exit_loop2
sll $t3, $t1, 2
addu $t3, $t3, $t0
lw $t6, 0($t3)
addu $s8, $s8, $t6
addi $t1, $t1, 1
j begin_loop2
exit_loop2:
li $v0, 4
la $a0, prompt2
syscall
li $v0, 1
la $a0, ($s8)
syscall
slt $t1,$s7,$s8 # checks if $s0 > $s1
beq $t1,1,prompt3 # if $s7 > $s8, goes to prompt3
beq $t1,$zero,prompt4 # if $s8 < $s7, goes to prompt4
li $v0, 4
la $a0, prompt3
syscall
li $v0, 4
la $a0, prompt4
syscall
Related
I am working on a MIPS Fibonacci sequence. However, if I run the program, I input a number and all I get is infinite number of the error: [Bad instruction address] occurred and ignored infinite times. What should I do? And I am only allowed to change the code between the ##########s.
Below is my MIPS code.
.data
newline:
.asciiz "\n"
str0:
.asciiz "Enter a positive integer: "
str1:
.asciiz "ERROR: received a negative integer!\n"
str2:
.asciiz "INFO: fibonacci returned "
.text
# $a0: n (<1024)
################################################################################FIXME
fibonacci:
li $t2, 1
addiu $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
addiu $s0, $a0, 0
beq $zero, $s0, ResultOne
beq $t2, $s0, ResultOne
bltz $s0, Negative
addiu $a0, $s0, -1
jal fibonacci
addiu $s1, $v1, 0
addiu $a0, $s0, -2
jal fibonacci
addu $v1, $v1, $s1
j finish
ResultOne:
li $v0, 1
li $v1, 1
j Finish
Negative:
li $v0, 0
j Finish
Finish:
lw $s1, 8($sp)
lw $s0, 4($sp)
lw $ra, 0($ra)
addiu $sp, $sp,12
jr $ra
# FIXME
################################################################################
.globl main
main:
# print_string str0
li $v0, 4
la $a0, str0
syscall
# $t0 = read_int
li $v0, 5
syscall
move $t0, $v0
# $s0 = $ra; fibonacci($t0); $ra = $s0
move $s0, $ra
move $a0, $t0
jal fibonacci
move $ra, $s0
# $t0 = $v0; $t1 = $v1
move $t0, $v0
move $t1, $v1
# if ($t0 == 0) { goto main_failure }
beq $t0, $zero, main_failure
main_success:
# print_string str2
li $v0, 4
la $a0, str2
syscall
# print_int $t1
li $v0, 1
move $a0, $t1
syscall
# print_string newline
li $v0, 4
la $a0, newline
syscall
# goto main_return
b main_return
main_failure:
# print_string str1
li $v0, 4
la $a0, str1
syscall
main_return:
# return
jr $ra
so I tried this code, took some parts of it from this forum. my answer is not coming okay though. I'm getting weird numbers for min and max. can anyone tell me what did I do wrong?
.text
.globl __start
__start: # execution starts here
la $t0,array # $t0 will point to the elements
lw $t2,($t0) # initialize min = a[0]
lw $t3,($t0) # initialize max = a[0]
addi $t4, $s0, 0
li $t1, 0
loop:
bge $t4, 8, EndLoop
lw $t2, array($t1)
bgt $s2, $s0, SetMax
blt $s2, $s1, SetMin
cont:
addi $s1, $s1, 4
addi $t4, $t4, 1
j loop
SetMax:
move $t3, $s2
j cont
SetMin:
move $t2, $s2
j cont
EndLoop:
li $v0 4
la $a0 ans2
syscall
li $v0 1
la $a0 ($s0)
syscall
li $v0 4
la $a0 Space
syscall
li $v0 4
la $a0 ans1
syscall
li $v0 1
la $a0 ($s1)
syscall
la $a0,endl # syscal to print out
li $v0,4 # a new line
syscall
li $v0,10 # Exit
syscall # Bye!
.data
array: .word 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13
ans1: .asciiz "min = "
ans2: .asciiz "\nmax = "
endl: .asciiz "\n"
Space: .asciiz " "
The basic flow of the program is as follows:
1. Print the original content of array.
2. Ask the user for two indices X and Y.
3. Swap the two elements if Array[X] > Array[Y].
4. Print the modified array only if swapping occurs.
.data
array: .word 8, 2, 1, 6, 9, 7, 3, 5, 0, 4
newl: .asciiz "\n"
.text
la $a0, array
li $a1, 10
jal printArray
#Ask the user for two indices
li $v0, 5 #System call code for read_int
syscall
addi $t0, $v0, 0 # first user input in $t0
li $v0, 5 #System call code for read_int
syscall
addi $t1, $v0, 0 # second user input in $t1
swap : li $a1, 0 #inx1
li $a2, 0 #inx2
sll $t0, $a1, 2 # $t0 = inx1* 4
add $t0, $t0, $a0 # $t0 is the address of A[$a1]
sll $t3, $a2, 2 # $t3 = inx2* 4
add $t3, $t3, $a0 # $t3 is the address of A[$a2]
lw $t1, 0($t0) # $t1 = A[$a1]
lw $t2, 0($t3) # $t2 = A[$a2]
ble $t1, $t2, noPrint# if A[$a1] <=A[$a2] goto noPrint
sw $t1, 0($t3) # do the swap
sw $t2, 0($t0) # do the swap
syscall
jal printArray
printArray:
addi $t1, $a0, 0 #$t1 is the pointer to the item
sll $t2, $a1, 2 #$t2 is the offset beyond the last item
add $t2, $a0, $t2 #$t2 is pointing beyond the last item
loop: beq $t1, $t2, end
lw $t3, 0($t1) #$t3 is the current item
li $v0, 1 # system call code for print_int
addi $a0, $t3, 0 # integer to print
syscall # print it
addi $t1, $t1, 4
j loop # Another iteration
end:
li $v0, 4 # system call code for print_string
la $a0, newl #
syscall # print newline
jr $ra # return from this function
noPrint:
li $v0, 10 # system call code for exit
syscall # terminate program
I'm having problems with swap function. It doesn't print anything on console window(using QtSpim). Can you please recommend any suggestion?
I am currently beginning to study the MIPS language and I am trying to write a bubble sort code in MIPS, but I guess I have made a mistake somewhere and i can't seem to find it. I believe I am storing elements properly but I don't know why it is not working. Here is a picture of my code:
.data
array: .space 100
i: .word 0
j: .word 0
temp: .word
.text
la $s2, i
la $s3, j
la $s4, temp
li $v0,5
syscall
move $s0, $v0 #array size
li $v0, 8
la $a0, array
li $a1, 100
move $s1, $a0 #actual array(with elements)
syscall
move $a0, $s0 #size becomes argument
move $a1, $s1 #array becomes argument
jal BubbleSort
li $v0, 4
move $a0, $v0
syscall
li $v0, 10
syscall
BubbleSort:
move $s0, $a0
move $s1, $a1
j ExternalLoop
ExternalLoop:
slt $t0,$s2, $s0 #for(i=0;I<N;i++)
beq $t0, $zero, ExitLoop
j InternalLoop
addi $s2, $s2,1 #i++
li $s3, 0 #reset j after every iteration
InternalLoop:
slt $t1, $s3, $s0 #for(j=0;J<N;j++)
beq $t1, $zero, ExitLoop
#array[j]=s1+4*j :
#sll $t2, $s3, 2
#add $s1, $s1, $t2
lb $t3, 0($s1) #array[j]
lb $t4, 4($s1) #array[j+1]
#swapping:
move $s4, $t3
move $t3, $t4
move $t4, $s4
sb $t5, 0($t3)
addi $s3, $s3, 1 #j++
j InternalLoop
move $v0, $t5
ExitLoop:
jr $ra
Maybe my mistake is somewhere at the end where I am storing the return values but I don't know. Can anyone help?
I'm writing a MIPS program (assembly language) that takes in 10 integers and prints out a histogram represented by asterisks.
E.g.:
User input of 1, 2, 3, 4
Output:
*
**
***
****
I have most of this code written already in MIPS. The problem I am running into is printing out the correct length of asterisks. As of now it is simply printing out the a histogram all of the same length; the FIRST user inputed integer.
# program functionality:
.data
menu: .asciiz "\n1. New Histogram\n2. Print Histogram\n3. Quit\n"
prompt: .asciiz "\nEnter 10 numbers between 0 and 50 (inclusive):\n"
prompt1: .asciiz "\nEnter a valid number:\n"
asterisk: .asciiz "*"
space: .asciiz "\n"
array: .word 0:10
.text
main:
do:
jal print_menu
li $v0, 5
syscall
beq $v0, 1, new
beq $v0, 2, print
beq $v0, 3, quit
j do # end do
new:
jal new_user
j do
print:
jal print_user
j do
j quit
print_menu:
la $a0, menu
li $v0, 4
syscall
jr $ra
new_user:
la $a0, prompt
li $v0, 4
syscall
enter_loop:
la $t0, array
li $t1, 10
enter_loop_2:
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($t0)
addi $t1, $t1, -1
beqz $t1, end_loop_2
addi $t0, $t0, 4
j enter_loop_2
end_loop_2:
jr $ra
print_user:
la $t0, array
li $t1, 10
pLoop:
la $a0, space
li $v0, 4
syscall
asterisk_fun:
li $v0, 1
lw $a0, ($t0)
syscall
counter:
la $a0, asterisk
li $v0, 4
syscall
addi $a0, $a0, -1
beqz $a0, asterisk_end
j counter
asterisk_end:
jr $ra
addi $t1, $t1, -1
beqz $t1, endpLoop
addi $t0, $t0, 4
j pLoop
endpLoop:
jr $ra
quit:
li $v0, 10
syscall
The problems is that you are overwriting register $a0 in counter with the address of the asterisk, and you also used $a0 to count the number of items in that bucket.
Easy solution is to use other register (e.g. $a1) to count the number of items:
That would be:
#... your code
asterisk_fun:
li $v0, 1
lw $a1, ($t0) # Load number in $a1
move $a0, $a1 # move to $a0 just to print it
syscall
la $a0, asterisk
counter:
li $v0, 4
syscall
addi $a1, $a1, -1 # we use $a1 to keep the counter
beqz $a1, asterisk_end
j counter
asterisk_end:
# ... more of your code