I'm stuck on an exercise, and am unsure how to proceed. This is the exercise:
Write a MIPS assembly language procedure, Test, that accepts 2 integers as arguments and
returns 0 if the integers are equal, 1 if the first is less than the second, and 2 if the first is greater
than the second.
Write a MIPS assembly language program that reads in 2 integers, calls the procedure Test, then
outputs one of the following messages:
The integers are equal
The first integer is less than the second
The first integer is greater than the second
What would be an example to carry this out? Mips is very confusing to me, as I'm used to Java. Thank you.
EDIT: Here is the program I am using as a foundation, since I am unsure where to start:
.data
str1: .asciiz "Please Enter Integer 1: " # a
str2: .asciiz "Please Enter Integer 2: " # a
str3: .asciiz "The sum is " # a
newline: .asciiz "\n" # g
.text
main: addi $v0, $zero, 4
la $a0, str1
syscall
addi $v0, $zero, 5
syscall
add $s0, $zero, $v0
addi $v0, $zero, 4
la $a0, str2
syscall
addi $v0, $zero, 5
syscall
add $s1, $zero, $v0
L1: beq $s1, $zero, cont
addi $v0, $zero, 1
add $a0, $s0, $zero
addi $s1, $s1, -1
syscall
j L1
cont: addi $v0, $zero, 4
la $a0, newline
syscall
addi $v0, $zero, 10
syscall
jr $ra
To check whether one register's value is smaller than another register's value, we can use the
set-on-less-than instruction, which also has a set-on-less-than-immediate counterpart.
slt $r0, $r3, $r4
()if r3 < r4, r0 is set to 1
else r0 is set to 0
slti $r0, $r3, 10
()if r3 < 10, r0 is set to 1
else r0 is set to 0
slt and slti are similar to beq or bne, however, there are two differences. First, they test
whether one value is smaller than another value and, second, they don't branch to some
address, but, instead, set a flag, stored in the first operand.
Related
.data
enterMsg1: .asciiz "Please enter the last four digits of your student id \n"
enterMsg2: .asciiz "Press enter between each digit \n"
enterMsg3: .asciiz "Enter next digit \n"
TotalDig4: .asciiz "The total of the digits is: "
.text
# output the initial instruction text to the console
addi $v0, $zero, 4
la $a0, enterMsg1
syscall
# output the second instruction text to the console
addi $v0, $zero, 4
la $a0, enterMsg2
syscall
# read an integer from keyboard input and store the input in $s0 for the total
addi $v0, $zero, 5
syscall
# store the input in $s0
add $s0, $zero, $v0
# output the text asking for the next digit to the console
# then receive the input,
jal receiveInputs
#add to total ($s0)
add $s0, $s0, $v0
# then receive the input,
jal receiveInputs
#add to total ($s0)
add $s0, $s0, $v0
# then receive the input,
jal receiveInputs
#add to total ($s0)
add $s0, $s0, $v0
# output the total text to the console
addi $v0, $zero, 4
la $a0, TotalDig4
syscall
add $a0, $s0, $zero
addi $v0, $zero, 1
syscall
addi $v0, $zero, 10
syscall
receiveInputs:
# receive the input, add to total ($s0)
addi $v0, $zero, 4
la $a0, enterMsg3
syscall
addi $v0, $zero, 5
syscall
jr $ra
Loop:
add $s0, $zero, $s0
addi $a0, $zero, 1
syscall
# output a space
addi $v0, $zero, 10
addi $a0, $zero, 0x20
syscall
addi $s0, $s0, -1
# end the program
addi $v0, $zero, 10
After adding the first digit to $s0, on the next line add that same first digit from $v0 to $s1 which will hold your product.
Then after getting each digit from the function receiveInputs use (mul $s1, $s1, $v0) to multiply the digits.
Finally print the result from $s1 just as you would for the sum.
I'm currently trying to code a function to divide integers in MIPS. I am supposed to use a division algorithm using this flowchart:
Here is the code that I have created:
.data
.text
main:
addi $a0, $0, 7
addi $a1, $0, 4
jal divide
addi $a0, $v1, 0
addi $v0, $0, 1
syscall
addi $v0, $0, 10
syscall
divide: # $a0/$s0 = Dividend-Remainder / $a1/$s1 = Divisor / $v1 = Quotient
# Initialize
addi $s0, $a0, 0
addi $s1, $a1, 0
addi $t0, $0, 0 # Counter = $t0
addi $t1, $0, 33 # Number of loops = $t1
# Start of loop
start:
# Step 1 : Subtract the divisor register from the remainder register and place in remainder register
sub $s0, $s0, $s1
# Step 2 : Test the remainder register. If < 0 go to part B, otherwise go to part A
addi $t3, $0, 0
slt $t2, $t3, $s0
beq $t2, $zero, partB
# Step 2A : Shift quotient register to the left, and make least significant bit 1
sll $v1, $v1, 1
ori $v1, $v1, 1
j after # Skip step 2B
# Step 2B : Add divisor to remainder and place sum in remainder register
partB:
add $s0, $s1, $s0
# Shift quotient register to left and make least significant bit 0
sll $v1, $v1, 1
after:
# Step 3 : Shift divisor right 1 bit
srl $s1, $s1, 1
# Increment counter
addi $t0, $t0, 1
# Test loop
bne $t0, $t1, start
jr $ra
I've been trying to catch my error but so far I have been unable to find it. When I run the code I get -1073741825 for the output.
Can someone check my logic and tell me what I'm doing wrong?
I have some problem when I'm writing a sorting program in MIPS.
The function of this program is like this: Input 10 integers from keyboard and print them out, then sort them and print the sorted array again.
But the output is different from what I expect, some numbers are not sorted, would someone help me?
.data
str1: .asciiz "Please input integer numbers, maximum 10: \n"
str2: .asciiz "The 10 integer numbers you input are: \n"
str3: .asciiz "The numbers you entered are sorted as: \n"
array: .space 40
space: .asciiz " "
.text
.globl main
main:
la $t6, array #load the address of array into $t6
move $t7, $t6
addi $t7, $t7, 40 #point $t7 to the end of the array
jal readin
la $t6, array
la $a0, str2
li $v0, 4
syscall
jal print1 #print out the array before sort
la $t0, array #put the address of array into $t0
add $t0, $t0, 40 #put $t0 to the end of the array
move $t1, $zero #set $t1 as counter of the outerloop
li $t2, 10 #set $t2 as number of the outerloop
la $t3, array #put the address to $t3
jal innerloop
la $a0, str3
li $v0, 4
syscall
la $t3, array
#move $t7, $t3
#add $t7, $t7, 40
jal print2
li $v0, 10
syscall
innerloop:
lb $t4, 0($t3)
lb $t5, 4($t3)
bgt $t4, $t5, swap
addi $t3, $t3, 4
blt $t3, $t0, innerloop #if $t3 < $t0, keep in the innerloop to check two #neighbor numbers
jal outerloop #jump to outerloop if one round is finished
swap:
sb $t4, 4($t3)
sb $t5, 0($t3)
addi $t3, $t3, 4
#sll $t3, $t3, 2
blt $t3, $t0, innerloop #
jr $ra
outerloop:
la $t3, array
addi $t1, $t1, 1 #add the outerloop counter $t1 by one,
addi $t0, $t0, -1
blt $t1, $t2, innerloop #if $t1 < $t2, keep searching
jr $ra
readin:
la $a0, str1 #print str1
li $v0, 4
syscall
li $v0, 5 #read in the number
syscall
sb $v0, ($t6) #store the number in the array
add $t6, $t6, 4
blt $t6, $t7, readin
jr $ra
print1:
lb $a0, ($t6)
li $v0, 1
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
add $t6, $t6, 4
blt $t6, $t7, print1
jr $ra
print2:
lb $a0, ($t3)
li $v0, 1
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
add $t3, $t3, 4
blt $t3, $t0, print2
jr $ra
And the result in SPIM is like this:
Please input integer numbers, maximum 10:
44
Please input integer numbers, maximum 10:
11
Please input integer numbers, maximum 10:
33
Please input integer numbers, maximum 10:
22
Please input integer numbers, maximum 10:
66
Please input integer numbers, maximum 10:
55
Please input integer numbers, maximum 10:
99
Please input integer numbers, maximum 10:
77
Please input integer numbers, maximum 10:
88
Please input integer numbers, maximum 10:
24
The 10 integer numbers you input are:
44 11 33 22 66 55 99 77 88 24 The numbers you entered are sorted as:
11 33 22 44 55 66 77 88 24 32
Would someone please explain to me why there is a 32 in the end and why they are not sorted properly?
I'm starting with mips32 and I'm getting stuck when trying to get a letter from a string to print it. The code should get the string, print it character by character and when it finds an i print iiing.
.data
msg: .asciiz "testing"
i: .asciiz "iiing"
.text
.globl main
main:
la $t0, msg
la $t1, i
li $t2, 0
loop:
bneq $t0, 105, end #$t0=i?
lb $a0, ($t0)
li $v0, 4
syscall
addi $t0, $t0, 1
b loop
end:
move $a0, $t1
li $v0, 4
syscall
Where is the problem?
You have a few problems.
You are comparing $t0, which is the address of the current character, not the character itself. Move that test below the lb line and test against $a0.
105 in ASCII is E, not i. Try 151 (or to be more normal, 0x69).
You want to compare with beq, not bneq.
Inside the loop, you should use syscall 11, which prints a single character, rather than the current syscall 4 you're using, which prints a string.
Your program doesn't make an exit syscall (10) at the end.
You can look at this link for a list of syscalls.
Here's a complete working program for reference:
.data
msg: .asciiz "testing"
i: .asciiz "iiing"
.text
.globl main
main:
la $t0, msg
la $t1, i
li $t2, 0
loop:
lb $a0, ($t0)
beq $a0, 0x69, end
li $v0, 11
syscall
addi $t0, $t0, 1
b loop
end:
move $a0, $t1
li $v0, 4
syscall
li $v0, 10
syscall
I am trying to sort an array of integers in MIPS using bubble sort but every time that I run bubble sort I get an address out of range error. I have been staring at the code for hours and have no idea why this is happening. Hopefully' it is something really obvious that someone with more experience can see and help me fix.
The point of the program is simply to read in integers and symbols (price of stock, and stock symbol) and then sort those based on the prices. Here is the code:
.data
welcome: .asciiz "Welcome!\n"
prompt1: .asciiz "Enter how many stocks you have.\n >"
prompt2: .asciiz "Enter the four character NASDAQ abbrevation and price for each stock.\n"
prompt3p1: .asciiz "You have entered "
prompt3p2: .asciiz " stock abbreviations and prices. How many stocks do you want to buy:\n"
prompt4: .asciiz "How many stocks do you want to sell:\n"
InfoPrompt: .asciiz "Here are the symbols and the corresponding numbers that were entered: \n"
numStocks: .word 0
stockPrice: .space 40 # this will be the list of the stock prices
stockSymbol: .word 0:20 # this will be for the lit of stock abbrevs
symbols: .space 12 #this should be used for a 40 character array
inputFormat: .asciiz "\n> "
length: .word 0
buffer: .space 4
.globl main
.text
main: # display all of the propts
li $v0, 4 # get ready to print welcome
la $a0, welcome
syscall # print welcome
li $v0, 4 # not sure this is necessary since it was already loaded into v0 before
la $a0, prompt1
syscall # print("enter how many stocks you have.")
li $v0, 5 # this will get ready to accept the number of stocks
syscall # this should store the number into the $v0 register
sw $v0, numStocks # store in memory so we dont lose it
move $t0, $v0 # this will hold the num of stocks
#before we go into the procedure we need to declare and move the stack for the $t registers.
la $t1, symbols
la $t2, stockPrice
sub $sp, $sp, 8
sw $t1, 0($sp)
sw $t2, 4($sp)
jal getInfo
# now that we have all the information lets run a test to see how successful we were
# jal printInfo
li $v0, 4 #get ready to ask how many items you want to buy
la $a0, prompt3p1
syscall
li $v0, 1
lw $a0, numStocks # prints the number of stocks in the portfolio
syscall
li $v0, 4
la $a0, prompt3p2
syscall
# we need to now get the number of stocks the person wants to buy
li $v0, 4
la $a0, inputFormat
syscall #format the next input
li $v0, 5
syscall
move $t3, $v0 # the number of stocks we want to buy is now stored as $t3
#we need to get how many they want to buy
li $v0, 4
la $a0, prompt4
syscall
li $v0, 4
la $a0, inputFormat
syscall #format the next input
li $v0, 5
syscall
move $t4, $v0 # the number of stocks that you want to sell
# now we have to sort the list to figure out what elements we are going to sell and buy
la $a2, stockPrice
la $a0, stockPrice
la $a1, numStocks
jal buble_sort
jal printInfo
# end program
li $v0,10 #load the syscall number for terminating
syscall #terminate
####################################################################
# This will iterate for the number of stocks
# Only accepts the number of stocks
####################################################################
getInfo:
sub $sp, $sp, 8
sw $ra, 0($sp) # store the return value
sw $t0, 4($sp) # Save the t registers that we will be using
li $v0, 4 # set up the first call to initialize the calls for the abbreviations and numbers
la $a0, prompt2
syscall
# we want to have a place to store the symbols
la $t1, symbols
la $t2, stockPrice
GI_loop:
beq $t0, $zero, GI_loop_done # if the counter == 0 then we are done
li $v0, 4
la $a0, inputFormat
syscall #format the next input
# ask for the string input
li $v0, 8
la $a0, 0($t1)
li $a1, 6
syscall
#store the value in the array
addi $t1, $t1, 6 #increment our "array"
# ask for the integer input
li $v0, 4
la $a0, inputFormat
syscall #format the next input
li $v0, 5
syscall # get the integer value that we require
sw $v0, 0($t2) #store the value
addi $t2, $t2,4 #increment our counter
addi $t0, $t0, -1 # decrement our counter
j GI_loop
GI_loop_done:
lw $ra, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
lw $t2, 12($sp)
add $sp, $sp, 8
jr $ra
####################################################################
# This will go through the lists and print out what was stored
# will go through the symbols then the numbers
####################################################################
printInfo:
sub $sp, $sp, 12
sw $ra, 0($sp)
sw $t0, 4($sp) # this will store the number of stocks that were entered
sw $t1, 8($sp)
li $v0, 4
la $a0, InfoPrompt
syscall
# we know that $t0 stores the correct number that was originally enetered so we need to loop through and print all the integers
InfoLoop:
beq $t0, $zero, InfoLoopDone # a basic counter check
#li $v0, 4
#la $a0, 0($t1)
#syscall
#addi $t1, $t1, 6
#addi $t0, $t0, -1
#j InfoLoop
################################### INTEGER PRINT WORKING
li $v0, 1 # this will print out the integers
lw $a0, 0($t2) # we have to load the world that is found in the address of $t2
syscall
addi $t2,$t2, 4 # this will increment the array
addi $t0, $t0, -1 ## this will fix our counter
j InfoLoop
InfoLoopDone:
lw $ra, 0($sp)
lw $t0, 4($sp)
add $sp, $sp, 8
jr $ra
################################
# BUBBLE SORT
################################
buble_sort:
#a0=address of table
#a1=sizeof table
add $t0,$zero,$zero #counter1( i )=0
loop1:
addi $t0,$t0,1 #i++
bgt $t0,$a1,endloop1 #if t0 > a1 break;
add $t1,$a1,$zero #counter2=size=6
loop2:
bge $t0,$t1,loop1 #j < = i
#slt $t3,$t1,$t0
#bne $t3,$zero,loop1
addi $t1,$t1,-1 #j--
mul $t4,$t1,4 #t4+a0=table[j]
addi $t3,$t4,-4 #t3+a0=table[j-1]
add $t7,$t4,$a2 #t7=table[j]
add $t8,$t3,$a2 #t8=table[j-1]
lw $t5,0($t7)
lw $t6,0($t8)
bgt $t5,$t6,loop2
#switch t5,t6
sw $t5,0($t8)
sw $t6,0($t7)
j loop2
endloop1:
jr $ra
Your problem is in the statement before calling your bubble sort routine:
la $a1, numStocks
It will load the address where the number of stocks are saved, not the number of stocks itself.
You should change it with
lw $a1, numStocks