.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.
Related
The Programm compiles just fine but as soon as i give input this error is thrown:
"Error in line 28: Runtime exception at 0x0040002c: address out of range 0x00000001
Processing terminated due to errors."
Here ist the code for my programm:
.data
line: .space 80
.text
main:
# read line from stdin
li $v0, 8
la $a0, line
li $a1, 80
syscall
# call caesar_line
li $a0, 1
jal caesar_line
# return 0
li $v0, 0
jr $ra
caesar_line:
# save start of line
move $t0, $a0
# loop through line
loop:
# load the character from memory
move $t1, $a0
lb $a0, 0($t1)
beqz $a0, end_loop
jal caesar_char
sb $v0, 0($t1)
addi $t1, $t1, 1
move $a0, $t1
j loop
end_loop:
# print line
li $v0, 4
move $a0, $t0
syscall
jr $ra
caesar_char:
# check if c is lowercase
blt $a0, 'a', upper
bgt $a0, 'z', not_alpha
addi $a1, $a1, 'a'
jal caesar_helper
j done
upper:
# check if c is uppercase
blt $a0, 'A', not_alpha
bgt $a0, 'Z', not_alpha
addi $a1, $a1, 'A'
jal caesar_helper
j done
not_alpha:
# not an alphabetical character
move $v0, $a0
j done
caesar_helper:
# subtract base from c
sub $t0, $a0, $a2 # $t0 = c - base
# add distance + 26 to c
addi $t0, $t0, 26 # $t0 += 26
add $t0, $t0, $a1 # $t0 += distance
# calculate c % 26
li $t1, 26 # $t1 = 26
div $t0, $t1 , $t1 # $lo = c % 26
mflo $t0
# add base to c
add $v0, $t0, $a2 # $v0 = c + base
done:
jr $ra
Not even ChatGBT could find an error.
The code should shift all chars of an string by a certain amount, but as soon as it is run the error above is thrown.
I load two arrays. I have to return a third array that contain the sum of the two and has the dimension of the smaller. Print the last one.
Example:
INPUT: Array1: [1 2 3 4 5] Array2: [2 6 1]
OUTPUT: Array3: [3 8 4]
Program runs... it give me the exact number of elements but every element is 0.
.data
vett1: .word 0:100
vett2: .word 0:100
vett3: .word 0:100
x: .word 0
space: .asciiz " "
.text
.globl main
main:
la $a0, vett1
la $a1, vett2
la $a2, vett3
la $a3, x
li $t0, 0
li $t1, 0
li $t2, 0
jal loadA1 #mi salvo 26 salto a caricavettore1
jal loadA2 #mi salvo 27 salto a caricavettore2
jal lenght #mi salvo 28 salto a controllalunghezza
lw $t2, ($a3)
lw $t3, ($a3)
la $a0, vett1
la $a1, vett2
la $a2, vett3
jal summ
jal print
li $v0, 10
syscall
loadA1:
li $v0, 5
syscall
beq $v0, -1, exit
sw $v0, ($a0)
addi $t0, $t0, 1
addi $a0, $a0, 4
j loadA1
exit: jr $ra
loadA2:
li $v0, 5
syscall
beq $v0, -1, exit2
addi $t1, $t1, 1
sw $v0, ($a1)
addi $a1, $a1, 4
j loadA2
exit2: jr $ra
lenght:
blt $t0, $t1, cond1
sw $t1, ($a3)
jr $ra
cond1: sw $t0, ($a3)
jr $ra
summ:
subi $sp, $sp, 4
sw $ra, 0($sp)
bnez $t2, rec
j exit3
rec: lw $s0, ($a0)
lw $s1, ($a1)
add $v0, $s0, $s1
sw $v0, ($a2)
addi $a0, $a0, 4
addi $a1, $a1, 4
addi $a2, $a2, 4
subi $t2, $t2, 1
jal summ
exit3:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
print:
beqz $t3, exit4
lw $a0, ($a2)
li $v0, 1
syscall
la $a0, space
li $v0, 4
syscall
addi $a2, $a2, 4
subi $t3, $t3, 1
j print
exit4: jr $ra
When you sum the two arrays you add 4 to $a2 at the end of each iteration to point it to the next element of vett3. So after the summing is complete $a2 will point to the first memory location after the end of vett3.
Then you call print but don't reset $a2 to point at the start of vett3, so you end up printing garbage data (which could happen to be all zeroes - or mostly zeroes at least, since x and space will be there).
Hi I have to write a program in MIPS that does a bubble sort and then prints the sorted array and plays a note(s). I am having trouble getting it to execute in Mars and I am wondering what I need to add to my program:
.include "ec2_data.asm" # load values into array
add $s0, $zero, 0x10010000
add $t0, $zero, 0x10010000 #how many times to loop
addi $t1, $zero, 0 #initilize the counter
addi $t2, $zero ,0 #initilize position x
addi $t3, $zero, 4 #initilize position y
lw $s2, $t3($s0) #get second position of $s0=y
LOOP:
addi $t0, $t0, -1 #subtract one from the counter
slt $t4, $s1, $s2 #t4 set to 1 if x > y
beqz $t4, BREAK #if t0 is zero (x<y) we dont' have to sort, so go to break
# sort:
add $t5, $zero, $s1 #temp. stores the value of x in t5
add $s1, $zero, $s2 #moves the value of y into x
add $s2, $zero, $t5 #moves the value of x into y
sw $s1, $t2($s0) #save the new x to register
sw $s2, $t3($s0) #save the new y to register
j BREAK
BREAK:
#in here: check to see if we have reached the end
#if not, increment t2 and t3 by 4 to get the next values and jump back to loop to go again
beq $t0, $zero, END #if we are done with the array, go to end
addi $t2, $t2, 4
addi $t3, $t3, 4
j LOOP #jump back to loop again
END:
li $v0, 1 # print integer
syscall
addi $a0, $0, 0xA # load line code into register
addi $v0, $0, 0xB # print new line
syscall
addi $v0, $zero, 33 # midi out synchronous
addi $a0, $zero, 60 # Middle-C
addi $a1, $zero, 250 # ms
addi $a2, $zero, 120 # some instrument
addi $a3, $zero, 64 # some volume
add $a0, $t7, $zero # load value into $a0
syscall # play note!
addi $t6, $t6, 4 # shift memory location by 32 bits
addi $s1, $s1, 1 # increment counter ++
j loop # loop
Exit:
li $v0, 10 # load exit code
syscall # exit
If by "having trouble getting it to execute", you mean it acts strange with the .include file, then you might not have a required option checked in MARS. Go to the Settings menu and check the box for "Initialize Program Counter to global Main if defined". That seems to be something that has to be in effect for included files to work - and for some reason, it isn't turned on by default.
I have a program that will capitalize all lowercase letters and lowercase all the uppercase letters entered in a string by the user. It does this by adding or subtracting 32 from the character value to get the desired character. My problem is that it doesn't change anything in the string. Any suggestions on what to change?
.data
prompt: .asciiz "\n\nEnter an string of characters: "
result: .asciiz "\n\nHere is the string you entered: "
after_sort: .asciiz "\n\nHere is the string after the case sorting: "
buffer: .space 80
.text
main:
#Prints the prompt string
li $v0, 4
la $a0, prompt
syscall
#reads string from user and saves in $a0
li $v0, 8
la $a0, buffer
li $a1, 80
syscall
#Prints the result string
li $v0, 4
la $a0, result
syscall
#Prints the string entered by the user
la $a0, buffer
li $v0, 4
syscall
li $t0, 0 # t0 = i = 0
for_loop:
slti $t1, $t0, 80 # t1 = 1 if and only if t0 < 80
beq $t1, $0, for_loop_done
slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower
upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
addi $t0, $t0, 1
j for_loop
for_loop_done:
#Prints the result string
li $v0, 4
la $a0, after_sort
syscall
#Prints the string entered by the user
la $a0, buffer
li $v0, 4
syscall
exitProgram: li $v0, 10 # system call to
syscall # terminate program
You are using $a0as a character, such as here:
slti $t2, $a0, 91
but it is never filled with the character. At the moment, it contains a memory address, not a character.
You should load the character using lb and store it back after making it upper/lowercase using sb.
Feel free to add a comment if you want a code example.
Edit: the changes in the relevant part of the code:
...
li $t0, 0 # t0 = i = 0
for_loop:
slti $t1, $t0, 80 # t1 = 1 if and only if t0 < 80
beq $t1, $0, for_loop_done
lb $t4, 0($a0)
beqz $t4, for_loop_done
beq $t4, 10, for_loop_done
slti $t2, $t4, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower
upper:
addi $t4, $t4, 32 #adds 32 to the character value to lowercase it
j done
lower:
addi $t4, $t4, -32 #subtracts 32 from the character value to capitalize it
done:
addi $t0, $t0, 1
sb $t4, 0($a0)
addi $a0, $a0, 1
j for_loop
for_loop_done:
#Prints the result string
...
It's easy to forget that in assembly, you can't do this:
if something
do this
else
do that
There is no "else", only shudder Goto.
So in this code:
slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower
upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
addi $t0, $t0, 1
When you branch to upper, it adds 32. Then it subtracts 32, because execution progressed to the next line. So your code capitalizes lower case, but does nothing to uppercase.
You need to add a jump to the first instruction after your if/then/else equivalent:
upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
j done # No, I don't want to subtract it again!
lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
done:
addi $t0, $t0, 1
In fact, you should probably get rid of the bne altogether - it's redundant. If beq doesn't branch, then it's not equal. So this would be the finished product:
slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
# Otherwise, it's lower
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
j done
upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
done:
addi $t0, $t0, 1
Hope that helps!
(Edit: #Patrik is right too, you need to "dereference" $a0. My example doesn't take that into account.)
here is my code:
it works perfectly
.data
theString:
.space 20
prompt: .asciiz "Enter a string of characters: "
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 8
la $a0, theString
li $a1, 20
syscall
li $v0, 4
syscall
la $t1,theString
for: lb $a0, 0($t1)
beqz $a0,out #to find out end of string
beq $a0,10,out #to find out end of string
slti $t2, $a0,91 #if $a0<91 $t2=1
beq $t2,1,small
beq $t2,0,capital
capital:
subu $a0, $a0, 32
li $v0,11
syscall
addi $t1,$t1,1
j for
small:
addi $a0, $a0, 32
li $v0,11
syscall
addi $t1,$t1,1
j for
out:
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