the program is about done, I am just having an issue with getting the value of $v0 to be put into the array "intarray" each time it loops through, e.g input "1" at intarray[0], "2" at intarray[1] etc...
The program asks for 5 integers via a loop. I need to set the integers into the intarray at each of the 5 spots. I don't understand how to do this in MIPS.
I am also trying to get the final value of $t1 into the variable "total", but I think that requires the same thing as intarray. The printing of these arrays would be good to know too.
The main issue is how to take a value in for example $t0 and put it into a position such as intarray[1].
Thanks for your help.
Here is the code:
.data
intarray: .word -1, -1, -1, -1, -1
total: .word 0
result: .asciiz "Enter a series of 5 integers, one per line: "
thetot: .asciiz "Your total is: "
.text
main:
li $v0, 4 #call to print string
la $a0, result #print string from location
syscall
li $t0, 0 #set $t0 = i = 0
li $t1, 0 #set $t1 = tot = 0
la $t2, intarray #puts the address of intarray into $t2
loop:
li $v0, 5 #system call code to accept integer
syscall
add $t1, $t1, $v0 #moves integer to $t0
add $t0, $t0, 1
blt $t0, 5, loop #end loop if i<5
li $v0, 4
la $a0, thetot
syscall
li $v0, 1
la $a0, ($t1)
syscall
li $v0, 10 #exits program
syscall
Related
i can find the max perfectly fine but when it comes to finding the min, im having some trouble, my max finds the max number good but my min always prints out 0 no matter what, i cant seem to find out what is wrong with my code, does anyone see the flaw in my code?
For exmaple:
i enter in 5 integers
5 10 15 20 25
Array: 5,10,15,20,25
Min: 0
Max: 25
.text
.globl main
main:
la $a0, ask #ask user for integer thats going to be the size of the array
li $v0, 4
syscall
li $v0, 5 #store that int
syscall
move $t1,$v0 #size of my array stored in $t1
la $t0, array #load address of our array
li $t2, 0 #counter = 0
lw $t3,($t0) #initialize min = array[0]
lw $t4,($t0) #initialize max = array[0]
while:
la $a0, intask #ask user for integer
li $v0, 4
syscall
li $v0, 5 #store that int
syscall
sw $v0, ($t0) #store that int in the array
end: add $t0, $t0, 4 #increment the array to the next index
add $t2, $t2, 1 #increment the counter by 1
blt $t2, $t1,while #branch to while if counter < size of array
endw:
la $a0,display # Display "Array is: "
li $v0,4
syscall
li $t0, 0 # initilize array index value back to 0
li $t2, 0 # initial size counter back to zero
la $t0, array # load address of array back into $t0
sprint:
lw $t6,($t0) #load word into temp $t2
move $a0, $t6 #store it to a safer place
li $v0, 1 #print it out
syscall
la $a0,space # Display " "
li $v0,4
syscall
add $t0, $t0, 4 #increment the array to the next index
add $t2, $t2, 1 #increment the counter by 1
blt $t2, $t1,sprint #branch to while if counter < size of array
li $t2, 0 # initial size counter back to zero
la $t0, array # load address of array back into $t0
add $t0, $t0, 4 #increment the array to the next index
add $t2, $t2, 1 #increment the counter by 1
loop: lw $t8,($t0) # t8 = next element in array
bge $t8, $t3, notMin #if array element is >= min goto notMin
move $t3,$t8 #min = array[i]
j notMax
notMin: ble $t8,$t4, notMax #if array element is <= max goto notMax
move $t4,$t8 #max = array[i]
notMax: add $t2,$t2,1 #incr counter
add $t0,$t0, 4 #go up in index
blt $t2, $t1,loop #if counter < size, go to loop
eprint:
la $a0,nextline # Display "\n"
li $v0,4
syscall
la $a0,min # Display "min number is "
li $v0,4
syscall
move $a0, $t3 #displays min number in array
li $v0,1
syscall
la $a0,nextline # Display "\n"
li $v0,4
syscall
la $a0,max # Display "max number is "
li $v0,4
syscall
move $a0, $t4 #displays max number in array
li $v0,1
syscall
li $v0,10 # End Of Program
syscall
.data
array: .space 100
ask: .asciiz "How many numbers will be entered? no more than 15 numbers!: "
intask: .asciiz "Enter an Integer: "
min: .asciiz "The minimum number is: "
max: .asciiz "The maximum number is: "
display: .asciiz "Array: "
space: .asciiz " "
nextline: .asciiz "\n"
You are initializing $t3 (which is used for storing min) to array[0] before any values are stored in array, so $t3 ends up being initialized to 0.
It should be fine if you initialize $t3 to array[0] after values have been entered into array.
I am trying to print on the console the values of the array, which are already given as intA in .data. I.e., trying to print values of an array without user's prompt.
My code:
.data
prompt: .asciiz "The values in the array are:\n"
finished: .asciiz "\nNo more values to present"
space: .asciiz " "
intA: .word 11, 2, 3, 4, 5, 34, 0
.text
.globl main
main:
#Prints the prompt string
li $v0, 4
la $a0, prompt
syscall
# initialization of a0, a1, and t3 (i, counter)
la $a0, intA # loading starting address (base) of array in register a0
addi $a1, $zero, 6 # array size - 1
addi $t3, $zero, 0 # i initialized to 0
j loop
loop:
lw $t1, 0($a0) # loading integer (value of array) in the current address to register t1, I use lw because integer is a word (4 bytes)
# printing current value of array
li $v0, 4
la $a2, ($t1)
syscall
# spacing between values
li $v0, 4
la $a2, space
syscall
# checking that next address is not outside of the array
addi $t3, $t3, 1
slti $t2, $t3, 6
bne $t2, 1, done
# accessing next integer and jumping back to print it
addi $a0, $a0, 4
j loop
done:
# indicating program is done
li $v0, 4
la $a0, finished
syscall
The output that I am getting: output
Any idea why it doesn't print the values of the array, and also what are these squares that are printed instead?
Edit:
I changed
# printing current value of array
li $v0, 4
la $a2, ($t1)
syscall
to
# printing current value of array
li $v0, 1
lw $a2, ($t1)
syscall
Because, from what I understand, I print an integer so $v0 should be fed 1, and I should lw and not la (because it is an integer, i.e, a word)
However, now I get a runtime error in line 31: lw $a2, ($t1) Telling me that
fetch address not aligned on word boundary 0x0000000b
Solution: Instead of lw $a0, ($t1) for printing the value of $t1, I need to do add $a0, $t1, $zero, because I am trying to use the value and not to access an address.
The code I'm working on takes in a string from the user in the form "X123" (not limited to 3 characters) where X can be any non-number character, and 123 can be any series of number characters. The code then strips the non-number, converts the number part to an int, adds 5, and prints the result.
.data
msgerror: .asciiz "The string does not contain valid digits."
input: .space 9
open: .asciiz "Enter a string:\n"
close: .asciiz "The value +5 is:\n"
.text
.globl main
main:
li $v0, 4
la $a0, open
syscall
li $v0, 8
la $a0, input #read a string into a0
move $t0, $a0
syscall
li $t3,0
li $t4,9
la $t0, input #address of string
lbu $t1, 1($t0) #Get first digit of actual number
li $a1, 10 #Ascii of line feed
li $a0, 0 #accumulator
addi $t1,$t1,-48 #Convert from ASCII to digit
move $a2, $t1 #$a2=$t1 goto checkdigit
jal checkdigit
add $a0, $a0, $t1 #Accumulates
addi $t0, $t0, 1 #Advance string pointer
lbu $t1, ($t0) #Get next digit
buc1:
beq $t1, $a1, print #if $t1=10(linefeed) then print
addi $t1,$t1,-48 #Convert from ASCII to digit
move $a2, $t1 #$a2=$t1 goto checkdigit
jal checkdigit
mul $t2, $a0, 10 #Multiply by 10
add $a0, $t2, $t1 #Accumulates
addi $t0, $t0, 1 #Advance string pointer
lbu $t1, ($t0) #Get next digit
b buc1
print:
add $a0, $a0, 5
li $v0, 1
syscall
b end
checkdigit:
blt $a2, $t3, error
bgt $a2, $t4, error
jr $ra
error:
la $a0, msgerror
li $v0, 4 #print eror
syscall
end:
li $v0, 10 #end program
syscall
However, my code ends up producing:
Enter a string:
x123
The value +5 is:
1128
(128 is the expected).
How can I make one of the duplicate numbers go away? I've tried incrementing the address by 1 with print statements, but it seems not to work with anything else/not as intended.
li $v0,4
la $a0, aString
add $a0, $a0, 1
syscall
The above snippet produces 23 if 123 is entered, but I can't get it to apply to the above.
Easier overall approaches are welcome too. New to mips, so I hardly think mine is all that great.
Looks like you read the first digit twice.
This part:
li $t3,0
li $t4,9
la $t0, input #address of string
lbu $t1, 1($t0) #Get first digit of actual number
Should be changed to:
li $t3,0
li $t4,9
la $t0, input+1 #address of first digit in string
lbu $t1, ($t0) #Get first digit of actual number
This is my first time coding PCSPIM. I find that there is a little trouble with my code.
.data
user_input: .asciiz "\n\nEnter an Integer for the value of n: "
result_display: .asciiz "\nThe sum from 0 to n is "
Greeting: .asciiz "\n\nThank you!"
.text
main:
#user input
li $v0, 4
la $a0, user_input
syscall
#allow user input
li $v0, 5
syscall
#store the input value into t8
move $t8, $v0
#calculation
addi $s0, $zero, $t8
I wish to use the integer value ($t8) that users input into the #calculation section, but it ends up with error.
addi $t0, $zero, 0
loop1:
add $t0, $t0, $s0
addi $s0, $s0, -1
bne $s0, $zero, loop1
nop
nop
# Display the result
li $v0, 4
la $a0, result_display
syscall
# Print out the result
li $v0, 1
move $a0, $t0
syscall
# Greets the user
li $v0, 4
la $a0, Greeting
syscall
# Exit the program
li $v0, 10
syscall
Sorry for my broken English.
The error is in the way you are using the "addi" instruction. The instruction requires an immediate (number) value to be passed as the third operand and not an architectural register. If you update the "addi" instruction to "addu" the code should work.
sorry, I try to multiply two integers but it doesn't work. I can't find where the problem is.Maybe because of register' names but I don't know how to correct it. I correct many times but it is not successful. Could anyone give me some points?
.data
prompt1: .asciiz "Please enter the first signed (decimal) integer: "
prompt2: .asciiz "Please enter the second signed (decimal) integer: "
result_msg: .asciiz "The result of these two 16-bit integers\' multiplication is: "
.text
.globl main
main:
li $v0, 4 #print prompt
la $a0, prompt1
syscall
li $v0, 5 #read multiplicand
syscall
move $s0, $v0
li $v0, 4 #print prompt
la $a0, prompt2
syscall
li $v0, 5 #read multiplier
syscall
move $s1, $v0
Mult: ori $t0,$zero,1 #mask
move $s3, $0 #initialize the result register
move $t1, $0
loop: beq $s1, $zero, end #if the multiplier is 0 then finished
and $t1, $t0, $s1 #mask
beq $t1, 1, mult_add
beq $t1, 0, shift
mult_add: addu $s3, $s3, $s0 #add to get product
shift:
sll $s0, $s0, 1 #shift multiplicand left
srl $s1, $s1, 1 #shift multiplier right
j loop
end:
jr $ra
result: #input the print_string
li $v0, 4
la $a0, result_msg
syscall
exit:
li $v0, 1 #input result
move $a0, $s3
syscall
li $v0, 10 #exit
syscall
Inspecting your code I see that you jump to label end when you are done multiplying.
The instruction at that label issues a jr $ra which "returns from a function", but I guess you just want to print the result and exit.
Therefore I'd suggest you remove that instruction so as to print the result and exit and maybe remove label result as it is not used anywhere in your code.